Using the same buttons for menu/access functions without delays

Hi Forum!

Please see attached photo of what I am trying to accomplish.

I want to use 3 buttons to control different modes (main menu, brightness and contrast functions for LCD)

Ideally i want to use the following commands:

  • Button 2 and button 3 pressed simultaneously, to enter menu
  • Then button 1 to go from brightness to contrast menus and vice versa.
  • Then to exit use buttons 2 and 3 again, just like entering the menu, from either brightness or contrast menus

The problem with my generic “if statements” is that unless I use a delay, the LCD never moves from main menu, since pressing the button to enter is the same as to exit.

Similarly between the two menus (brightness and contrast), pressing button 1 will depend on how long the button is pressed for and randomising if it switches menus or stays on the same one.

I was thinking of finding a way to recognise the release of the buttons before they can be used for a different function, but I am unable to come up with a way to implement this.

Any suggestions welcome!
Thank you!

I wonder if you can use a pulse in to time how long the button is held down. There are other ways to do this of course. Such as current time = millis... etc.

OP diagram:

SOLVED!

Couldn't get PulseIn() to work although i think it is possible.

Instead I used empty while loops that waited until buttons were released:

void loop() {
while(mode == 0){
lcd.setCursor(0,0);
lcd.print("Hello");
if(digitalRead(button1Pin) == LOW && digitalRead(button2Pin) == HIGH && digitalRead(button3Pin) == HIGH){
while(digitalRead(button1Pin) == LOW && digitalRead(button2Pin) == HIGH || digitalRead(button3Pin) == HIGH){}
lcd.clear();
mode = 1;
}
}

while(mode == 1){
brightnessFn();
if(digitalRead(button1Pin) == LOW && digitalRead(button2Pin) == HIGH && digitalRead(button3Pin) == HIGH){
while(digitalRead(button1Pin) == LOW && digitalRead(button2Pin) == HIGH || digitalRead(button3Pin) == HIGH){}
lcd.clear();
mode = 0;
}

}