LCD to show Keypad input, not displaying

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").

#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!

The reason for the malfunction could be cause of using the getKey function inside the "picture loop". This was also discussed recently here: testing u8glib and GLCD 12864 - Displays - Arduino Forum

Maybe this modified code works:

#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

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.

Even more simpler could be this:

#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

That worked, thank you so much : )