I need this code to run for 5 seconds only but I am not sure how to to do that

// 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();

void setup(){
Serial.begin(9600);

}

void loop(){

// 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 = ");

Serial.print("Vout = ");
Serial.print(Vout,2);
Serial.print(" milliVolts");
Serial.print("\t Current = ");
Serial.print(Current,2);
Serial.println(" milliAmps");

delay(1000);

}Preformatted text

Please post your code properly by highlighting your code and hitting the </> button above the posting area.

Do you want loop() to execute for 5 seconds and then just stop until you reset the Arduino? Please be clear.

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

1 Like

a naive approach

// 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);
    }
}
1 Like

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?

unclear what you're trying to do.

should Vout be initialized to zero before

do you want to average the average?

When the code stops running after 5 seconds, I want the average of the total current measured.

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

Thats what I want to do is to calculate the final average.

what am i confused about?

Maybe I am confused, so If I wanted to get the average current from the entire run what should I do?
so if i get current = .03 .02 . .01 .05 .06

How can I get the average from those numbers?

add them together and divide by the # of values

....ok

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.