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");
}
}
}