user11772: 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.

user313: 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.


user50258: 
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:

simulate this circuit – Schematic created using CircuitLab
See example code here!

user4143: 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.

11772: <p>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.</p>

<p>I tried using <code>if</code> with a condition but it failed for my purpose. I think this is because when <code>if(...)</code> 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.</p>
 313: <p>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.</p>

<p>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 <a href="http://www.gammon.com.au/switches" rel="nofollow noreferrer">"Switches" page</a> for a thorough discussion.</p>

<p><a href="https://i.stack.imgur.com/sVu2L.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sVu2L.jpg" alt="Pullup vs. Pulldown Schematic"></a></p>
 50258: <blockquote>
  <p>I need a code to pause arduino code until a button is pressed. </p>
</blockquote>

<p>I wrote a simple <a href="https://gist.github.com/rahuldottech/839cb13140f24debae8f72343115de4b" rel="nofollow noreferrer">function</a> to do this. </p>

<h3>Usage:</h3>

<pre><code>buttonWait(&lt;pin_number&gt;);
</code></pre>

<h3>Code:</h3>

<pre><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
  // ...
}
</code></pre>

<p>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:</p>



<p><img src="https://i.stack.imgur.com/DTfwv.png" alt="schematic"></p>



<p><sup><a href="/plugins/schematics?image=http%3a%2f%2fi.stack.imgur.com%2fDTfwv.png">simulate this circuit</a> &ndash; Schematic created using <a href="https://www.circuitlab.com/" rel="nofollow">CircuitLab</a></sup></p>

<p>See example code <a href="https://gist.github.com/rahuldottech/839cb13140f24debae8f72343115de4b" rel="nofollow noreferrer">here</a>!</p>
 4143: <p>The construct you are looking for is called <code>while</code>.</p>
<p>Instead of doing &quot;If the button is pressed then continue&quot; you need to do &quot;while the button is not pressed do nothing&quot;.</p>
<p>Assuming you have an &quot;active low&quot; button (that is, a resistor pulling it up to +5V when not pressed, so <code>digitalRead(A2) == LOW</code> is &quot;pressed&quot;), you can do:</p>
<pre><code>while (digitalRead(A2) == HIGH) {
  // Do nothing
}
</code></pre>
<p>So while it's not pressed (<code>digitalRead(A2) == HIGH</code>) don't do anything.  As soon as <code>digitalRead(A2)</code> returns LOW (so it is not <code>== HIGH</code> any more) the <code>while</code> loop will finish and your sketch can continue.</p>
<p>I write all about the <code>while</code> loop in an article <a href="https://majenko.co.uk/blog/while-loop" rel="nofollow noreferrer">here</a>.</p>
