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