I have a variable timeInSec
which records the time as loop begins. It is defined as timeInSec = millis();
. I have two conditions. If (timeInSec - lastRun)< 5
(that is 5 sec) , the first condition is executed(the if statement).Otherwise if (timeInSec - lastRun)>5
the second condition is executed(else statement). Here is the code:
void loop(){
timeInSec = millis(); // Records time in seconds
// if(lastRun > 0) { // if i run this, only else statement is executed
// delay(10000);
// }
if((timeInSec - lastRun) < 5000){ // Treshold time (if condition which runs when timeInSec is less than 5 seconds)
// Some code
}
else{ // If treshold time exceeds (executes when timeInSec is > 5seconds)
delay(10000); // if i run this, only else statement is executed
lastRun = timeInSec;
}
}
Here the else
statement executes once and control goes back if
statement again. I want to pause the control at else
statement for 10 seconds and then go back to if
statement for further execution. Everytime the control should pause at else
statement for 10 seconds. I have used delay
in the else
statement and in this case only else
statement is being executed. The same happens if I use delay
at the start of loop (as in code). I have tried without using delay
but didn't succeed.
Thanks for the help!
1 Answer 1
Do not try to change millis()
. Instead, record the last time the
second code (else statement) ran, and check whether that was more than
five seconds ago:
void loop() {
static uint32_t lastRun;
uint32_t now = millis();
if (now - lastRun < 5000) {
// Some code
}
else {
// Some code
lastRun = now; // record the time
}
}
This is essentially the same technique used in the Blink without delay Arduino tutorial.
-
Thanks for the reply. The code works perfect. I have a small doubt here. If I keep a delay at the start of
else
code saydelay(2000);
, only theelse
statement runs continuously instead of going toif
statement. If I remove thedelay
inelse
, the code works perfect. Please tell me the reason. I need the compiler to stop inelse
statement (after execution) for 2 seconds and then go toif
statement. I have used the delay inelse
statement but it didn't work. To achieve the same I have typedif(lastRun>0){delay(2000);}
,but it didn't work.Sri Harsha– Sri Harsha2017年06月06日 12:09:00 +00:00Commented Jun 6, 2017 at 12:09 -
@SriHarsha: Do not use the comments to ask new questions. You can either write a new question or, if you believe this really belongs to the same question, edit that question.Edgar Bonet– Edgar Bonet2017年06月06日 12:49:05 +00:00Commented Jun 6, 2017 at 12:49
-
@edgar-Thanks for the suggestion. I have edited the question.Sri Harsha– Sri Harsha2017年06月06日 13:08:10 +00:00Commented Jun 6, 2017 at 13:08
-
TLDR; try
else{ delay(2000); lastRun = millis(); }
instead. Using thenow
variable after a delay, will result in using the old time.Gerben– Gerben2017年06月06日 13:12:43 +00:00Commented Jun 6, 2017 at 13:12 -
@Gerben-Thanks for the reply. It works perfect.Sri Harsha– Sri Harsha2017年06月06日 13:23:48 +00:00Commented Jun 6, 2017 at 13:23
Explore related questions
See similar questions with these tags.