help with my code

Hi! I'm making a program wherein I will be able to switch on 3 different modes. Modes 1,2 or 3 can be obtain if keys 1,2, or 3 on the 4x3 keypad matrix is pressed.

Mode 1 = key '1' : Is the main panel wherein the LCD only displays the temperature and pH level of the water.
Mode 2 = key '2' : View log.
Mode 3 = key '3' : Enter new recipient's number.

My problem is that when I upload the sketch I can't switch to other modes and stuck at mode 1 even if I press keys 1,2 or 3. Don't know where I'm mistaken. What I want to do is that after uploading, mode 1 will be displayed first not until I hit keys 2 or 3 then it will switch modes. TIA! Here's my code:

void loop()
{
  key = keypad.getKey();
  
  //switch mode:
  if (digitalRead(ledPin)==LOW)
  {
    //Main Panel
    if (key=='1')
    {    
      lcd.setCursor(0,0);
      lcd.print("Temperature: ");
      lcd.setCursor(0,1);
      lcd.print("pH Level: ");
      mode = 1;      
    }
    //View log
    else if (key=='2')
    { 
      lcd.clear();   
      lcd.setCursor(0,0);
      lcd.print("View log:       ");
      mode = 2;
    } 
    //Enter new recipient's number
    else if (key=='3')
    { 
      lcd.clear();   
      lcd.setCursor(0,0);
      lcd.print("Enter number:   ");
      mode = 3;
    }
    else                         
    {
      lcd.setCursor(0,0);
      lcd.print("Temperature: ");
      lcd.setCursor(0,1);
      lcd.print("pH Level: ");
      mode = 1;
    }    
  }

use a CASE switch to set a variable 1, 2, or 3

then write the IF code for each possible.

That way your loop will always run in the mode UNTILL you reset the variable by pressing a key

okay i'll try it with a switch case..thanks!

  if (digitalRead(ledPin)==LOW)

Why are you reading from a pin named ledPin?

PaulS:

  if (digitalRead(ledPin)==LOW)

Why are you reading from a pin named ledPin?

it is only when the led is Low I can switch mode, If it is high, I will be able to write values in the EEPROM. SO it serves as an indication whether I'm on switching modes or writing data.. :slight_smile:

SO it serves as an indication whether I'm on switching modes or writing data.

You can't remember which you are doing?

PaulS:

SO it serves as an indication whether I'm on switching modes or writing data.

You can't remember which you are doing?

Well, it helps me to know what state my program is coz only when I press '*' enables/disables the LED..If its HIGH, I can only write data otherwise, I can only switch modes.. :slight_smile:

Well, it helps me to know what state my program is coz only when I press '*' enables/disables the LED..If its HIGH, I can only write data otherwise, I can only switch modes.

My point was that you are writing a value to the LED pin at some point. Make that value a global variable, and it won't be necessary to read an OUTPUT pin. It will be faster, too, to access the global variable, instead of reading the pin state.