// Constants for A/D converter resolution
// Arduino has 10-bit ADC, so 1024 possible values
// Reference voltage is 5V if not using AREF external reference
// Zero point is half of Reference Voltage
// Vout is read 1000 Times for precision
for(int i = 0; i < 1000; i++) {
Vout = (Vout + (resADC * analogRead(A0)));
Vout -= 2.78;
delay(1);
}
// Get Vout in mv
Vout = Vout /1000;
// Convert Vout into Current using Scale Factor
Current = (Vout - zeroPoint)/ scale_factor;
Current +=12.51;
Current -=.13;
unsigned long CurrentTime = millis();
unsigned long ElapsedTime = CurrentTime - StartTime;
// Print Vout and Current to two Current = ");
Put all your code in a function. Then use a millis timer eg
StartTimer
If currentMillis-StartTimer > 5000
Flag a bool eg TimesUp = true
If timesUp is false
myCodeFunction ()
All my code is pseudocode to point in a direction, fill in the blanks. Read the guide to this forum posted at the top of every section to get the best help
// Variables for Measured Voltage and Calculated Current
double Vout = 0;
double Current = 0;
// Constants for Scale Factor
// Use one that matches your version of ACS712
const double scale_factor = 0.2; // 5A
//const double scale_factor = 0.1; // 20A
//const double scale_factor = 0.066; // 30A
// Constants for A/D converter resolution
// Arduino has 10-bit ADC, so 1024 possible values
// Reference voltage is 5V if not using AREF external reference
// Zero point is half of Reference Voltage
const double vRef = 5.00;
const double resConvert = 1024;
double resADC = vRef/resConvert;
double zeroPoint = vRef/2;
unsigned long StartTime = millis();
int cnt = 5;
void setup(){
Serial.begin(9600);
}
void sample () {
// Vout is read 1000 Times for precision
for(int i = 0; i < 1000; i++) {
Vout = (Vout + (resADC * analogRead(A0)));
Vout -= 2.78;
delay(1);
}
// Get Vout in mv
Vout = Vout /1000;
// Convert Vout into Current using Scale Factor
Current = (Vout - zeroPoint)/ scale_factor;
Current +=12.51;
Current -=.13;
// Print Vout and Current to two Current = ");
Serial.print("Vout = ");
Serial.print(Vout,2);
Serial.print(" milliVolts");
Serial.print("\t Current = ");
Serial.print(Current,2);
Serial.println(" milliAmps");
}
void loop() {
if (0 < cnt) {
cnt --;
sample ();
delay(1000);
}
}
If I wanted to calculated the average current from the total values as well during the five seconds. Would I have to initialize and then add towards the end?
is there any need to calculate an average after each sub-sec iteration? why not accumulate all the measurements and calculate a final average when the cnt reaches zero