7 segment LED display controlled by keypad

Hello guys!
Last week I have got my first arduino ever. I will use it for my project - substitution board. It will have 4 7 segment displays and will be controlled with keypad. I have done 1x 7 segment already. But this is my first project with arduino, so I have no expirience with programing here. I would like to ask you if you could tell me the code or where I could find the answers.

My idea is that when I press number five on keypad, the number five will show on display.
Like if you press 5 ,turn this and this segment on.

I would be very thankful for all answers! :slight_smile:

You find all of your answers in Arduino tutorials and the examples provided with Arduino and the libraries you use :wink:

Aka, don't expect to create a program out of thin air without knowing how to program. So start there :wink:

What kind of displays? Common anode or common cathode?
Do you have a MAX7219, or any shift registers, or any transistors?

Thank you for quick answer septillion!
Till now i know how to turn on each segment. But keypad is next level of progaming. I will follow your advice.

I have just Arduino Mega, 7 LED segment that I do by myself (I wired all ground together) and keypad 4x4 (1-9 and a,b,c,d, #, *).

That's pretty simple then. Use the Keypad.h library to read the keypad, and 7 outputs to drive the 7 segments (thru 220 to 270 ohm current limit resistors).
If you use a Port of the Mega to drive the LEDs, then it can be easy with an array to map the segments to hardware.

byte fontArray [] = {
0b00111111, // 0 DP-g-f-e-d-c-b-a, 1 = segment on, 0 = segment off
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
etc
0b01110001, // F
};
In setup, declare one of the ports as output pins, say port A
for (byte x = 22; x<30; x=x+1){
pinMode (x, OUTPUT);
}

Then in loop, once you've read in a key and want to display it:
PORTA = fontArray[keypressed]; // with PORTA is pins D29 to D22, connect to DP-g-f-e-d-c-b-a
// keypressed = 0,1,2,3,4,5,6,7,8,9,A,b,C,d,E,F, with matching entries in the fontArray

And I just copied this from another post:

#include <Keypad.h>
#define LED 13
const byte ROWS = 4; 
const byte COLS = 4;
char keyInsert[6];

//int i = 0;
//int j = 0;
//int s = 0;
//int x = 0;

//char code[7]= "11ABCD";
char Keys[ROWS][COLS]=
{
{'1','2','3','A'} ,
{'4','5','6','B'},
{'7','8','9','C'},
{'F','0','E','D'}  // I changed # and * to F and E here
};
byte colPins[4] = {5,4,3,2}; 
byte rowPins[4] = {9,8,7,6}; 
 
Keypad keypad = Keypad( makeKeymap(Keys), rowPins, colPins, ROWS, COLS);
// Add your font array here
 
void setup(){
  Serial.begin(9600);
  pinMode(LED,OUTPUT);

// Add your pinMode code here
}
 
void loop(){
  char key = keypad.getKey();
// add your port/segment driving lines here
PORTA = fontArray[key]; // with PORTA is pins D29 to D22, connect to DP-g-f-e-d-c-b-a

That should pretty much do it I think.

7 Segments are arranged like so:

A
F B
G
E C
D

Thank you very very much!
I will try it in simulator and tell you if it work. :slight_smile: :slight_smile:

I draw my project in Autodesk Circuits (but there I could only use arduino Uno), I copy code that you show me. But I do not know what exactly I must to do in this step --> // add your port/segment driving lines here //
Also when I want to start simulation program give me error 25:1: error: expected unqualified-id before numeric constant

What is wrong?
Here is link https://circuits.io/circuits/1807671-7-led-segment-display

Or just a code:
#include <Keypad.h>
#define LED 13
const byte ROWS = 4;
const byte COLS = 4;
char keyInsert[6];

//int i = 0;
//int j = 0;
//int s = 0;
//int x = 0;

//char code[7]= "11ABCD";
char Keys[ROWS][COLS]=
{
{'1','2','3','A'} ,
{'4','5','6','B'},
{'7','8','9','C'},
{'F','0','E','D'} // I changed # and * to F and E here
};
byte colPins[4] = {10,11,12,13};
byte rowPins[4] = {A5,A4,A3,A4};

Keypad keypad = Keypad( makeKeymap(Keys), rowPins, colPins, ROWS, COLS);
// Add your font array here
0b00111111, // 0 DP-g-f-e-d-c-b-a, 1 = segment on, 0 = segment off
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
etc
0b01110001, // F
void setup(){
Serial.begin(9600);
pinMode(LED,OUTPUT);

// Add your pinMode code here
for (byte x = 2; x<9; x=x+1){ // here I changed formula x=22; x<30; x=x+1
pinMode (x, OUTPUT);
}

void loop(){
char key = keypad.getKey();
// add your port/segment driving lines here
PORTA = fontArray[key]; // with PORTA is pins D29 to D22, connect to DP-g-f-e-d-c-b-a

I can't compile here. From what you have tho:

  1. Finish the fontArray - I only did a couple characters (0,1,2,3,F), you need to create the rest and complete the array with };
    It should look like this:
    byte fontArray[] = {
    0b00111111, // 0
    // other letters, with the same kind of syntax I used
    0b01110001, // F
    };
  2. setup() needs a } to close out the setup function.
  3. loop() just needs a } at the end to close out loop the loop ( ) function.

.