Aloha folks,
I am attempting to compare characters gathered from a keypad to a code "123" While searching the forums, I came upon a couple solutions:
I've tried using "strcmp (strcmp(key, "123") == 0" //I've also tried setting a string variable to 123 and using the string instead of "123"
I get this error:
Keypad_Screen_ino.ino: In function 'void loop()':
Keypad_Screen_ino:49: error: cannot convert 'String' to 'const char*' for argument '1' to 'int strcmp(const char*, const char*)'
I've also tried "if (key.contains("123"))" and received this error:
Keypad_Screen_ino.ino: In function 'void loop()':
Keypad_Screen_ino:50: error: 'class String' has no member named 'contains'
I've included my overall code below:
#include <Keypad.h>
#include "U8glib.h"
#include <WString.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 );
String key; // key should be a global variable, written in "loop()" and read in "draw()"
char tempkey;
void draw(void) {
u8g.setFont(u8g_font_osb18);
u8g.setPrintPos(0, 18);
u8g.print(key);
}
void draw2(void) {
u8g.setFont(u8g_font_osb18);
u8g.setPrintPos(0, 60);
u8g.print(key);
}
void setup(){
Serial.begin(9600);
}
void loop(){
String solution = "123";
tempkey = keypad.getKey();
if (tempkey != NO_KEY){
key = key+tempkey;
Serial.println(key);
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
//if (strcmp(key, "123") == 0){
//if (strcmp( key, solution ) == 0 ){
if (key.contains("123")) {
do {
draw2();
} while( u8g.nextPage() );
}
}
}
Any ideas would be most appreciated!