0

How do I write a program to wait (delay) until one out of two buttons pressed? I have written a program to wait until a single button is pressed. It's working fine. However, if I extend it to second button it's not working. Pls help on this.

  1. while (digitalRead(bt_Select) == HIGH) {} -- working.
  2. while (digitalRead(bt_Select) == HIGH || digitalRead(bt_Reject) == HIGH) {} -- Not working.

Complete code:

const int bt_Select = 2;
const int bt_Reject = 3;

void setup() {
  Serial.begin(9600);  
  pinMode(bt_Select, INPUT_PULLUP);
  pinMode(bt_Reject, INPUT_PULLUP);
}

void loop() {
  Serial.println("Pl input:");

  while (digitalRead(bt_Select) == HIGH {} // This is the line I change
      
  if (digitalRead(bt_Select) == LOW) {
    Serial.println("Select Button pressed:");
    delay(250);
    Serial.println("");
  }

  if (digitalRead(bt_Reject) == LOW) {
    Serial.println("Reject Button pressed:");
    delay(250);
    Serial.println("");
  }
}
0

while (digitalRead(bt_Select)==HIGH||digitalRead(bt_Reject)==HIGH){}

should be

while (digitalRead(bt_Select)==HIGH&&digitalRead(bt_Reject)==HIGH){}

The first loop would break only if you press both buttons at once. I believe you are trying to break if either button is pressed.

4
  • yes I try to break if either button is pressed. – Ak Rikas Mar 3 at 15:36
  • Thank you it's working.. 5n – Ak Rikas Mar 3 at 15:37
  • however i confused how it could works?!* – Ak Rikas Mar 3 at 15:39
  • @AkRikas Your code stays in the while loop when one of the buttons is HIGH and the other code when both are HIGH. Does this answer your question? – Python Schlange Mar 3 at 17:43

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.