Go Down

Topic: Error while displaying Keypad input on LCD (Read 905 times) previous topic - next topic

Jewel

Mar 27, 2012, 04:04 am Last Edit: Mar 27, 2012, 04:27 am by Jewel Reason: 1
Hi!

So i have a keypad and a LCD Display with the Arduino Mega and this is my first time with an Arduino.
I am still testing my hardware so all I want to do is punch in a number on the keypad and have it displayed on the LCD. This is my code.

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

/**********************************************************************/
// start of creating the keypad//
const byte ROWS = 4; //Four rows//
const byte COLS = 3; //Three columns//
char keys[ROWS][COLS] =
{
 {'1','2','3'},
 {'4','5','6'},
 {'7','8','9'},
 {'#','0','*'}
}; // Define the Keymap//
byte rowPins[ROWS] = { 17, 16, 15, 14 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins//
byte colPins[COLS] = { 20, 19, 18 }; // Connect keypad COL0, COL1 and COL2 to these Arduino pins//
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );//end of creating the Keypad//
/**********************************************************************/


/**********************************************************************/
// LCD Display//
LiquidCrystal lcd(30, 31, 32, 33, 34, 35); //LiquidCrystal lcd(RS, E, D4, D5, D6, D7), initialize the library with the numbers of the interface pins//
/**********************************************************************/


/**********************************************************************/
void setup()
{
lcd.begin(20,4); //20 columns and 4 rows in the lcd display//
lcd.setCursor(1, 1); //set the location at which subsequent text written to the LCD will be displayed//
}
/**********************************************************************/


/**********************************************************************/
void loop()
{
   lcd.clear();
   delay(1000);
   {
     while (char key = keypad.getKey()==NO_KEY);
     {
       char key = keypad.getKey();
       lcd.print(key);
       delay(3000);
     }
   }
}

/**********************************************************************/

All I am getting now on the LCD screen when I punch a key is this - || two vertical lines irrespective of which key i punch.
I put in print statements in between and figured that the keypad is being detected and so is the key punch but I have no idea why I am getting those two vertical lines. It doesn't even detect which key is being punched , just the keypunch. I tried looking up forums but to no avail!

Can someone please help?!

liudr

Code: [Select]
while (char key = keypad.getKey()==NO_KEY);
      {
        char key = keypad.getKey();
        lcd.print(key);
        delay(3000);
      }

Obviously we don't speak the same language because I can't understand what the above code is trying to do. So why don't you explain what you meant with the above code or was that cut/paste from somewhere else? Makes no sense to me but I didn't write it 8)

Jewel

sure!

That section is just waiting for a keypress and when a key is pressed, it gets the pressed key and prints it on the screen The delay is just to be able to see the output on the display.

Have I made a mistake in the coding?

Jewel

Oh and i found out that if i change the 'char key' to 'int key', I get '0' printed on the LCD for all key presses. That's not really helping me in anyway to find out what's wrong but maybe it might be useful.

MikeOToole

#4
Apr 03, 2012, 11:24 pm Last Edit: Apr 03, 2012, 11:36 pm by MikeOToole Reason: 1
Code: [Select]
char key;

// your main loop //
void loop()
{
 key = keypad.getKey();

 if (key)
 {
   call the display function sending it the key (or use a global key variable)...
   do something else...
 }
}


I do the exact same using Arduino Uno, LCD and Keypad...
See: http://arduino.cc/forum/index.php/topic,99745.msg748141.html#msg748141 for the complete code...

Sacman

I just recently started using the keypad library. This is by far the easiest library to implement straight out of the box and the example code is perfect. I am amazed at how people mess this one up.
Luck,

Wade

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy