LCD not clearing during if statement

Hi everyone,

I'm currently trying to get my LCD screen to return various info based on certain key presses. So far I have the second if statement working, but the first doesn't seem to be working. The LCD starts by asking "Enter A-D" at (0,0). When the "A" button of the keypad the screen is supposed to clear and then say "Enter Amount 0-255" at the same start. Unfortunately, it doesnt seem to be doing this. I've added lcd.clear() and even tried lcd.begin(16,2) in an attempt to clear it for the message, but nothing seems to work. The second line works if I press "B", so it seems the LCD isnt clearing the top text when it enters the "if" statement.

Is there a way to get the LCD to clear in the "if" statement?

The code is below and also attached for reference. The board I'm using is a mega.

#include <Keypad.h>
#include <Wire.h> 
#include <LiquidCrystal.h>

const int rs = 52, en = 11, d4 = 10, d5 = 50, d6 = 12, d7 = 48;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);


int out1 = 3;
char keyselect;

const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};

byte rowPins[ROWS] = {
9,8,7,6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
5,4,3,2}; //connect to the column pinouts of the keypad

Keypad customKeypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS); //initialize an instance of class NewKeypad 



void setup()
{
lcd.begin(16, 2);
pinMode(out1, OUTPUT);
}

void loop()
{

lcd.setCursor(0,0);


lcd.print("Enter A-D");
keyselect = customKeypad.getKey();
if (keyselect == 'A') {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Enter Amount 0-255");


}
if (keyselect == 'B') {
   lcd.clear();
 lcd.setCursor(0, 1);
 lcd.print("B Selected");
}  
}

V4.ino (1.21 KB)

I ran your sketch and it IS displaying "Enter Amount 0-255" when I press A. It's just immediately overwriting it with "Enter A-D" as soon as it gets back to the top of loop(), so what it's actually showing is:

Enter A-D[color=red]unt 0-255[/color]

hmm ok, so should I place a loop within the if statement to keep it in there?

Check your sketch. Your loop() function does what it's expected to do.
You are displaying : "Enter A-D"
Then you are waiting for keyboard input.
By entering A you are displaying : "Enter amount 0-255"
Now you don't do anything, so you just goes back to start loop().
You need to do something after "Enter amount 0-255"

Btw: READ: General Guidance and How to use the Forum.
Mostly the info regarding sending your code.

/ffur

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

Thanks for the tip on the code and the post!

Fixed the code; Had to insert an lcd.clear() command after the keypress THEN add a while loop with the same exit keypress as the if statement. Now it clears the LCD and enters a while loop to correctly display everything.

Thanks for the help!

For those who have similar problems, here's the code I'm using with the edits (its a little more in depth).

Also, forgot to mention, but big thanks to Andrew Mascolo who wrote a password code that this utilizes! This particular code helped a lot to add options to the keypad.

#include <Keypad.h>
#include <Wire.h> 
#include <LiquidCrystal.h>

const int rs = 52, en = 11, d4 = 10, d5 = 50, d6 = 12, d7 = 48;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

#define Password_Lenght 7 // Give enough room for six chars + NULL char


char Data[Password_Lenght]; // 6 is the number of chars it can hold + the null char = 7
char Master[Password_Lenght] = "1234"; 
byte data_count = 0, master_count = 0;
bool Pass_is_good;
char customKey;
unsigned char G = 0;
int out1 = 3;
char keyselect;

//test
int myint;

const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[ROWS] = {
  9,8,7,6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
  5,4,3,2}; //connect to the column pinouts of the keypad

Keypad customKeypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS); //initialize an instance of class NewKeypad 

void setup()
{
  lcd.begin(16, 2);
  pinMode(out1, OUTPUT);
}

void loop()
{
 customKey=customKeypad.getKey();
  lcd.setCursor(0,0);
  lcd.print("Amount=");
  lcd.setCursor(8,0);
  lcd.print(myint);
  
customKey=customKeypad.getKey();
if (customKey == 'A'){
  lcd.clear();
  customKey = customKeypad.getKey();
  while (customKey !='#') {
    lcd.setCursor(0,0);
    lcd.print("Set");
  customKey = customKeypad.getKey();
  if (customKey && customKey != '#') // makes sure a key is actually pressed, equal to (customKey != NO_KEY)
  {
    Data[data_count] = customKey; // store char into data array
    lcd.setCursor(data_count+4,0); // move cursor to show each new char
    lcd.print(Data[data_count]); // print char at said cursor
    data_count++; // increment data array by 1 to store new char, also keep track of the number of chars entered
    }

  if(customKey == '#') // if the array index is equal to the number of expected chars, compare data to master
  {
    lcd.clear();
    lcd.setCursor(0, 1);
    lcd.print(Data);
    myint = atoi(Data);
    lcd.clear();
    lcd.print(myint);
    delay(1000);// added 1 second delay to make sure the password is completely shown on screen before it gets cleared.
    lcd.clear();
    clearData();
    
  }}
  data_count=0;
  lcd.clear();}

  if(customKey == 'B'){
  lcd.setCursor(0,1);
  lcd.print(myint);
  delay(1000);
  lcd.clear();
  }
  
  }
  void clearData()
{
  while(data_count !=0)
  {   // This can be used for any array size, 
    Data[data_count--] = 0; //clear array for new data
  }
  return;
}