Hi,
I would like to run a function only for a specified period of time.
int totalTime = 1; // in minutes
int Interval = 2; // in seconds
int counter = 0;
unsigned long currentTime = 0;
unsigned long previousTime = 0;
void setup () {
Serial.begin(9600);
Serial.println("Start");
}
void loop() {
myCount();
}
void myCount(){
currentTime = millis();
if (currentTime - previousTime > (Interval*1000)){
previousTime = currentTime;
counter = counter+1;
Serial.println(counter);
}
}
In my code totalTime sets the overall period of time for which the function should run. My first attempt was to have a variable startTime and check this against the currentTime - totalTime. My problem is that when I put the code like below startTime gets overwritten each time.
void myCount(){
currentTime = millis();
startTime = millis();
if (currentTime - previousTime > (Interval*1000) && startTime + totalTime < currentTime){
previousTime = currentTime;
counter = counter+1;
Serial.println(counter);
}
}
How would you implement this? Many thanks in advanve!