Programming problem in code (logic error)

Dear all, I am currently interfacing an LCD and Keypad to the Arduino and have tested the LCD and Keypad alone, an they worked.

Now in my program I am trying to interface them together.
After a set of instrctions to the user is illustrated on the LCD, the user has to then press a Key.

This is my code. I do not understand why it is not functioning good

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

LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2); //rs,rw,enable,d4,d5,d6,d7

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char hexaKeys[ROWS][COLS] = {
{‘1’,‘2’,‘3’,‘A’},
{‘4’,‘5’,‘6’,‘B’},
{‘7’,‘8’,‘9’,‘C’},
{’*’,‘0’,’#’,‘D’}
};

byte rowPins[ROWS] = {23, 25, 27, 29}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {31, 33, 35, 37}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad cusomKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup()
{
Serial.begin(9600);
lcd.begin(16,2); //initialise LCD
Display(); //call function
//Mykeypad();

}
void loop()
{
bool flag;

lcd.noCursor(); //turn off the cursor
delay(500);
lcd.cursor(); //turn on the cursor
delay(500);

do
{
char customKey = cusomKeypad.getKey();
Serial.println(customKey);
flag = true;
lcd.print(customKey);
}
while(flag == false);

flag = true;

}

void Display() //display function when switching on device
{

lcd.print(“Choose Setup:”); //display on lcd
delay(2000); //give some time for user to read
lcd.clear(); //clear LCD screen
lcd.print("Press A for "); //display on LCD
lcd.setCursor(0, 1); //set cursor bottom left
lcd.print(“entering amount”); //display on LCD
delay(2000); //give some time for user to read
lcd.clear(); //clear LCD screen
lcd.print(“using the keypad”); //display on LCD
delay(2000); //give some time for user to read
lcd.clear(); //clear LCD screen
lcd.print(“Press B for”); //display on LCD
lcd.setCursor(0,1); //set cursor bottom left
lcd.print("entering car "); //display on LCD
delay(2000); //give some time for user to read
lcd.clear(); //clear LCD screen
lcd.print(“manufacturer”); //display on LCD
delay(2000); //give some time for user to read
lcd.clear(); //clear LCD screen
lcd.print(“Press C for”); //display on LCD
lcd.setCursor(0,1); //set cursor bottom left
lcd.print(“interfacing with”); //display on LCD
delay(2000); //give some time for user to read
lcd.clear(); //clear LCD screen
lcd.print(“a computer”); //display on LCD
lcd.setCursor(0,1); //set cursor bottom left

}

void Mykeypad()
{

}

I think the problem is since the function loops and so the bool will go under the do condition.

Is theere any way in which I can do this pls?
Thank you

A part of your code:

  bool flag;
  
  lcd.noCursor();                              //turn off the cursor
  delay(500);
  lcd.cursor();                                //turn on the cursor
  delay(500); 
  
  
   do
   {
     char customKey = cusomKeypad.getKey();
     Serial.println(customKey);
     flag = true; 
     lcd.print(customKey);
   }
   while(flag == false);

Try assigning the flag variable a value... if it is left floating you may have a problem. Also, the do...while loop only executes if the flag var is false... but outside the loop, you make it true... so the di function will only exec. once. Also, the 1/2 seconds delays that you have will wait every time the loop executes, and keep in mind that delay() turns EVERYTHING else off... including inputs.

Can you please tell us what you want to happen (not a program) i.e. I want to print "Hello" when a user presses a button.

Thanks.

Yes I tried giving the flag a value, but still is resulting in same result...

I want to print on the LCD some instructions for the user - DONE then user presses a key on the keypad - and this needs to be shown on the LCD.

Thats it for now

Thanks

And what is it doing with the above code?

Managed to get code to work, to show button pressed on LCD. Now I have to return that value and use it in the main...

Thanks

so your customkey variable must hold the key presses, right?

If you make that varibale 'global' (check out scope of variables on the arduino reference for help with that) then any function can access it.

Also... you don't need all those comments in the display() function. maybe one comment at the beginning saying what it does, but not on every line.

Good Luck! :)

