LCD Shield screen toggling doesn't follow commands

My code is simple; I'm not sure why my lcd loops through all the screens I have setup as toggle options. It should display each screen individually based upon what button is pressed. (right or left) Can anyone see my problem and help me fix it? It may be obvious, so I apologize for my newbieness.. I removed most of the code due to the limited amount of characters aloud in a post, but if requested, i can post the whole thing. My problem should be in the following code:

void setup()
{ 
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0, 0);
 
dht.begin();
sensors.begin();  
}

int sensorReadDelay=1000;
int lastSensorRead=0; 
float h;
float t;
 float insideTempC;
float outsideTempC;
float waterTankTempC;
 
float temp;
float wata;
float owside; 

void loop() {
 currentButton = keypad.getKey();//Read Button Press
 if(currentButton > 0){
   checkButtons();
 }
 
 if(millis()-lastSensorRead>sensorReadDelay){ //millis() gets the time
   /*Everthing inbetween these curly braces should only be run every 1 second*/
   
   readSensors();
   
   /*These two pieces of code can also go in here
   so the won't have to be run as many times*/
   
   if(temp >= setTemp) //Trip relay if temp is exceeded for DHT22 SENSOR
   {
     digitalWrite(CHANNEL1, LOW); 
   } else {
     digitalWrite(CHANNEL1, HIGH);
   }
   
   if (wata >= waterSetTemp) { 
     digitalWrite(CHANNEL3, HIGH);
     //digitalWrite(CHANNEL2, HIGH);
   } else {
     digitalWrite(CHANNEL3, LOW);
   }
   
   /*Save off the time the sensors were read so we don't read them again until 1 second is past*/
   lastSensorRead=millis();
 }
float h = dht.readHumidity();
float t = dht.readTemperature();
 
float insideTempC = sensors.getTempC(inside); 
float outsideTempC = sensors.getTempC(outside);
float waterTankTempC = sensors.getTempC(waterTank);
 
float temp = (t * 1.8) + 32; //Convert *C to *F. 
float wata = (insideTempC * 1.8) + 32; //Convert *C to *F.
float owside = (outsideTempC * 1.8) + 32; //Convert *C to *F.

if (displayTemp == 1) //Shows DHT22 SENSOR temp screen upon first boot
{
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("ROOM       ");
    lcd.print(int(temp));
    lcd.print("");
    lcd.print((char)223); // degree symbol
    lcd.print("F");
    lcd.setCursor(0,1);
    lcd.print("SET TEMP   ");
    lcd.print(setTemp);
    lcd.print("");
    lcd.print((char)223);
    lcd.print("F");
    currentDisplay = 0; 
}

if (displayHumid == 1)// DHT22 SENSOR
{
  lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("HUMIDITY    ");
    lcd.print(int(h));
    lcd.print(" %");
    lcd.setCursor(0,1);
    lcd.print("SET HUMID   ");
    lcd.print(setHumid);
    lcd.print(" %");
    
    currentDisplay = 1;
}    

if (waterDisplay == 1)
{
    sensors.requestTemperatures(); //This command must be there in order to get live stream of temperature data
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Water    ");
    printTemperature(inside);
    lcd.print("");
    lcd.print((char)223); // degree symbol
    //lcd.print("F");
    lcd.setCursor(0,1);
    lcd.print("SET TEMP   ");
    lcd.print(waterSetTemp);
    lcd.print("");
    lcd.print((char)223);
    //lcd.print("F");
    currentDisplay = 2;
}

if ( sensorsDisplay == 1)
{
     sensors.requestTemperatures();
     lcd.clear();
     lcd.setCursor(0,0);
     lcd.print(" ");
     printTemperature(inside);
     lcd.print("");
     lcd.print((char)223); // degree symbol
     lcd.print("F");
     lcd.setCursor(0,1);
     lcd.print(" ");
     printTemperature(outside);
     lcd.print("");
     lcd.print((char)223); // degree symbol
     lcd.print("F");
     currentDisplay = 3;
     
}
} // LEAVE THIS ONE HERE FOR LOOP COMPLETION

