3 button Menu Program.....will not update sensor input

I've been trying to adapt a menu system with my project and the first menu "menu1" contains sensor readings 1,2 and 3.
These sensor readings need to update every time the loop repeats. The code works great until I insert the sensor code into the menu function, when I push the enter button for "menu1" the sensor readings update but do not continue to update. The only way I can get them to update is by pushing the enter button every second. I assume this is because the sensor code is being called only when the "Enterbutton" is pressed bringing this input HIGH for a split second and the loop only updates when the "Enterbutton" is pressed. Is there a way to get this sensor code to loop in the "menu" function. Please have a look at my code below.

void loop()
      {
         
         Input = analogRead(A0);
         myPID.Compute();
        /************************************************
         * turn the output pin on/off based on pid output
         ************************************************/
        if(millis() - windowStartTime>WindowSize)
        { //time to shift the Relay Window
        windowStartTime += WindowSize;}
        if(Output < millis() - windowStartTime) digitalWrite(RelayPin,HIGH);
        else digitalWrite(RelayPin,LOW);
        
             
         BtnDownState = digitalRead(BtnDownPin);
         BtnEnterState = digitalRead(BtnEnterPin);
           
          //THE DOWN BUTTON
        if (BtnDownState != BtnDownLastState) 
           {
             if (BtnDownState == HIGH && newState == 0) 
               {
           
                  if (BtnDownCounter < 4) 
                    {BtnDownCounter++;}
                      
                      else 
                        {BtnDownCounter = 1;}
                  
             
                  if (BtnDownCounter == 1) {         //Menu 1
                    lcd.clear();
                    lcd.setCursor(0, 0);
                    lcd.print(">MENU1");
                    lcd.setCursor(0, 1);
                    lcd.print("menu2");
                  } else if (BtnDownCounter == 2) {  //Menu 2
                    lcd.clear();
                    lcd.setCursor(0, 0);
                    lcd.print(">MENU2");
                    lcd.setCursor(0, 1);
                    lcd.print("menu3");
                  } else if (BtnDownCounter == 3) {  //Menu 3
                    lcd.clear();
                    lcd.setCursor(0, 0);
                    lcd.print(">MENU3");
                    lcd.setCursor(0, 1);
                    lcd.print("menu4");
                  } else if (BtnDownCounter == 4) {  //Menu 4
                    lcd.clear();
                    lcd.setCursor(0, 0);
                    lcd.print(">MENU4");
                    lcd.setCursor(0, 1);
                    lcd.print("menu1");
                  }
           
              }
           
          }
                     
            BtnDownLastState = BtnDownState;
           
            //THE ENTER BUTTON
            if (BtnEnterState == HIGH) 
          {
              if (BtnDownCounter == 1) 
                 {
                  lcd.clear();
                  lcd.setCursor(0, 0); // set the cursor to column 0, line 0
                  lcd.print("PPO1");  // Print a message to the LCD.
    
                  lcd.setCursor(6, 0); // set the cursor to column 6, line 0
                  lcd.print("PPO2"); // Print a message to the LCD
    
                  lcd.setCursor(12, 0); // set the cursor to column 12, line 0
                  lcd.print("PPO3");  // Print a message to the LCD.
                
                  lcd.setCursor(0, 1); //sets the cursor to column 0, line 1    
                  inputReading1 = analogRead(sensor1); //getting the voltage reading from the 02 sensor    
                  float voltage1 = inputReading1 * aref_voltage; // converting that reading to voltage 
                  voltage1 /= 1024.0;    
                  lcd.print(voltage1, 2);//print actual voltage to lcd    
                  delay(200);
            
        
                  lcd.setCursor(6, 1); //sets the cursor to column 6, line 1     
                  inputReading2 = analogRead(sensor2); //getting the voltage reading from the temperature sensor    
                  float voltage2 = inputReading2 * aref_voltage; // converting that reading to voltage
                  voltage2 /= 1024.0; 
                  lcd.print(voltage2, 2);  //print actual voltage to lcd
                  delay(200);     
          
          
                  lcd.setCursor(12, 1); //sets the cursor to column 12, line 2    
                  inputReading3 = analogRead(sensor3); //getting the voltage reading from the temperature sensor    
                  float voltage3 = inputReading3 * aref_voltage; // converting that reading to voltage 
                  voltage3 /= 1024.0; 
                  lcd.print(voltage3, 2);  //print actual voltage to lcd
                  delay(200);
                  
                  if(DateTime.available()) {
                  lcd.setCursor(3, 1);
                  unsigned long prevtime = DateTime.now();
                  while( prevtime == DateTime.now() ); // wait for the second to rollover

                  DateTime.available(); //refresh the Date and time properties
                  digitalClockDisplay( ); // update digital clock
                  }
                 
                }  
               else if (BtnDownCounter == 2) 
                {
                  lcd.clear();
                  lcd.setCursor(0, 0); // set the cursor to column 0, line 0
                  lcd.print("PSI");  // Print a message to the LCD.
    
                  lcd.setCursor(5, 0); // set the cursor to column 6, line 0
                  lcd.print("TEMP"); // Print a message to the LCD
    
                  lcd.setCursor(11, 0); // set the cursor to column 12, line 0
                  lcd.print("DEPTH");  // Print a message to the LCD.
                
                }
               else if (BtnDownCounter == 3)  
                {
                  lcd.clear();
                  lcd.println("You have entered");
                  lcd.setCursor(0, 1);
                  lcd.print(" at menu 3");
                } 
               else if (BtnDownCounter == 4)
                {
                  lcd.clear();
                  lcd.println("You have entered");
                  lcd.setCursor(0, 1);
                  lcd.print(" at menu 4");
                }
           
           }
           
          
       }

