Pages: [1]   Go Down
Author Topic: Error while displaying Keypad input on LCD  (Read 768 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?!
« Last Edit: March 26, 2012, 09:27:24 pm by Jewel » Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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 smiley-cool
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Dublin, Ireland
Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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...
« Last Edit: April 03, 2012, 04:36:56 pm by MikeOToole » Logged

Cleveland, TN
Offline Offline
Full Member
***
Karma: 2
Posts: 207
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Luck,

Wade

Pages: [1]   Go Up
Jump to: