1

I need some code to pause the Arduino code until a button is pressed. I have a long program and at point I need a condition that if a Button on pin A2 is pressed then go forward, otherwise wait.

I tried using if with a condition but it failed for my purpose. I think this is because when if(...) detects that the button is not pressed, it moves to next lines (as usual). But I have a long program and I want to wait forever until the button is pressed.

4
  • Is it active high or active low (are you using pullup or pulldown resistors)?
    – Majenko
    Commented Sep 5, 2015 at 11:29
  • See my edit please. I am just using a Pushbutton with a resistor. I tested the button with different programs. It all works fine. Commented Sep 5, 2015 at 11:31
  • You haven't answered my question though. Is the resistor connected to +5V or to GND? It makes a big difference to the code snippet I will spoon feed you.
    – Majenko
    Commented Sep 5, 2015 at 11:40
  • The resistor is connected to Ground. Help me Commented Sep 5, 2015 at 13:17

4 Answers 4

3

The construct you are looking for is called while.

Instead of doing "If the button is pressed then continue" you need to do "while the button is not pressed do nothing".

Assuming you have an "active low" button (that is, a resistor pulling it up to +5V when not pressed, so digitalRead(A2) == LOW is "pressed"), you can do:

while (digitalRead(A2) == HIGH) {
  // Do nothing
}

So while it's not pressed (digitalRead(A2) == HIGH) don't do anything. As soon as digitalRead(A2) returns LOW (so it is not == HIGH any more) the while loop will finish and your sketch can continue.

I write all about the while loop in an article here.

1
  • will it continue right from where it has stopped..? Lets say if my motor is at 67 degrees it should resume from the same. Is it doing the same..?
    – Sonali_B
    Commented Nov 8, 2017 at 7:42
2

I need a code to pause arduino code until a button is pressed.

I wrote a simple function to do this.

Usage:

buttonWait(<pin_number>);

Code:

/* the function */
void buttonWait(int buttonPin){
  int buttonState = 0;
  while(1){
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
      return;
    }
  }
}


void loop() {
  buttonWait(2); // wait for button press on pin 2
  // do something
  // ...
}

This code assumes that you have a pulldown resistor, but you can easily modify it to work with a pullup resistor instead, if you feel the need:

schematic

simulate this circuit – Schematic created using CircuitLab

See example code here!

3
  • Thank you rahuldottech!! I had been fussing with my code for several months on and off and tonight...I hit gold!!
    – Osprey
    Commented Jun 23, 2019 at 21:31
  • @Osprey That's great to hear! :)
    – undo
    Commented Jun 24, 2019 at 4:19
  • The battery doesn't make sense in that circuit. you have it wired to use +5V from VCC, which is correct, but why have a battery hooked to +5V? (Unless you are planning to bypass the internal regulator and the battery is really a regulated 5V supply?)
    – Duncan C
    Commented Dec 24, 2020 at 0:12
1

Here are two ways you might have wired the button. If you wired it with a pullup, you will need to test for LOW (the button will ground its output when pressed). If pulldown, the button will raise its output when pressed and you would test for HIGH.

If you will be testing the button again within less than a second, don't forget to debounce it. Switches send a bunch (technical term :) of pulses for a significant fraction of a second during a make or bread. See Nick Gammon's "Switches" page for a thorough discussion.

Pullup vs. Pulldown Schematic

-1

Pausing a program can be accomplished using Light Sleep mode as demonstrated in given URL: https://www.youtube.com/watch?v=GIbSt5mJ5VY

1
  • 2
    Welcome to Arduino! Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
    – Juraj
    Commented Nov 22, 2022 at 7:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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