Have you debounced the button?

IngHooch:
Have you debounced the button?

I have not tried that on this one, but have tried it on a similar program and get the same result. If I understand this correctly the debounce provides a time that button must be pressed for, in order to complete the action? If I'm wrong please correct me.

The debounce is to prevent the button from change state too fast e.g. you push the button then release it, it will bounce for a while so the buttonState variable in your Arduino code will change from TRUE to FALSE randomly. You need to prevent this. Try this an let us know :slight_smile:

IngHooch:
Have you debounced the button?

Why do you ask? how will this help?

IngHooch:
The debounce is to prevent the button from change state too fast e.g. you push the button then release it, it will bounce for a while so the buttonState variable in your Arduino code will change from TRUE to FALSE randomly. You need to prevent this. Try this an let us know :slight_smile:

I'm not quite sure this will solve my issue, could you please elaborate. The button selects menu 1 fine without any bouncing or changing of state. It's when I select "menu1" (where the sensor reading are displayed) I would like the sensors to update and loop continuously in "menu1". This only happens now when the "Enter" button is pressed for that one second.

concretefreak:

IngHooch:
The debounce is to prevent the button from change state too fast e.g. you push the button then release it, it will bounce for a while so the buttonState variable in your Arduino code will change from TRUE to FALSE randomly. You need to prevent this. Try this an let us know :slight_smile:

I'm not quite sure this will solve my issue, could you please elaborate. The button selects menu 1 fine without any bouncing or changing of state. It's when I select "menu1" (where the sensor reading are displayed) I would like the sensors to update and loop continuously in "menu1". This only happens now when the "Enter" button is pressed for that one second.

I did look at the example you provided, this may just work!!!!
Thank you for your input.....I'll you know it goes....

