if else if structure not working - char problem?

I have a nano hooked up to a non-standard membrane keypad from an old video distribution system. Even though it is a 2 x 8 layout, I was able to use the standard 4 x 4 layout, and map all the keys to 0-9 and a-f. That works great.

Now I’m trying to create a structure to let me assign commands to each button press. At first I thought I could use switch case, but I’m not sure that works with the hex characters.

So, I have just tried If and else If to see if I could get it working that way, but I am not having any luck. Here’s my code:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_SSD1306.h>
#include <splash.h>
#include <Key.h>
#include <Keypad.h>

LiquidCrystal_I2C lcd(0x3f,20,4);

const byte ROWS = 4;
const byte COLS = 4;

char hexaKeys[ROWS][COLS] = {
{‘4’, ‘3’, ‘2’, ‘1’},
{‘5’, ‘A’, ‘C’, ‘E’},
{‘0’, ‘B’, ‘D’, ‘F’},
{‘9’, ‘8’, ‘7’, ‘6’}

};

byte rowPins[ROWS] = {11, 10, 9, 8};
byte colPins[COLS] = {7, 6, 3, 2};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

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

lcd.init(); //initialize the lcd
lcd.backlight(); //open the backlight

}

void loop() {
char customKey = customKeypad.getKey();

if (customKey){

String strCustomKey = String(customKey);

Serial.println(customKey);

lcd.clear();

if (strCustomKey = “1”){
lcd.setCursor (0,1);
lcd.print(“You Pressed 1”);
}
else if (strCustomKey = “2”){
lcd.setCursor (0,1);
lcd.print(“You Pressed 2”);
}
else if (customKey = 3){
lcd.setCursor (0,1);
lcd.print(“You Pressed 3”);
}
else if (customKey = 4){
lcd.setCursor (0,1);
lcd.print(“You Pressed 4”);
}
else if (customKey = 5){
lcd.setCursor (0,1);
lcd.print(“You Pressed 5”);
}
else if (customKey = 6){
lcd.setCursor (0,1);
lcd.print(“You Pressed 6”);
}
else if (customKey = 7){
lcd.setCursor (0,1);
lcd.print(“You Pressed 7”);
}
else if (customKey = 8){
lcd.setCursor (0,1);
lcd.print(“You Pressed 8”);
}
else if (customKey = 9){
lcd.setCursor (0,1);
lcd.print(“You Pressed 9”);
}
else if (customKey = 0){
lcd.setCursor (0,1);
lcd.print(“You Pressed 0”);
}
else if (customKey = “A”){
lcd.setCursor (0,1);
lcd.print(“You Pressed Clear”);
}
else if (customKey = “B”){
lcd.setCursor (0,1);
lcd.print(“You Pressed Enter”);
}
else if (customKey = “C”){
lcd.setCursor (0,1);
lcd.print(“You Pressed Source”);
}
else if (customKey = “D”){
lcd.setCursor (0,1);
lcd.print(“You Pressed Dest”);
}
else if (customKey = “E”){
lcd.setCursor (0,1);
lcd.print(“You Pressed The Up Arrow”);
}
else if (customKey = “F”){
lcd.setCursor (0,1);
lcd.print(“You Pressed The Down Arrow”);
}
customKey = “”;
}
}

When I run this, as I press each button, the correct character appears in the serial console - so it is recognizing and displaying the key presses correctly.

But, on the LCD screen, I only get “You Pressed 1”, no matter what button I press.

Why don’t any of the else if statements work?

(p.s. - it didn’t make any difference if I just used “customKey”, or “StrCustomKey”)

else if (customKey = 4){Oops

Every single comparison should be ==

Please remember to use code tags when posting code

Thanks - that was it. Still getting used to C...

And I missed the part in the instructions about the code tags - I know better now.

Thanks!

String strCustomKey = String(customKey);

Why do this when you could test the char returned directly ?

else if (customKey = 3){

Even when you fix the problem with equals this will not work because you cast the returned char to a String

Yeah, I was just trying the string because it wasn’t working, and I had only changed the first couple for testing. I’ve change them all to the string now, and it works perfectly.

I’ve change them all to the string now, and it works perfectly.

Just for kicks, try not converting to a String and instead test for a char (single character in single quotes) instead