Problem with button reading codes

Hi,
I have a problem with the code below for my project. The problem is, once I press one of the buttons, it runs through both of the 'if's and it always has the same outcome. Plus I did a serial print to troubleshoot, and I found out that once a button is pressed it runs through both statements two times each, and I don't know why.

Here's the code which leads to the 'if' statements (if that helps):

if (startTime == 0) {
        yn = 0;
        tone(buzz, 2000, 2000);
        delay(100);
        lcd.clear();
        lcd.setCursor(3, 0);
        lcd.print("Time's up!");
        lcd.setCursor(1, 1);
        lcd.print("Are you done??");
        timer = 0;
        text = 0;
        Serial.print("No: ");
        Serial.println(no);
        Serial.println(yes);
        while (digitalRead(sbtn) && digitalRead(nobtn)) {}
        delay(100);
        yn = 1;
        Serial.print("No: ");
        Serial.println(no);
        Serial.println(yes);
      }

I wanted to stop the loop until one of the buttons is pressed and then have a different massage due to which button I pressed from these 2. (Both of the codes are in the void loop section).

if (yn == 1) {
    Serial.print("No: ");
    Serial.println(no);
    Serial.println(yes);
    if (digitalRead(sbtn == LOW)) {
      yes = 1;
      delay(1000);
      Serial.print("No: ");
    Serial.println(no);
    Serial.println(yes);
      }
      if (digitalRead(nobtn == LOW)) {
      no = 1;
      delay(1000);
      Serial.print("No: ");
    Serial.println(no);
    Serial.println(yes);
      }
    if (yes == 1) {
      no = 0;
      Serial.print("No: ");
    Serial.println(no);
    Serial.println(yes);
          lcd.clear();
          lcd.setCursor(3, 0);
          lcd.print("Well done!");
          servo.write(25);
          delay(5000);
          lcd.clear();
          text = 0;
          yn = 0;
          yes = 0;
          }
  if (no == 1) {
    yes = 0;
    Serial.print("No: ");
    Serial.println(no);
    Serial.println(yes);
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("Get back to work");
          lcd.setCursor(6, 1);
          lcd.print("!!!!");
          servo.write(115);
          delay(5000);
          lcd.clear();
          yn = 0;
          no = 0;
          }
    }

The circuit works perfectly, before I added this part it didn't have any problem. (I'm using the arduino starter kit's LCD, servo and buttons, but these work just fine and I think the problem lies in the codes somewhere)

I would really appreciate any kind of help!
Hope everybody has a good day :slight_smile:

Please post a complete program that illustrates your problem. The cause of a problem is often in the other part - which is why it is hard to find.

...R

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.

Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

How have you got your buttons wired,

Thanks.. Tom... :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.