I'm writing a choose-your-own-adventure style project for a class project. My Arduino Uno is set up with a 16-digit LCD display and two pushbuttons with pulldown resistors. I'm wanting each function to display text on the LCD and then take two user inputs, Yes or No, from the pushbuttons, and then call another function respective of which input is pressed. Each successive function will also take the two inputs to then call the next function in the series, and so on.
My issue is that after the first time a pushbutton is pressed, its state does not change. When question1()
is called, and the user presses button1
, question2
is called. However, the function runs and never waits for user input, and question4
is called right away because the state of button1
seemingly stays HIGH
after the first time it is pressed.
What am I missing? Is there a better way to be gathering the user input? My pushbuttons are properly wired with the resistor pulling down to ground so my issue must be with my code.
Thanks in advance for any help.
void loop()
{
question1();
}
void question1()
{
button1State = digitalRead(9);
button2State = digitalRead(8);
if (q1 == false)
{
text1();
}
if (button1State == HIGH)
{
question2();
}
if (button2State == HIGH)
{
question3();
}
}
void question2()
{
button1State = digitalRead(9);
button2State = digitalRead(8);
if (q2 == false)
{
text2();
}
if (button1State == HIGH)
{
question4();
}
if (button2State == HIGH)
{
question5();
}
}
loop()
withbutton1State = digitalRead(9); ...
. Once you callquestion1()
fromloop()
, the values ofbuttonState1
andbuttonState2
are fixed and will not change until your code returns toloop()
to re-read the physical button state intobuttonState1
andbuttonState2
.if
statement, perhaps that's what is holding this up?