void checkButtons(){
//MENU SYSTEM
// Reads which buttons are pressed. 
// No button = 0
// Select button = 1
// Left button = 2
// Up Button = 3
// Down Button = 4
// Right Button = 5

if (currentButton == 5 && currentDisplay == 0)//Turns display temp off so it will show humidity after pressing right button
{
   displayHumid = 1;
   displayTemp = 0;
   waterDisplay = 0; 
   sensorsDisplay = 0;
   updateScreen();
}

if (currentButton == 5 && currentDisplay == 1) //Inside Water Display
{  
  waterDisplay = 1;
  sensorsDisplay = 0;
  displayHumid = 0;
  displayTemp = 0;
  updateScreen();
}

if (currentButton == 5 && currentDisplay == 2)
{
  sensorsDisplay = 1;
  displayTemp = 0;
  waterDisplay = 0;
  displayHumid = 0;
  updateScreen();
}

if (currentButton == 5 && currentDisplay == 3)
 {
  displayTemp = 1;
  waterDisplay = 0;
  displayHumid = 0;
  sensorsDisplay = 0;
  updateScreen();
 } 
// Left Button Reverse Scrolling Through Menu 
if (currentButton == 2 && currentDisplay == 0)
{
  waterDisplay = 1;
  sensorsDisplay = 0;
  displayHumid = 0;
  displayTemp = 0;
  updateScreen();
}

if (currentButton == 2 && currentDisplay == 1)
{
  displayTemp = 1;
  waterDisplay = 0;
  sensorsDisplay = 0;
  displayHumid = 0;
  updateScreen();
}

if (currentButton == 2 && currentDisplay == 2)
{
   displayHumid = 1;
   displayTemp = 0;
   waterDisplay = 0;
   sensorsDisplay = 0;
   updateScreen();
}

if (currentButton == 2 && currentDisplay == 3)
{
  sensorsDisplay = 1;
   displayHumid = 0;
   displayTemp = 0;
   waterDisplay = 0;
   updateScreen();
} 

if (currentButton == 3 && currentDisplay == 0) //When up button is pressed and the temp screen is showing, increment the set temp
{
   ++setTemp;
  updateScreen();
}

if (currentButton == 4 && currentDisplay == 0) //When down button is pressed with the temp screen showing, decrement set temp
{
   --setTemp;
  updateScreen();
}

if (currentButton == 3 && currentDisplay == 1) //When up button is pressed and the humidity screen is showing, inc the set humidity
{
  ++setHumid;
  updateScreen();
}

if (currentButton == 4 && currentDisplay == 1) //When down button is pressed and humidity screen is showing, dec the set humidity
{
  --setHumid;
  updateScreen();
}

if (waterDisplay == 1 && currentButton == 3)
  {
    ++waterSetTemp;
    updateScreen();
  }
  
if (waterDisplay == 1 && currentButton == 4)
    {
      --waterSetTemp;
      updateScreen();
    }


}

