#include <Keypad.h>
// Define the number of rows and columns in the keypad
const byte ROWS = 4;
const byte COLS = 4;
// Define the keypad matrix
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Define the pin numbers for the keypad
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
// Define the pin numbers for the LEDs
const int ledPins[] = {10, 11, 12, 13};
// Create the Keypad object
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
for (int i = 0; i < sizeof(ledPins) / sizeof(ledPins[0]); i++) {
pinMode(ledPins[i], OUTPUT);
}
Serial.begin(9600);
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.println("Key Pressed: " + String(key));
handleLED(key);
}
}
void handleLED(char key) {
// Turn off all LEDs initially
for (int i = 0; i < sizeof(ledPins) / sizeof(ledPins[0]); i++) {
digitalWrite(ledPins[i], LOW);
}
// Determine which LED to turn on based on the pressed key
switch (key) {
case 'A':
digitalWrite(ledPins[0], HIGH); // Turn on LED corresponding to 'A'
break;
case 'B':
digitalWrite(ledPins[1], HIGH); // Turn on LED corresponding to 'B'
break;
case 'C':
digitalWrite(ledPins[2], HIGH); // Turn on LED corresponding to 'C'
break;
case 'D':
digitalWrite(ledPins[3], HIGH); // Turn on LED corresponding to 'D'
break;
default:
break;
}
}