This is what I have for the debounce.....

 void  readButtons(){  //read buttons status
  int reading;
  int buttonEnterState=LOW;             // the current reading from the Enter input pin
  int buttonEscState=LOW;             // the current reading from the input pin
  int buttonLeftState=LOW;             // the current reading from the input pin
  int buttonRightState=LOW;          // the current reading from the input pin


  //Enter button
                  // read the state of the switch into a local variable:
                  reading = digitalRead(buttonPinEnter);

                  // check to see if you just pressed the enter button 
                  // (i.e. the input went from LOW to HIGH),  and you've waited 
                  // long enough since the last press to ignore any noise:  
                
                  // If the switch changed, due to noise or pressing:
                  if (reading != lastButtonEnterState) {
                    // reset the debouncing timer
                    lastEnterDebounceTime = millis();
                  } 
                  
                  if ((millis() - lastEnterDebounceTime) > debounceDelay) {
                    // whatever the reading is at, it's been there for longer
                    // than the debounce delay, so take it as the actual current state:
                    buttonEnterState=reading;
                    lastEnterDebounceTime=millis();
                  }
                  
                  // save the reading.  Next time through the loop,
                  // it'll be the lastButtonState:
                  lastButtonEnterState = reading;
                  

    //Esc button               
                  // read the state of the switch into a local variable:
                  reading = digitalRead(buttonPinEsc);

                  // check to see if you just pressed the Down button 
                  // (i.e. the input went from LOW to HIGH),  and you've waited 
                  // long enough since the last press to ignore any noise:  
                
                  // If the switch changed, due to noise or pressing:
                  if (reading != lastButtonEscState) {
                    // reset the debouncing timer
                    lastEscDebounceTime = millis();
                  } 
                  
                  if ((millis() - lastEscDebounceTime) > debounceDelay) {
                    // whatever the reading is at, it's been there for longer
                    // than the debounce delay, so take it as the actual current state:
                    buttonEscState = reading;
                    lastEscDebounceTime=millis();
                    
                  }
                  
                  // save the reading.  Next time through the loop,
                  // it'll be the lastButtonState:
                  lastButtonEscState = reading; 
                  
                     
   //Down button               
                  // read the state of the switch into a local variable:
                  reading = digitalRead(buttonPinRight);

                  // check to see if you just pressed the Down button 
                  // (i.e. the input went from LOW to HIGH),  and you've waited 
                  // long enough since the last press to ignore any noise:  
                
                  // If the switch changed, due to noise or pressing:
                  if (reading != lastButtonRightState) {
                    // reset the debouncing timer
                    lastRightDebounceTime = millis();
                  } 
                  
                  if ((millis() - lastRightDebounceTime) > debounceDelay) {
                    // whatever the reading is at, it's been there for longer
                    // than the debounce delay, so take it as the actual current state:
                    buttonRightState = reading;
                   lastRightDebounceTime =millis();
                  }
                  
                  // save the reading.  Next time through the loop,
                  // it'll be the lastButtonState:
                  lastButtonRightState = reading;                  
                  
                  
    //Up button               
                  // read the state of the switch into a local variable:
                  reading = digitalRead(buttonPinLeft);

                  // check to see if you just pressed the Down button 
                  // (i.e. the input went from LOW to HIGH),  and you've waited 
                  // long enough since the last press to ignore any noise:  
                
                  // If the switch changed, due to noise or pressing:
                  if (reading != lastButtonLeftState) {
                    // reset the debouncing timer
                    lastLeftDebounceTime = millis();
                  } 
                  
                  if ((millis() - lastLeftDebounceTime) > debounceDelay) {
                    // whatever the reading is at, it's been there for longer
                    // than the debounce delay, so take it as the actual current state:
                    buttonLeftState = reading;
                    lastLeftDebounceTime=millis();;
                  }
                  
                  // save the reading.  Next time through the loop,
                  // it'll be the lastButtonState:
                  lastButtonLeftState = reading;  

                  //records which button has been pressed
                  if (buttonEnterState==HIGH){
                    lastButtonPushed=buttonPinEnter;

                  }else if(buttonEscState==HIGH){
                    lastButtonPushed=buttonPinEsc;

                  }else if(buttonRightState==HIGH){
                    lastButtonPushed=buttonPinRight;

                  }else if(buttonLeftState==HIGH){
                    lastButtonPushed=buttonPinLeft;

                  }else{
                    lastButtonPushed=0;
                  }                  
}
int lastButtonPushed = 0;

int lastButtonEnterState = LOW;   // the previous reading from the Enter input pin
int lastButtonEscState = LOW;   // the previous reading from the Esc input pin
int lastButtonLeftState = LOW;   // the previous reading from the Left input pin
int lastButtonRightState = LOW;   // the previous reading from the Right input pin


long lastEnterDebounceTime = 0;  // the last time the output pin was toggled
long lastEscDebounceTime = 0;  // the last time the output pin was toggled
long lastLeftDebounceTime = 0;  // the last time the output pin was toggled
long lastRightDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 500;    // the debounce time