2

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();
  }
}
12
  • You're reading the (physical) button states in one location, inside loop() with button1State = digitalRead(9); .... Once you call question1() from loop(), the values of buttonState1 and buttonState2 are fixed and will not change until your code returns to loop() to re-read the physical button state into buttonState1 and buttonState2.
    – StarCat
    Commented Nov 9, 2020 at 15:23
  • I was reading the button state at the beginning of each function and that wasn't seeming to do anything. Let me retry that though, thank you
    – Snyper
    Commented Nov 9, 2020 at 15:24
  • Yeah the behavior is still the same. Since the state of the button is the condition of my if statement, perhaps that's what is holding this up?
    – Snyper
    Commented Nov 9, 2020 at 15:34
  • think about this .... how do you detect two presses of one button?
    – jsotola
    Commented Nov 9, 2020 at 15:44
  • @jsotola Yeah that's kind of what I'm trying to figure out
    – Snyper
    Commented Nov 9, 2020 at 15:45

1 Answer 1

2

There needs to be a delay() after the button state is read to allow time for it to return LOW, dumba**.

You already have enough delay() in your textN() functions, just check the button state after calling that function, not before. Code should be as follows:

void loop()
{
  question1();
}

void question1()
{ 
  if (q1 == false)
  {
    text1();
  }

  button1State = digitalRead(9);
  button2State = digitalRead(8);

  if (button1State == HIGH)
  {
    question2();
  }

  if (button2State == HIGH)
  {
    question3();
  }
}

void question2()
{ 
  if (q2 == false)
  {
    text2();
  }

  button1State = digitalRead(9);
  button2State = digitalRead(8);

  if (button1State == HIGH)
  {
    question4();
  }

  if (button2State == HIGH)
  {
    question5();
  }
}

This solves the issue completely.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.