// Define the keypad layout: rows x columns
char key[3][3] = {{'1','2','3'}, {'4','5','6'}, {'7','8','9'}};
// Define the pins for the keypad
#define pin1 8
#define pin2 9
#define pin3 10
#define pin5 2
#define pin6 3
#define pin7 4
// Define input and output pins for the keypad
int input_pins[3] = {2, 3, 4};
int output_pins[3] = {8, 9, 10};
char Pressed;
char digit;
// Segment pin definitions
#define A 5
#define B 6
#define C 7
#define D 11
#define E 12
#define F 13
#define G A0
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Keypad pins setup
for (int i = 0; i < 3; i++) {
pinMode(output_pins[i], OUTPUT);
digitalWrite(output_pins[i], HIGH); // Deactivate outputs initially
pinMode(input_pins[i], INPUT_PULLUP); // Set inputs with internal pull-up resistors
}
// Segment pins setup
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
pinMode(E, OUTPUT);
pinMode(F, OUTPUT);
pinMode(G, OUTPUT);
// Turn off all segments initially
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
}
// Function to scan the keypad for pressed buttons
char mykey(void) {
int r, c;
for (r = 0; r < 3; r++) {
digitalWrite(output_pins[r], LOW); // Activate a specific row
for (c = 0; c < 3; c++) {
if (digitalRead(input_pins[c]) == LOW) {
// Print the pressed button's location
Serial.print("Button pressed at row ");
Serial.print(r);
Serial.print(", column ");
Serial.println(c);
// Debounce by waiting for a short time
delay(50); // Adjust this value based on your needs
if (digitalRead(input_pins[c]) == LOW) {
while (digitalRead(input_pins[c]) == LOW) {
// Wait until the button is released
}
digitalWrite(output_pins[r], HIGH); // Deactivate the row
return key[r][c]; // Return the pressed key
}
}
}
digitalWrite(output_pins[r], HIGH); // Deactivate the row
}
return '$'; // Return '$' if no button is pressed
}
void loop() {
Pressed = mykey();
if (Pressed != '$') {
Serial.println(Pressed);
// Display logic using segment pins
switch (Pressed) {
case '1':
// Define segment pin states for '1'
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 segment pin states for '2'
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 segment pin states for '3'
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 segment pin states for '4'
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 segment pin states for '5'
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 segment pin states for '6'
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 segment pin states for '7'
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 segment pin states for '8'
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 segment pin states for '9'
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
break;
// ... Add more cases if needed ...
default:
// Turn off all segments for unhandled cases
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, HIGH);
break;
}
// Add delay to control display refresh rate
delay(100); // Adjust as needed
}
}