Also, what's that thing at the beginning doing turning the cursor on and off? You may run into the problems with that.

Thats so that the LCD cursor beeps on and off. I think im gona get rid of that if it isn’t needed.

Yes now I am trying to make it global , so that when the user presses A or B or C it enters the function required, but till now havenet got it working yet.

This is my code till now:

#include <LiquidCrystal.h>
#include <Keypad.h>
//#include <ctype.h>

LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2); //rs,rw,enable,d4,d5,d6,d7

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char hexaKeys[ROWS][COLS] = {
{‘1’,‘2’,‘3’,‘A’},
{‘4’,‘5’,‘6’,‘B’},
{‘7’,‘8’,‘9’,‘C’},
{’*’,‘0’,’#’,‘D’}
};

int customKey;

byte rowPins[ROWS] = {23, 25, 27, 29}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {31, 33, 35, 37}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad cusomKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup()
{

//char customKey;
Serial.begin(9600);
lcd.begin(16,2); //initialise LCD
Display(); //call function
Mykeypad();
if(customKey == 65)
{
lcd.print(“Hello”);
delay(2000);
}

}
void loop()
{
bool flag = true;

lcd.noCursor(); //turn off the cursor
delay(500);
lcd.cursor(); //turn on the cursor
delay(500);

}

void Display() //display function when switching on device
{

lcd.print(“Choose Setup:”); //display on lcd
delay(2000); //give some time for user to read
lcd.clear(); //clear LCD screen
lcd.print("Press A for "); //display on LCD
lcd.setCursor(0, 1); //set cursor bottom left
lcd.print(“entering amount”); //display on LCD
delay(2000); //give some time for user to read
lcd.clear(); //clear LCD screen
lcd.print(“using the keypad”); //display on LCD
delay(2000); //give some time for user to read
lcd.clear(); //clear LCD screen
lcd.print(“Press B for”); //display on LCD
lcd.setCursor(0,1); //set cursor bottom left
lcd.print("entering car "); //display on LCD
delay(2000); //give some time for user to read
lcd.clear(); //clear LCD screen
lcd.print(“manufacturer”); //display on LCD
delay(2000); //give some time for user to read
lcd.clear(); //clear LCD screen
lcd.print(“Press C for”); //display on LCD
lcd.setCursor(0,1); //set cursor bottom left
lcd.print(“interfacing with”); //display on LCD
delay(2000); //give some time for user to read
lcd.clear(); //clear LCD screen
lcd.print(“a computer”); //display on LCD
delay(2000);
lcd.setCursor(0,1); //set cursor bottom left

}

int Mykeypad()
{
bool flag = false;

do
{

char customKey = cusomKeypad.getKey();
Serial.println(customKey);
lcd.clear();

if(customKey == 0)
{
lcd.clear();
}
else if((customKey == ‘A’) ||(customKey == ‘B’)||(customKey == ‘C’))
{
lcd.print(“Button pressed:”);
lcd.setCursor(0,1);
lcd.print(customKey);
delay(2000);
customKey == 65; //FOR TESTING BUT NOT WORKING
return customKey;
flag = true;
}
else
{

lcd.print(“Wrong button”);
lcd.setCursor(0,1);
lcd.print(“enter again”);
delay(2000);

}

}
while(flag == false);

}

is making it global just like i did declaring it outside setup??

if yes then why is it not printing Hello at the final stage when I press A?

Tnx

The cursor blinky part needs to go in the setup func... other wise it will do that every second, preventing your program from executing.

To (sort of) answer your question, the global variable must be OUTSIDE all functions, i.e. after your #include but before the setup function.

if yes then why is it not printing Hello at the final stage when I press A?

65 is the ascii for 'A' (cap. latin a)