2

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!

3

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 say delay(2000); , only the else statement runs continuously instead of going to if statement. If I remove the delay in else, the code works perfect. Please tell me the reason. I need the compiler to stop in else statement (after execution) for 2 seconds and then go to if statement. I have used the delay in else statement but it didn't work. To achieve the same I have typed if(lastRun>0){delay(2000);} ,but it didn't work. – Sri Harsha Jun 6 '17 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 Jun 6 '17 at 12:49
  • @edgar-Thanks for the suggestion. I have edited the question. – Sri Harsha Jun 6 '17 at 13:08
  • TLDR; try else{ delay(2000); lastRun = millis(); } instead. Using the now variable after a delay, will result in using the old time. – Gerben Jun 6 '17 at 13:12
  • @Gerben-Thanks for the reply. It works perfect. – Sri Harsha Jun 6 '17 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.