user81643: This code  compiles without problem, but doesn't work at all... The switch does not set the LED low and the while loop doesn't work, plus the serial print doesn't print....
Until I put the while loop in pressing either button would change the LED state. I wanted to make the sequence "button one, LED change, THEN button two"
I'm certain to have missed something...
const int buttonPin1 = 2;     // the number of the pushbutton 1 pin 2
const int buttonPin2 = 7;     // the number of the pushbutton 2 pin 7
const int ledPin = 13;      // the number of the LED pin

// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;
int i = 0;

void setup() {
// initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin 1 as an input:
  pinMode(buttonPin1, INPUT);
// initialize the pushbutton pin 2 as an input:
  pinMode(buttonPin2, INPUT);
  Serial.begin(9600);
}

void loop() {

  digitalWrite(ledPin, HIGH);

// read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1);

// check whether a  pushbutton is pressed.
// if it is, the buttonState is HIGH:

  if (buttonState1 == HIGH) {
// turn LED off:   // 
    digitalWrite(ledPin, LOW);
    while (1) {
      (buttonState1 == LOW);
      if (buttonState1 == LOW) {
        Serial.println("1");
      }
      {
        break;
      }
    }

    delay[1000];

    digitalWrite(ledPin, HIGH);

// read the state of the pushbutton 2 value:
    buttonState2 = digitalRead(buttonPin2);
    if (buttonState2 == HIGH) {
// turn LED off:    
      while (1) {
        if (buttonState2 == LOW) {
          break;
        }
      }
      digitalWrite(ledPin, LOW);

      delay(10000);
      digitalWrite(ledPin, HIGH);

    }
  }
}




user62523: I think there’s a misunderstanding of how variables work.
Your code is only reading the real state of buttonPin1 once (before the first if () statement) and copies the value read from the I/O port as it is at that moment to the variable buttonState1, by executing buttonState1 = digitalRead(buttonPin1);.
After you’ve copied the value read from the buttonPin1 port to the buttonState1 variable, this variable will no longer change or reflect the actual button status. I.e. the value of the variable buttonState1 will not automagically change when you push or release button 1. buttonState1 is a normal program variable and not coupled to the I/O port.
If you want to continue to check the current button status, you have to continuously read the I/O port, by using digitalRead() statements where appropriate.
The same goes for buttonPin2 and buttonState2.

81643: <p>This code  compiles without problem, but doesn't work at all... The switch does not set the LED low and the while loop doesn't work, plus the serial print doesn't print....
Until I put the while loop in pressing either button would change the LED state. I wanted to make the sequence &quot;button one, LED change, THEN button two&quot;
I'm certain to have missed something...</p>
<pre><code>const int buttonPin1 = 2;     // the number of the pushbutton 1 pin 2
const int buttonPin2 = 7;     // the number of the pushbutton 2 pin 7
const int ledPin = 13;      // the number of the LED pin

// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;
int i = 0;

void setup() {
// initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin 1 as an input:
  pinMode(buttonPin1, INPUT);
// initialize the pushbutton pin 2 as an input:
  pinMode(buttonPin2, INPUT);
  Serial.begin(9600);
}

void loop() {

  digitalWrite(ledPin, HIGH);

// read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1);

// check whether a  pushbutton is pressed.
// if it is, the buttonState is HIGH:

  if (buttonState1 == HIGH) {
// turn LED off:   // 
    digitalWrite(ledPin, LOW);
    while (1) {
      (buttonState1 == LOW);
      if (buttonState1 == LOW) {
        Serial.println(&quot;1&quot;);
      }
      {
        break;
      }
    }

    delay[1000];

    digitalWrite(ledPin, HIGH);

// read the state of the pushbutton 2 value:
    buttonState2 = digitalRead(buttonPin2);
    if (buttonState2 == HIGH) {
// turn LED off:    
      while (1) {
        if (buttonState2 == LOW) {
          break;
        }
      }
      digitalWrite(ledPin, LOW);

      delay(10000);
      digitalWrite(ledPin, HIGH);

    }
  }
}

</code></pre>
<p><a href="https://i.stack.imgur.com/MddDZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MddDZ.png" alt="enter image description here" /></a></p>
 62523: <p>I think there’s a misunderstanding of how variables work.</p>
<p>Your code is only reading the real state of <em>buttonPin1</em> once (before the first <code>if ()</code> statement) and copies the value read from the I/O port <em>as it is at that moment</em> to the variable <em>buttonState1</em>, by executing <code>buttonState1 = digitalRead(buttonPin1);</code>.</p>
<p>After you’ve copied the value read from the <em>buttonPin1</em> port to the <em>buttonState1</em> variable, this variable will no longer change or reflect the actual button status. I.e. the value of the variable <em>buttonState1</em> will not automagically change when you push or release button 1. <em>buttonState1</em> is a normal program variable and not coupled to the I/O port.</p>
<p>If you want to continue to check the current button status, you have to continuously read the I/O port, by using <code>digitalRead()</code> statements where appropriate.</p>
<p>The same goes for <em>buttonPin2</em> and <em>buttonState2</em>.</p>
