lcd controlled by pushbuttons

I have four buttons. First button changes between the screen info on the press. two buttons pressed at once will turn off the display. second button will turn it back on and display the home log. My problem is when press two buttons at once I turn off the display but then right away i will get the home logo displayed even though i haven’t pressed second button. I’m not sure what I’m missing or why is it doing it. I’m sure it’s something really simple. thanks in advance.

#include <LiquidCrystal.h>

int buttonPin1= 18;
int buttonPin2 = 19;
LiquidCrystal lcd(9, 8, 5, 4, 3, 2);

int prevButtonState1;
int prevButtonState2;

long startTime;
long elapsedTime;
int flag, flag2;
int buttonState1;
int buttonState2;

void setup()
{
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  digitalWrite(buttonPin1, LOW);  // pulldown resistors
  digitalWrite(buttonPin2,LOW);  
  lcd.begin(20, 4);
  lcd.setCursor(2,1);
  lcd.print("Trail Tracker Co.");

}


void loop(){
  buttonState1 = digitalRead(buttonPin1);  //records current state of button1 and button2
  buttonState2 = digitalRead(buttonPin2);

  if (buttonState1 == LOW && prevButtonState1== HIGH && flag == false){  // detects transition from High to low transition
    lcd.clear();
    startTime = (millis());
    flag = true;

    lcd.setCursor(0,0);
    lcd.print("Time elapsed:");

    lcd.setCursor(0,1);
    lcd.print("Distance: ");
    lcd.setCursor(0,2);
    lcd.print("Real time:");

    prevButtonState1 = buttonState1;  // updates previous button state
  }
  else if(buttonState1 == LOW && prevButtonState1== HIGH && flag == true){

    lcd.clear();
    lcd.print("Velocity:");
    double vel = velocity(50,389.5);
    lcd.print(vel);
    lcd.print("mi/hr");
    lcd.setCursor(0,1);
    lcd.print("Altitude:");
    lcd.setCursor(0,2);
    lcd.print("Coordinates:");
    flag = false;
    prevButtonState1 = buttonState1;
  }
  else if(buttonState2 == LOW && prevButtonState2== HIGH ) {

    lcd.clear();
   

    lcd.setCursor(2,1);
    lcd.print("Trail Tracker");
   

    prevButtonState1 = buttonState1;
    prevButtonState2 = buttonState2;
  }
  else if((buttonState1 == HIGH && buttonState2 == HIGH) ){
    lcd.clear();
    lcd.setCursor(6,1);
    lcd.print("GOODBYE!");
    delay(3000);
    lcd.clear();  //display off
   


    prevButtonState1 = buttonState1;
    prevButtonState2 = buttonState2;
  }

  else{

    prevButtonState1 = buttonState1;
    prevButtonState2 = buttonState2;
  }

}
double velocity(double acc, double time){

  return acc * time/3600;


}

It seems you are not updating the button states in all cases.

Sometimes you just update one of them.

I take it that the program shuts the LCD off for 3 seconds, then it just goes back to the main message? (time elapsed, distance etc)

This bit of code seems right to me -

else if((buttonState1 == HIGH && buttonState2 == HIGH) ){
    lcd.clear();
    lcd.setCursor(6,1);
    lcd.print("GOODBYE!");
    delay(3000);
    lcd.clear();  //display off
  
    prevButtonState1 = buttonState1;
    prevButtonState2 = buttonState2;
  }

However - once you let go of the buttons (after a turn off) the button states go from HIGH to LOW which then satisfies the main if statement -

if (buttonState1 == LOW && prevButtonState1== HIGH && flag == false){  // detects transition from High to low transition
    lcd.clear();
    startTime = (millis());
    flag = true;

    lcd.setCursor(0,0);
    lcd.print("Time elapsed:");

    lcd.setCursor(0,1);
    lcd.print("Distance: ");
    lcd.setCursor(0,2);
    lcd.print("Real time:");

    prevButtonState1 = buttonState1;  // updates previous button state
  }

You would have to change this part of code so it no longer runs when you go from HIGH to LOW states, then it should stay turned off.

:)

thank you guys for you feedback and help. jim if you i make it button1 == high instead of detecting transition when i press the button the info doesn't stay on screen, it shows it and then goes to prev info displayed. Leppie i added to update each button state for each part. however that didn't help. when i press two buttons at once and want it to act as a shutdown button, so it will turn off the screen until i press button2 to turn the lcd back on. it doen't work because 'goodbye' shows up and then one of the other screens shows up.