Problems with making a question on LCD

First off, I am extremely new to arduino programming. In fact, I do not even own an Arduino currently. (Its OTW)

Currently only using tinkercad’s web simulator to get used to programming the Arduino.

Thus, I might ask some obvious/stupid questions. I hope you can forgive me for that, haha.

My goal for this project is to create a system that do a “test” every 20 seconds. But you can also bypass the waiting time of 20 seconds, if you wanted to.

Thus, I created a feature so that when button 1 is pressed, a question is asked.

In this case the question is “Manual Testing?”.

To which, the user will use button 1 for Yes or button 2 for No.

I have 2 problems.

The first is for when button 2 is pressed. What I wanted when button 2 is pressed was to resume the “main display”. Which is counting down from 20 seconds. But I have no idea how to resume back to that part… I tried break; and return;, but they just gave me errors.

The second problem is that button 1 is not registering, hence the question is not displaying on the LCD, for the life of me I just can’t figure out why.

If anyone can guide me on how to solve those problems, that would be greatly appreciated!!

My sketch:

#include <LiquidCrystal.h>

LiquidCrystal lcd(3, 4, 5, 6, 7, 8);

const int buttonPin = 2;
const int button2Pin = 9;

bool optionMode = false;
bool systemBegin = false;

unsigned long debounceDelay = 50;
unsigned long lastPress = 0;

int buttonState = LOW;
int button2State = LOW;
int lastbuttonState = LOW;
int lastbutton2State = LOW;

void setup()
{
  pinMode(buttonPin, INPUT);
  pinMode(button2Pin, INPUT);
  lcd.begin(16, 2);
  Serial.begin(9600);
}

void loop()
{
 buttonState = digitalRead(buttonPin);
 button2State = digitalRead(button2Pin);
  
  if (systemBegin == false)
  {
   lcd.print("SYSTEM STARTED");
   delay(2000);
   lcd.clear();
   systemBegin = true;
  }
  
  if ((millis() - lastPress) > debounceDelay)
  {
   if (buttonState == HIGH && optionMode == false)
   {
     Serial.println("Option Mode is True");
     lcd.clear();
     lcd.setCursor(0,0);
     lcd.print("Manual Testing?");
     lcd.setCursor(0,1);
     lcd.print("Yes  |  No");
     optionMode = true;
     lastPress = millis();
     
     if (buttonState == HIGH && optionMode == true)
     {
       testingPrint();
       delay(5000);
       lcd.clear();
       lcd.print("Completed!");
       delay(2000);
       lcd.clear();
       optionMode = false;
       lastPress = millis();
     }
     else if (button2State == HIGH && optionMode == true)
     {
       lcd.clear();
       lcd.print("Returning to");
       lcd.setCursor(0,1);
       lcd.print("countdown");
       delay(5000);
       optionMode = false;
       lastPress = millis();
     }   
   }
  }
  
  while (optionMode == false)
  {
    lcd.clear();
  for (int i = 20; i >= 0; i--)
  {
    lcd.setCursor(0,0);
    lcd.print("Time remaining =");
    lcd.setCursor(0,1);
    lcd.print(i); 
    delay(1000);
    if ( i == 10)
    {
     lcd.setCursor(1,1);
     lcd.print(" ");
    }
    
    if (i == 0)
    {
      testingPrint();
      delay(5000);
      lcd.clear();
      i = 20;
    }
  }
  }
}
              
void testingPrint()
{
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("Testing in ");
 lcd.setCursor(0,1);
 lcd.print("progress");
}

My circuit:

You can't use buttonstate as an argument in the IF statement. As soon as you release the button the conditions are no longer met. Therefor, the IF statement within the first IF will never excecute. You would be better off letting that first buttonpress setting a boolean high and use that as a condition for your first IF statement. Set the boolean to false again when the execution you want is done.

i.e.
IF (buttonstate == high){
bQuestion = true;
}
IF (bQuestion == true){
"do what you want here)
}

I'm on my cell at the moment, but if you want I can rewrite it for you later tonight.

idavidwan:
The second problem is that button 1 is not registering, hence the question is not displaying on the LCD, for the life of me I just can't figure out why.

Your code starts with optionMode == false. Once you enter loop() the first time, it will race through your statements (shortform below). As 50 ms (debounceDelay) has not lapsed yet, you will end up at the while statement and your program will hang there forever as there is nothing that sets optionMode to true.

void loop()
{
  buttonState = digitalRead(buttonPin);
  button2State = digitalRead(button2Pin);

  if (systemBegin == false)
  {
    ...
    ...
  }

  if ((millis() - lastPress) > debounceDelay)
  {
    ...
    ...
  }

  while (optionMode == false)
  {
    ...
    ...
  }
}