HELP: 4 x 4 Keypad, Menus and Case statements

Help needed, I’m trying to build a controller for Temperature, Humidity and later other parameters using a 20x4 LCD and 4x4 keypad, to enter values rather than up,down, left, right buttons.

I used a subroutine called ‘mainLCDLoop’ to control the backend of LCD menus for navigating setting setpoints menus which I want to Press ‘A’ to enter Temperature settings, Press B for Humidity at this point.

With respect to the temperature settings, I want to press ‘A’ again to call TempLow() subroutine to allow me to enter a new temperature and then save it.

Currently I have tried the TempLow() subroutine by calling it inside a test program inside the loop and it works, however when I place it inside the mainLCDLoop() it does not work correctly. I get value = 0 on the terminal window. I would be grateful for some direction on where I may be going wrong so I can try to sort this out?

#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Wire.h>
#include <EEPROM.h>
#include <dht11.h>
#define DEBUG
#include "RTClib.h"
RTC_DS1307 RTC; // define the Real Time Clock object

//LiquidCrystal_I2C lcd(0x27); // set the LCD address to 0x27 for 2004 display
#define lcdAddr 0x27 // set the address of the I2C device the LCD is connected to

// create an lcd instance with correct constructor for how the lcd is wired to the I2C chip
LiquidCrystal_I2C lcd(lcdAddr, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // addr, EN, RW, RS, D4, D5, D6, D7, Backlight, POLARITY 

/************************DHT11 Hardware Related Macros************************************/
dht11 DHT11;					      // Init DHT11.
#define DHT11_PIN 2				      // DHT11 OUT to digital I/O pin.

/****************************Globals Temperature and Humidity Setpoints*****************/
//here is where the temp and hum setpoints are changed
int tempset=24, tempreset=22;
int humset=60,  humpreset=55;

float FanTemp;
float FanHumid;

int position = 0;
int page = 0;
// Fan Setpoint
int EepromFanTemp = 200;       //location of FanTemp in Eeprom
int EepromFanHumid = 250;      //location of FanHumid in Eeprom

const  byte  ROWS = 4;           // four lines
const  byte  COLS = 4;           // four columns
byte  colPins [4] = {6,5,4,3};   // Pin to which they are connected columns
byte  rowPins [4] = {14,15,8,7};   // Pin to which they are connected lines
char  Keys [ROWS] [COLS] =             // create the array of keys of the keyboard.
{
{ '1 ' , '2 ' , '3 ' , 'A' },
{ '4 ' , '5 ' , '6 ' , 'B' },
{ '7 ' , '8 ' , '9 ' , 'C' },
{ '*' , '0 ' , '#' , 'D' }
};
Keypad keypad = Keypad (makeKeymap (Keys), rowPins, colPins, ROWS, COLS);
long value = 0;                    //value in thousandths of an inch

void  setup () {
  EepromRead();
  Serial.begin (9600);
  lcd.begin(20,4);
  RTC.begin();

  RTC.adjust(DateTime(__DATE__, __TIME__));

  if (!RTC.isrunning()) {
    lcd.setCursor (3,0);
    lcd.print(" *-- RTC's --*");
    lcd.setCursor (3,1);
    lcd.print("  NOT running!");
    Serial.println("RTC is not running");
    delay (50) ;
    lcd.clear();
    RTC.adjust(DateTime(__DATE__, __TIME__));    
    }
  DateTime now = RTC.now();
    
  delay(4000);  
  mainmenu();
}


void loop()

{
  TempLow();
  GetDHT();
  mainLCDLoop();
}

void GetDHT() 
{
  // Send DHT11 message.
  int rc = DHT11.read(DHT11_PIN);
  switch (rc) {
    case DHTLIB_OK:

      if (page == 0)
      {
      lcd.setCursor(0,1);
      lcd.print("H:");
      lcd.print((float)DHT11.humidity, 0);
      lcd.print("%");  
      }    

      if (page == 0)
      {
      lcd.setCursor(0,0);
      lcd.print("T:");
      lcd.print((float)DHT11.temperature, 0);
      lcd.print("C"); 
      } 
      break;

    case DHTLIB_ERROR_CHECKSUM:
      Serial.print("REMOTE1: Checksum!");
      break;
    case DHTLIB_ERROR_TIMEOUT:
      Serial.print("REMOTE1: Time Out!");
      break;
    default:
      Serial.print("REMOTE1: Unknown!");
      break;
  }
}
 
void  mainmenu() 
{
  lcd.setCursor(7,0);
  lcd.print("SET:");
  lcd.print(FanTemp, 2);
  lcd.setCursor(15,0);  
  lcd.print("BUT:A");
 
  lcd.setCursor(7,1);
  lcd.print("SET:");
  lcd.print(FanHumid, 0);
  lcd.setCursor(15,1);  
  lcd.print("BUT:B");
}

void TempSetting()
{
//menu temperature menu
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("TEMPERATURE SETTINGS");
  lcd.setCursor(0,1);
  lcd.print("PRESET LO:");
  lcd.print(FanTemp, 2);
  lcd.print("C");
  lcd.setCursor(15,1);  
  lcd.print("BUT:A");  
  lcd.setCursor(0,2);
  lcd.print("PRESET HI:");
 // lcd.print(Tpreseth, 2);
  lcd.print("C");
  lcd.setCursor(15,2);  
  lcd.print("BUT:B");
//Back to menu location for temperature menu     
  lcd.setCursor(8,3);
  lcd.print("Main Menu");
  lcd.setCursor(19,3);
  lcd.print("#");
} 

void HumiditySetting()
{
//menu humidity menu
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("HUMIDITY SETTINGS");
  lcd.setCursor(0,1);
  lcd.print("PRESET LO:");
 // lcd.print(Hpresetl, 2);
  lcd.print("C");
  lcd.setCursor(15,1);  
  lcd.print("BUT:A");  
  lcd.setCursor(0,2);
  lcd.print("PRESET HI:");
 // lcd.print(Hpreseth, 2);
  lcd.print("C");
  lcd.setCursor(15,2);  
  lcd.print("BUT:B");
//Back to menu location for temperature menu     
  lcd.setCursor(8,3);
  lcd.print("Main Menu");
  lcd.setCursor(19,3);
  lcd.print("#");
}

void EepromRead()            //reads eepromvalue for following variables
{
  FanTemp = EEPROM.read(EepromFanTemp);
  FanHumid = EEPROM.read(EepromFanHumid);
}

void mainLCDLoop()
{
 //Main Screen menu
 if (page == 0)
    {
     char  key = keypad.getKey();
     if  (key == 'A')
        {
          Serial.println (key);
          page = 1;
          lcd.clear();
          TempSetting();
        }
     else if (key == 'B')
        {
          Serial.println (key);
          page = 2;
          lcd.clear();
          HumiditySetting();
        }  
    }
//Temperature setting menu
  if (page == 1)
      {
        char  key = keypad.getKey();
        if  (key == 'A')
        {
          Serial.print(key);
          TempLow();
        }
        else if (key == 'B')
        {
            Serial.println (key);
      //    getpresethigh()
          }  
        else if (key == '#')
          {
            Serial.println (key);
            page=0;
            lcd.clear();
            mainmenu();
          }
      }
      
//Humidity setting menu
  if (page == 2)
      {
        char  key = keypad.getKey();
        if  (key == 'A')
          {
            Serial.println (key);

          }
        else if (key == 'B')
          {
            Serial.println (key);
      //    getpresethigh()
          }  
        else if (key == '#')
          {
            Serial.println (key);
            page=0;
            lcd.clear();
            mainmenu();
          }
      }      
}


void TempLow(){
  char key = keypad.getKey();
  switch (key) {
    case NO_KEY:
      // No key read. Do nothing
      break;

    case '0': case '1': case '2': case '3': case '4':
    case '5': case '6': case '7': case '8': case '9':
      // Digits from 0 to 9
      // Don't forget we deal with ASCII characters and not numbers
      value = value * 10 + key - '0';
      Serial.print(key);
      break;

    case '*':
      // Enter key

      Serial.println ();
      Serial.print ("Temp: ");
      Serial.print (value);
      EEPROM.write(EepromFanTemp,value);
      FanTemp = EEPROM.read(EepromFanTemp);
      Serial.print("Copied to EEprom: ");
      Serial.println(FanTemp);
      value = 0;
      break;
  }
}

Thanks in advance.

Anthony

When you post all of your code, we can help you. Until then, good luck.