void updateScreen(){

  
  
  if (displayTemp == 1) //Shows DHT22 SENSOR temp screen upon first boot
  {
      delay (5000);  
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("ROOM       ");
      lcd.print(int(temp));
      lcd.print("");
      lcd.print((char)223); // degree symbol
      lcd.print("F");
      lcd.setCursor(0,1);
      lcd.print("SET TEMP   ");
      lcd.print(setTemp);
      lcd.print("");
      lcd.print((char)223);
      lcd.print("F");
      currentDisplay = 0; 
  }
  
  if (displayHumid == 1)// DHT22 SENSOR
  {
    delay (5000);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("HUMIDITY    ");
      lcd.print(int(h));
      lcd.print(" %");
      lcd.setCursor(0,1);
      lcd.print("SET HUMID   ");
      lcd.print(setHumid);
      lcd.print(" %");
      
      currentDisplay = 1;
  }    
  
  if (waterDisplay == 1)
  {
    delay (5000);  
    sensors.requestTemperatures(); //This command must be there in order to get live stream of temperature data
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Water    ");
      printTemperature(inside);
      lcd.print("");
      lcd.print((char)223); // degree symbol
      lcd.setCursor(0,1);
      lcd.print("SET TEMP   ");
      lcd.print(waterSetTemp);
      lcd.print("");
      lcd.print((char)223);
      currentDisplay = 2;
  }
  
  if ( sensorsDisplay == 1)
  {
       sensors.requestTemperatures();
       lcd.clear();
       lcd.setCursor(0,0);
       lcd.print(" ");
       printTemperature(inside);
       lcd.print("");
       lcd.print((char)223); // degree symbol
       lcd.print("F");
       lcd.setCursor(0,1);
       lcd.print(" ");
       printTemperature(outside);
       lcd.print("");
       lcd.print((char)223); // degree symbol
       lcd.print("F");
       currentDisplay = 3;
  }

I think your code can be a bit simpler than this.
I'm missing the declarations of the variables (that only hold 0 or 1) that set your screen.

Why don't you use 1 variable that selects a screen number instead of a number of variables that enables a screen, and of which only 1 can ever be active ?
Much easier to handle (if you ask me) because a press of left or right key does a -- or a ++ (with a stop or rollover) of that value.
That saves variables and time to set every variable (even if it was already set to that new value).

Tip: Use that screen as a debug tool.
There have to be a few character positions on that screen you can use to display some values you might want to keep track of.
I've found that to be very helpful.

 if(millis()-lastSensorRead>sensorReadDelay){

Isyourspacebarbroken?

MAS3- Here are my declarations. I'm not sure I understand what you mean by "Why don't you use 1 variable that selects a screen number instead of a number of variables that enables a screen, and of which only 1 can ever be active ?" I only need to view one screen at a time. Check out my code with all my declarations, maybe that will help you understand exactly how I set it up. :slight_smile:

#include <LiquidCrystal.h> //LCD Screen
#include <DFR_Key.h> //LCD Keypad

#include "DHT.h"

#include <OneWire.h> //one wire input sensors
#include <DallasTemperature.h> //Dallas 1 wire DS18B20

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
DFR_Key keypad;

#define DHTTYPE DHT22   // DHT 22  (AM2302)

int sensePin = 11; //Sensor input pin for the DHT22 sensor to control CHANNEL3

int currentButton = 0; //Current button pressed
int currentDisplay = 0;////Keeps track of what screen is currently being displayed. 0 = temp, 1 = humidity, 3 = Watering.
int displayTemp = 1; //Keeps track of what screen is currently being displayed. If displayTemp is 1 temp is showing. If 0 Humidity is showing.
int displayHumid = 0;
int waterDisplay = 0;//NEW SCREEN 1 = Inside or outside water temp display
int sensorsDisplay = 0;//NEW SCREEN 2 = Display for both, inside and outside water temps
int setTemp = 80; //Default to set the desired temp
int setHumid = 50; //Defualt to set the desired humidity
//int outputPin = 3; //Output pin to the relay to trip the fans with Adafruit PowerTail
int displayInsideTemp = 0; //Dallas sensor 1
int displayOutsideTemp = 0; //Dallas sensor 2
int waterSetTemp = 75; 

DHT dht(sensePin, DHTTYPE);

// Dallas sensor data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
DeviceAddress inside = { 0x28, 0xB3, 0x58, 0x66, 0x04, 0x00, 0x00, 0xEC };
DeviceAddress outside = { 0x28, 0x12, 0x31, 0x66, 0x04, 0x00, 0x00, 0x65 };
DeviceAddress waterTank = { 0x28, 0x3E, 0x43, 0x66, 0x04, 0x00, 0x00, 0x40 };

Wow, lots of answers of all people still trying to help you since you decided to put some smart ass replies to the ones that actually did try to help you. :~

If you need help, post all code.
Not just the part of which you think might have an error that is invisible to you, that's my point by telling you i missed the declarations.

And by adding spaces to your code, you make it better readable.
Not just to you, but for sure for someone that responds to your cry for help.

You have these variables:

displayHumid = 1;
displayTemp = 0;
waterDisplay = 0;
sensorsDisplay = 0;

You are setting one of these 4 to 1 and the other ones to 0 every time you select to display another screen.
OK to me, if you need to do it this way (for whatever reason).
I would have used a variable, let's call it displayMode, and have the left and right keys control this mode.
You can do that like this:

if (currentButton == 5)
 {
displayMode ++;
if (displayMode == 5) displayMode = 1; // this is called a rollover, we're over maximum mode, so roll to minimum mode
 } 
// Left Button Reverse Scrolling Through Menu 
if (currentButton == 2)
{
displayMode --;
if (displayMode == 0) displayMode = 4; // this is called a rollover, we're below minimum mode, so roll to maximum mode
}

Next, at the moment you are writing your display, check what displayMode is set and only execute that which belongs to the displayMode.
I would do that utilizing switch..case .

Doesn't that look like it would be easier to do, to be easier to expand and easier to keep track on ?

You're right, that was immature of me. I wasn't really trying to be a smart a$$ but I agree it did come across that way. Sorry everybody :frowning:

I will try this method of scrolling, thanks for taking the time to show me how it's done!

if (currentButton == 5)
 {
displayMode ++;
if (displayMode == 5) displayMode = 1; // this is called a rollover, we're over maximum mode, so roll to minimum mode
 } 
// Left Button Reverse Scrolling Through Menu 
if (currentButton == 2)
{
displayMode --;
if (displayMode == 0) displayMode = 4; // this is called a rollover, we're below minimum mode, so roll to maximum mode
}

I will spend some time rearranging my code to fit this method and check back with my results. I just thought my current method would be easier for the arduino to function with because of the simple layout. The thing that is still kind of bothering me is how inconsistent my program seems to be running. It seems like the more functions I add to my code, the more bugs it has. When it's just 1 or 2 screens, everything works great and is lightning fast... but any more than that and buttons sometimes don't even respond and everything slows down to the point of uselessness. :frowning:

I see you are calling a function updateScreen.
The function seems to be updated every iteration, if a button was pressed.
But before that, you seem to also do the same thing in your loop.
I think you can leave the one in our loop out, and only call your function.
Or remove the function and have the display handled very iteration.

By the way, it also seems to me you are writing to your screen every iteration.
Every write is preceded by a lcd.clear, essentially sending as much spaces to your LCD, as the number of characters available (which in turn means it's going to take some time).
There's a lot of iterations per second.
So you are writing to your LCD a lot of times, which might make your LCD blink.
I'd try to figure out a way to do less lcd.clear 's per second.
One way to do that, is to check if there is some change to the screen to be displayed.
In case you need to do such update, only write to the part that will change.
Of course if you are going to display a new screen, a lcd.clear is ok.

All this doesn't explain why all screens are displayed as you said, but it would result in a somewhat more efficient code i guess.

Any progress yet ?