strange behavior from numeric keypad

I’ve written a function that takes in values from a 4X3 numeric keypad and compares them with numbers to decide which compartment to use in a candy vending machine project my son is working on.) It’s supposed to return “invalid choice” for anything other than the numbers 1 and 2 from the keypad. It works fine for 1 and 2 and returns invalid for all other numbers except 8,9 or *. If I input any of these numbers, they’re interpreted as valid under the strcmp ‘2’ condition. I’m stumped as to why these characters are being equated with ‘2’. Can anyone enlighten me?

#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 3; //four columns

int Candy_choice;
boolean Candy_chosen = false;
int Which_candy;

char Key_pressed;

//define the symbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {

byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad My_Keypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void Get_choice()
Serial.print("Enter choice: ");
while(! Candy_chosen) //if the user hasn’t chosen which candy they want
Key_pressed = My_Keypad.getKey(); //wait for a key to be input
if (Key_pressed) //a key has been pressed
if(!strcmp(‘1’, Key_pressed)) //if the key is “1”
Which_candy = 1; //set the candy choice to 1
Candy_chosen = true; //set the candy chosen flag to true
Serial.print("Enter password: "); prompts user for password
return; //return to main loop
if(!strcmp(‘2’, Key_pressed))
Which_candy = 2;
Candy_chosen = true;
Serial.print("Enter password: ");
Serial.println(“invalid choice”);
Serial.print("Enter choice: ");

Suggest you spend some time with a few tutorials.


Many, many errors in this code. strcmp() expects zero terminated character arrays, like "string" or

char s[]={'s','t','r','i','n','g',0};

'2' is a single character.

if(!strcmp('2', Key_pressed))

To compare single characters, the following is acceptable:

if (Key_pressed == '2') do_something();

       Key_pressed = My_Keypad.getKey(); //wait for a key to be input

That function does NOT wait for a key to be input.

Who else's keypad would you be using? Really, now, is it that difficult to come up with reasonable instance names?

I'm most grateful for the responses and will do my best to mend the errors of my ways. I've gotta say though that as a non-programmer dad trying to learn how to write arduino code on the QT to help out his kid, the condescending remarks come off as unfairly harsh.

The above being said, Im anxious to learn to do things the right way... Can I get a quick summary of at least some of the "many,many errors" in my code? (have already changed the name of my keypad instance!)

Please read the posts "How to use this forum - please read" and "Read this before posting a programming question".

Can I get a quick summary of at least some of the "many,many errors" in my code?

It's more like one error repeated. strcmp() is used to compare strings, not individual characters.

"the condescending remarks come off as unfairly harsh."

I've reviewed the posts, I see nothing condescending at all. Looks like mostly straightforward direction & suggestions to me.

Keep working on your coding, it'll get better with practice.


ones or zeros, right or wrong, dont be offended

Points well-taken... it was the "Really, now, is it that difficult..." that I guess I over-reacted to.

BTW, substituting the "if (Key_pressed == n)" got rid of the strange behavior I was experiencing... And I've just discovered switch, which has eliminated all those ugly nested if-then-else statements!

Thanks to everyone for the help... I'm sure I'll be back!

skitch/case solves a lot of problems when decoding keypad presses, see examples mentioned.