Infinite loop when converting char to int - 7-segment display with matrix keyboard

I need to create a circuit that when I press a key on the matrix keyboard, it is shown on the 7-segment display. I managed to structure the code. But it goes into an infinite loop or ends up not showing the number on the Display. Could you guys help me?

The code is below

#include <Keypad.h>
const int pins[7] = {10,A0,A1,A2,A3,A4,A5};

byte byteNumbers[10][7] = {
  {1,1,1,1,1,1,0},
  {0,1,1,0,0,0,0},
  {1,1,0,1,1,0,1},
  {1,1,1,1,0,0,1},
  {0,1,1,0,0,1,1},
  {1,0,1,1,0,1,1},
  {1,0,1,1,1,1,1},
  {1,1,1,0,0,0,0},
  {1,1,1,1,1,1,1},
  {1,1,1,1,0,1,1}
};

char display;
int digit;

void turnOn(int number){
  for(int i = 0; i <= 6; ++i){
    digitalWrite(pins[i], byteNumbers[number][i]);
  }
}

const byte Rows = 4; 
const byte Cols = 4; 

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

byte rowPins[Rows] = {9, 8, 7, 6}; 
byte colPins[Cols] = {5, 4, 3, 2}; 

Keypad customKeypad = Keypad(makeKeymap(rowsCols), rowPins, colPins, Rows, Cols); 

void setup(){ 
  Serial.begin(9600);
  for(int i = 0; i <= 6; i++){
    pinMode(pins[i], OUTPUT);
  }
  for(int i = 2; i <= 9; i++){
    pinMode(i, OUTPUT);
  }
}

void loop(){
  display = customKeypad.getKey();
  digit = display - '0';
  turnOn(digit);
  if(display){
    Serial.println(display);
  }
}

I think the problem is in the loop function.
Thanks in advance.

Those are not the pins for your 7 segment display. Why don’t you use the array you declared?

Also you should check that a key was actually pressed before doing the display

display = customKeypad.getKey();
if (display != NO_KEY) { // key was pressed 
  digit = display - '0';
  …

If you don’t, you get an int that is 0-'0' passed to your function and it’s a mess

Wow, regarding the display showing the number, it worked, thank you very much!!
Could you explain this part of using the array I declared? Did not quite understand.

When you do this in the setup

You set pins 2 to 9 as output. That’s for the keypad (and not necessary as the library does this for you)

But it seems your seven segment display is connected to the pins defined in the array

Since this is what you used in the turnOn() function

So those are the pins you need to set as output. As it stands they are likely INPUT and setting them HIGH turns on the pull-up…

Now I understood. Thank you so much, you are amazing!

wow I'll feel good at least for the rest of the day now :slight_smile:

have fun

side note

this is a lot of memory

given you only have 1 and 0 and less than 8 you could store that in a byte and use bit operators to go grab an element

basically

const byte byteNumbers[] = {
  0b1111110,
  0b0110000,
  0b1101101,
...
  0b1111011}
};

and to access a specific bit (0 is the right side, the least significant bit and 7 the most significant bit so you might want to reverse the bit order in the bytes to keep the code you had) you use bitRead()

that means your digits description now takes only 10 bytes instead of 70