1

I´m trying to make a pause in a loop, but just for the first time it loops. The next loops i would like the program to NOT do the pause. Just once time, each time a button is pressed. Does anybody knows how to do it? Thanks! This is the code i want to pause just once:

void loop() {
  if(digitalRead(switch_pin) == LOW){
    digitalWrite(led_green_pin,LOW);
    digitalWrite(led_red_pin,HIGH);
    lastMotionState    = currentMotionState;            
    currentMotionState = digitalRead(MOTION_SENSOR_PIN); //
      if (currentMotionState == LOW && lastMotionState == HIGH) { 
      servo.write(90);
      }
      else
      if (currentMotionState == HIGH && lastMotionState == LOW) { // pin state change: HIGH -> LOW
      servo.write(0);
      }
  }
3
  • 1
    Does anybody knows how to do it? is not a question that asks how to do it ... it is not a question about the arduino – jsotola Apr 22 at 17:15
  • 1
    what do you mean by pause? ... that implies that the code would somehow start up again, but you said nothing about what would stop it and what would start it again and where it should "pause" – jsotola Apr 22 at 17:17
  • Juan, stackoverflow.com/help/merging-accounts – Juraj Apr 29 at 13:54
4

The following code will pause (Give some delay) when it runs for the first time and also when you press the button.


void loop() {
  static int status = true;
  if(status){
    //Pause
    delay(15);
    status=false;
    return;
  }
  
  if(digitalRead(switch_pin) == LOW){
    digitalWrite(led_green_pin,LOW);
    digitalWrite(led_red_pin,HIGH);
    lastMotionState    = currentMotionState;            
    currentMotionState = digitalRead(MOTION_SENSOR_PIN); //
      if (currentMotionState == LOW && lastMotionState == HIGH) { 
        servo.write(90);
      }
      else if (currentMotionState == HIGH && lastMotionState == LOW) { // pin state change: HIGH -> LOW
        servo.write(0);
      }
  }
  else{
    status = true;
  }
}

Give delay according to your need.

3
  • 3
    I don't know what that continue; line does when there isn't a for/while/do-loop. – Gerben Apr 22 at 18:10
  • Better would be void loop() { static int status = true; ... } keeping the scope of status inside of loop() instead of global. – lurker Apr 22 at 19:04
  • 1
    @Gerben and lurker thanks for pointing that out. I have made the changes accordingly. Instead of continue I have added return which will work as desired. – HARSH MITTAL Apr 23 at 7:30
0

This is pretty simple, although you shouldn't see any difference, because loop() is running a lots of times in one second. But:

bool firstrun = true;
void loop() {
  if(firstrun == true){};
  else{
    if(digitalRead(switch_pin) == LOW){
      digitalWrite(led_green_pin,LOW);
      digitalWrite(led_red_pin,HIGH);
      lastMotionState    = currentMotionState;            
      currentMotionState = digitalRead(MOTION_SENSOR_PIN); //
        if (currentMotionState == LOW && lastMotionState == HIGH) { 
          servo.write(90);
        }
        else if (currentMotionState == HIGH && lastMotionState == LOW) { // pin state change: HIGH -> LOW
          servo.write(0);
        }
    }
  }
  firstrun = false;
}

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.