//define the matrix, X,Y values for each button
char key[4][4]={{'1','2','3','A'},{'4','5','6','B'},{'7','8','9','C'},{'*','0','#','D'}};
//define the pins for the keypad
#define pin1 31
#define pin2 33
#define pin3 35
#define pin4 37
#define pin5 23
#define pin6 25
#define pin7 27
#define pin8 29
//create new char variable for the value from keypad
char Pressed;
//define which pins of keypad are inputs and outputs
int input_pins[4]={31,33,35,37};
int output_pins[4]={23,25,27,29};
char digit;
char mykey(void)
{
int r,c; //integers for row and colum
for(r=0;r<4;r++) //define the course of the loop, from row 0 to row 3
{
digitalWrite(output_pins[r],LOW); //send 0 to all output pins
for(int c=0;c<4;c++)
{
if((digitalRead(input_pins[c])==LOW)) //check for the right colum
return key[r][c];
}
digitalWrite(output_pins[r],HIGH); // send 1 to the right output pin
}
return '$'; //return value for unpressed
}
void setup()
{
Serial.begin(9600);
pinMode(pin1,INPUT_PULLUP); //define which pin is input and which is output
pinMode(pin2,INPUT_PULLUP); //define which pin is input and which is output
pinMode(pin3,INPUT_PULLUP); //define which pin is input and which is output
pinMode(pin4,INPUT_PULLUP); //define which pin is input and which is output
pinMode(pin5,OUTPUT); //define which pin is input and which is output
pinMode(pin6,OUTPUT); //define which pin is input and which is output
pinMode(pin7,OUTPUT); //define which pin is input and which is output
pinMode(pin8,OUTPUT); //define which pin is input and which is output
digitalWrite(pin5,LOW); //start outputs from 1
digitalWrite(pin6,LOW); //start outputs from 1
digitalWrite(pin7,LOW); //start outputs from 1
digitalWrite(pin8,LOW); //start outputs from 1
#define A 53 // define the parts of the digit to specific pins
#define B 51
#define C 49
#define D 47
#define E 45
#define F 41
#define G 39
pinMode(A, OUTPUT); //define pins as outputs
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
pinMode(E, OUTPUT);
pinMode(F, OUTPUT);
pinMode(G, OUTPUT);
}
void loop()
{
Pressed=mykey();
if(Pressed!='$') //if pressed
{
Serial.println(Pressed); //prints the pressed value
}
//delay(200); //delay to get the value from the keypad once and not multiple times for one press
digit = Pressed;
switch(digit)
{
case('0'): //Define which parts of screen to turn on for each digit
digitalWrite(A,LOW);
digitalWrite(B,LOW);
digitalWrite(C,LOW);
digitalWrite(D,LOW);
digitalWrite(E,LOW);
digitalWrite(F,LOW);
digitalWrite(G,HIGH);
break;
case('1'): //Define which parts of screen to turn on for each digit
digitalWrite(A,HIGH);
digitalWrite(B,LOW);
digitalWrite(C,LOW);
digitalWrite(D,HIGH);
digitalWrite(E,HIGH);
digitalWrite(F,HIGH);
digitalWrite(G,HIGH);
break;
case('2'): //Define which parts of screen to turn on for each digit
digitalWrite(A,LOW);
digitalWrite(B,LOW);
digitalWrite(C,HIGH);
digitalWrite(D,LOW);
digitalWrite(E,LOW);
digitalWrite(F,HIGH);
digitalWrite(G,LOW);
break;
case('3'): //Define which parts of screen to turn on for each digit
digitalWrite(A,LOW);
digitalWrite(B,LOW);
digitalWrite(C,LOW);
digitalWrite(D,LOW);
digitalWrite(E,HIGH);
digitalWrite(F,HIGH);
digitalWrite(G,LOW);
break;
case('4'): //Define which parts of screen to turn on for each digit
digitalWrite(A,HIGH);
digitalWrite(B,LOW);
digitalWrite(C,LOW);
digitalWrite(D,HIGH);
digitalWrite(E,HIGH);
digitalWrite(F,LOW);
digitalWrite(G,LOW);
break;
case ('5'): //Define which parts of screen to turn on for each digit
digitalWrite(A,LOW);
digitalWrite(B,HIGH);
digitalWrite(C,LOW);
digitalWrite(D,LOW);
digitalWrite(E,HIGH);
digitalWrite(F,LOW);
digitalWrite(G,LOW);
break;
case ('6'): //Define which parts of screen to turn on for each digit
digitalWrite(A,LOW);
digitalWrite(B,HIGH);
digitalWrite(C,LOW);
digitalWrite(D,LOW);
digitalWrite(E,LOW);
digitalWrite(F,LOW);
digitalWrite(G,LOW);
break;
case ('7'): //Define which parts of screen to turn on for each digit
digitalWrite(A,LOW);
digitalWrite(B,LOW);
digitalWrite(C,LOW);
digitalWrite(D,HIGH);
digitalWrite(E,HIGH);
digitalWrite(F,HIGH);
digitalWrite(G,HIGH);
break;
case ('8'): //Define which parts of screen to turn on for each digit
digitalWrite(A,LOW);
digitalWrite(B,LOW);
digitalWrite(C,LOW);
digitalWrite(D,LOW);
digitalWrite(E,LOW);
digitalWrite(F,LOW);
digitalWrite(G,LOW);
break;
case ('9'): //Define which parts of screen to turn on for each digit
digitalWrite(A,LOW);
digitalWrite(B,LOW);
digitalWrite(C,LOW);
digitalWrite(D,HIGH);
digitalWrite(E,HIGH);
digitalWrite(F,LOW);
digitalWrite(G,LOW);
break;
default:
digitalWrite(A,LOW);
digitalWrite(B,HIGH);
digitalWrite(C,HIGH);
digitalWrite(D,LOW);
digitalWrite(E,HIGH);
digitalWrite(F,HIGH);
digitalWrite(G,HIGH);
break;
}
delay(1000);
}