I have the basic program running with no issues. It reads 3 voltages, Battery/Solar, 110VAC to 12VDC power supply, the load side of 2 Blocking Diodes and prints them out. Now I want to read each voltage 10 times to get a better average of the actual voltage. I am trying to use “While” in a loop, to get the 10 voltage readings, then divide the sum of the 10 readings by 10 and use the average in the display calculation.
No mater where I place the While code, it doesn’t work. I have tried numerous examples, with no luck.
Here is the working code, with the :“While” code commented out, between the dashed lines. I also commented out all but the A0 entries needed for the averaging code.
Also, after much redoing, I finally got the voltages to display properly whether the input voltage is 12, 9 or 5 VDC. I designed a UPS PCB board that I am using for my Home Automation and Energy Management electronics which will use battery & SOLAR and a 110VAC to 12VDC 20 Amp power supply as inputs. When the AC Power supply drops off because of a power outage the electronics will continue working with no interruptions as the Battery/Solar will be also powering it.
Thanks
// set reference voltage;
float refVcc = 12.0;
// float refVcc = 09.0;
// float refVcc = 5.0;
#define NUM_SAMPLES 10 // Number of samples to take
int sum_A0 = 0; // sum of A0 samples taken
// int sum_A1 = 0; // sum of A1 samples taken
// int sum_A2 = 0; // sum of A2 samples taken
unsigned char sample_count_A0 = 0; // current sample for A0
// unsigned char sample_count_A1 = 0; // current sample for A1
// unsigned char sample_count_A2 = 0; // current sample for A2
void setup()
{
Serial.begin(9600);
}
void loop()
{
// read the input on analog pin 0, 1, 2:
int DiodeOutputVoltage = analogRead(A0);
// -------------------------------------------------------------
// While sample_count_A0 is < NUM_SAMPLES
// {
// sum_A0 += analogRead(A0); Not sure if this code will work
// sample_count_A0 ++; I have tried it in different places
// delay(10); with no luck. Need to do a sample of
// } 10 to average out the voltages
// -------------------------------------------------------------
int VoltageSource_1 = analogRead(A1);
int VoltageSource_2 = analogRead(A2);
// print out the converted (voltage) value
Serial.println ("----------------------------------------");
Serial.println (" HOME AUTOMATION - ENERGY MANAGEMENT");
Serial.println (" UPS System Control Voltages");
Serial.println ("----------------------------------------");
Serial.print ("Diode Output Voltage: ");
Serial.print (DiodeOutputVoltage * 1.6 * refVcc/1024);
Serial.println ( " Volts");
Serial.println ();
Serial.print ("SOLAR / BATTERY INPUT: ");
Serial.print (VoltageSource_1 * 1.61 * refVcc/1024);
Serial.println ( " Volts");
Serial.println ();
Serial.print ("AC POWER SUPPLY INPUT: ");
Serial.print (VoltageSource_2 * 1.61 * refVcc/1024);
Serial.println ( " Volts");
Serial.println ("----------------------------------------");
Serial.println ();
Serial.println ();
delay(5000);
}
Serial Monitor Output:
18:16:56.066 ->
18:17:00.982 -> ----------------------------------------
18:17:01.029 -> HOME AUTOMATION - ENERGY MANAGEMENT
18:17:01.082 -> UPS System Control Voltages
18:17:01.082 -> ----------------------------------------
18:17:01.129 -> Diode Output Voltage: 12.04 Volts
18:17:01.183 ->
18:17:01.183 -> SOLAR / BATTERY INPUT: 12.06 Volts
18:17:01.229 ->
18:17:01.229 -> AC POWER SUPPLY INPUT: 12.02 Volts
18:17:01.283 -> ----------------------------------------
18:17:01.330 ->
18:17:01.330 ->