Go Down

Topic: LCD to show Keypad input, not displaying (Read 1 time) previous topic - next topic

ordenrahl

Aloha,

I am using a 4x4 Universal Keypad and a SainSmart 12864 LCD screen in hopes to display numbers punched in from a keypad and then check against a stored string (In this case "I < 3 U").  I tried looking at other posts on this forum but haven't seen where anyone has printed using the keypad.getkey function.  When I run this code, I DO get the correct inputs on the serial monitor when I punch the keys (though they are laggy).  There is some problem using the ug8.print function(key) when key is set by keypad.getkey (it works when I set to a integer like "399").

Code: [Select]
#include <Keypad.h>
#include "U8glib.h"

U8GLIB_ST7920_128X64 u8g(13, 11, 12, U8G_PIN_NONE);

const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
  {'1','2','3','<'},
  {'4','5','6','>'},
  {'7','8','9','='},
  {'*','0','I','U'}
};
byte rowPins[ROWS] = {2,3,4,5}; //connect to row pinouts
byte colPins[COLS] = {6,7,8,9}; //connect to column pinouts

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char key; //= keypad.getKey(); [color=green]//I feel like I should make this a global variable, but it doesn't seem to make much difference either way.[/color]

void draw(void) {

  char key = keypad.getKey();
  u8g.setFont(u8g_font_osb18);
  u8g.setPrintPos(0, 18);
  u8g.print(key);


}

void setup(){
  Serial.begin(9600);
}

void loop(){
  u8g.firstPage();
  char key = keypad.getKey();
  do {
    draw();
  } while( u8g.nextPage() );
 
  delay(500);
 

  if (key != NO_KEY){
    Serial.println(key);
}
}


Thank you for your help!

olikraus

#1
Jul 31, 2013, 11:17 am Last Edit: Jul 31, 2013, 11:18 am by olikraus Reason: 1
The reason for the malfunction could be cause of using the getKey function inside the "picture loop". This was also discussed recently here: http://forum.arduino.cc/index.php?topic=179389.0

Maybe this modified code works:
Code: [Select]

#include <Keypad.h>
#include "U8glib.h"

U8GLIB_ST7920_128X64 u8g(13, 11, 12, U8G_PIN_NONE);

const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
  {'1','2','3','<'},
  {'4','5','6','>'},
  {'7','8','9','='},
  {'*','0','I','U'}
};
byte rowPins[ROWS] = {2,3,4,5}; //connect to row pinouts
byte colPins[COLS] = {6,7,8,9}; //connect to column pinouts

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

char key;  // key should be a global variable, written in "loop()" and read in "draw()"

void draw(void) {

  u8g.setFont(u8g_font_osb18);
  u8g.setPrintPos(0, 18);
  u8g.print(key);


}

void setup(){
  Serial.begin(9600);
}

void loop(){
  char tmp_key;
  tmp_key = keypad.getKey();
  if ( tmp_key != NO_KEY )
    key = tmp_key;
  u8g.firstPage();
  do {
    draw();
  } while( u8g.nextPage() );
  delay(500);
  if (tmp_key != NO_KEY){
    Serial.println(tmp_key);
}
}


Oliver

ordenrahl

Thank you Oliver.  I'll give this a shot when I get home from work tonight. I I like how you used the temp_key variable to check for the != NO_KEY. 


olikraus

Even more simpler could be this:
Code: [Select]

#include <Keypad.h>
#include "U8glib.h"

U8GLIB_ST7920_128X64 u8g(13, 11, 12, U8G_PIN_NONE);

const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
  {'1','2','3','<'},
  {'4','5','6','>'},
  {'7','8','9','='},
  {'*','0','I','U'}
};
byte rowPins[ROWS] = {2,3,4,5}; //connect to row pinouts
byte colPins[COLS] = {6,7,8,9}; //connect to column pinouts

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

char key;  // key should be a global variable, written in "loop()" and read in "draw()"

void draw(void) {
  u8g.setFont(u8g_font_osb18);
  u8g.setPrintPos(0, 18);
  u8g.print(key);
}

void setup(){
  Serial.begin(9600);
}
void loop(){
  key = keypad.getKey();
  if (key != NO_KEY){
    Serial.println(key);
    u8g.firstPage();
    do {
      draw();
    } while( u8g.nextPage() );
}
}


Oliver

ordenrahl


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