Membrane Keypad and Seven Segment Display

Hello all!
I’m relatively new to Arduino, but I have a project that I’ve been working on, and I’d like some help.
I have a 4x4 membrane keypad, and I want it to control a one digit 7 segment display.

Here’s what I’ve done:

I hooked up a 74HC595 shift register to 3 pins on my Elegoo UNO R3 and hooked up the 4x4 keypad to 8 other pins. From the shift register, I sent all the outputs to a single digit 7 segment display. I can both print to the display by sending binary through the shift register and read imputs from the keypad.

I have some unfinished code here

int latchPin=11;
int clockPin=9;
int dataPin=12;
int dt=250;
byte zero = 0b11111100; // These are the variable that will be sent to my seven segment display. 
byte one=0b01100000;     //On the display Q0 = A, Q1 = B,....Q7 = DP
byte two=0b11011010;
byte three = 0b11110010;
byte four = 0b01100110;
byte five = 0b10110110;
byte six = 0b10111110;
byte seven = 0b11100000;
byte eight = 0b11111110;
byte nine = 0b11100110;
#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns

char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8, 13}; //connect to the column pinouts of the keypad

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



void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(latchPin,OUTPUT);
pinMode(dataPin,OUTPUT);
pinMode(clockPin,OUTPUT);
Serial.begin(9600);
}



void loop() {
  // put your main code here, to run repeatedly:
char key = keypad.getKey(); // this should and does read keypresses 

//KeyState state = keypad.getState();
Serial.print(key); //this prints keypresses to the serial moniter => it works

//This if statement is just a test, but it is not working.... 
if (key==7){        
  Serial.print("IT WORKS");
}

//This next code is just how I will send any number to the seven segment display. 
//All I need to change is the last input in the shiftOut command to one of the previously defined bytes (one, two, etc.)
  digitalWrite(latchPin,LOW);
shiftOut(dataPin,clockPin,LSBFIRST,seven);
digitalWrite(latchPin,HIGH);
delay(dt);


}

I want to print keypresses to the display and keep the display lit up until another keypress, but it seems like the if statemens and the matrix display aren’t really working well.

Here’s a couple of ideas I’ve had:

1: Have an if statement for each number, and put a while loop inside of that statement that sets the display to the correct number and breaks when a key is pressed again. ( I could use KeyState getState())

2: Have a switch statement with each case displaying the correct number inside of a while loop that breaks when a key is pressed.

Is there a simpler way to do this? I’ve really haven’t gotten the matrix keypad to do anything outside of the serial monitor.

Thanks!

A couple of things…

When you read a key, you don’t read 7, you read ‘7’ which is the ASCII value 0x37 so that is why your if() statement is not testing true.

If you put all your 7 segment digits into an array, you can easily translate what to display.

The basic outline of the code

const int latchPin = 11;
const int clockPin = 9;
const int dataPin = 12;
const unsigned long dt = 250;
const byte digits[] = {
  0b11111100, // These are the variable that will be sent to my seven segment display.
  0b01100000,   //On the display Q0 = A, Q1 = B,....Q7 = DP
  0b11011010,
  0b11110010,
  0b01100110,
  0b10110110,
  0b10111110,
  0b11100000,
  0b11111110,
  0b11100110,
};

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns

char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8, 13}; //connect to the column pinouts of the keypad

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

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  Serial.begin(9600);
}



void loop() {
  // put your main code here, to run repeatedly:
  char key = keypad.getKey(); // this should and does read keypresses

  //KeyState state = keypad.getState();
  Serial.print(key); //this prints keypresses to the serial moniter => it works

  //This if statement is just a test, but it is not working....
  if (key >= '0' && key >= '9') {
    Serial.print("IT WORKS");
    key = key = '0';

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, digits[key]);
    digitalWrite(latchPin, HIGH);
    delay(dt);
  }
}

Thank you very much!!!