#include <Keypad.h>
// Define keypad setup
const byte ROW_NUM = 4; // four rows
const byte COL_NUM = 4; // four columns
char keys[ROW_NUM][COL_NUM] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; // Connect to row pins of the keypad
byte pin_column[COL_NUM] = {5, 4, 3, 2}; // Connect to column pins of the keypad
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COL_NUM);
void setup() {
// Initialize the LED pin as an output
pinMode(2, OUTPUT);
digitalWrite(2, LOW); // Ensure LED is off initially
pinMode(3, OUTPUT);
digitalWrite(3, LOW);
pinMode(4, OUTPUT);
digitalWrite(4, LOW);
pinMode(5, OUTPUT);
digitalWrite(5, LOW);
pinMode(6, OUTPUT);
digitalWrite(6, LOW);
pinMode(7, OUTPUT);
digitalWrite(7, LOW);
pinMode(8, OUTPUT);
digitalWrite(8, LOW);
pinMode(9, OUTPUT);
digitalWrite(9, LOW);
pinMode(10, OUTPUT);
digitalWrite(10, LOW);
pinMode(11, OUTPUT);
digitalWrite(11, LOW);
pinMode(12, OUTPUT);
digitalWrite(12, LOW);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
pinMode(14, OUTPUT);
digitalWrite(14, LOW);
pinMode(15, OUTPUT);
digitalWrite(15, LOW);
pinMode(16, OUTPUT);
digitalWrite(16, LOW);
pinMode(17, OUTPUT);
digitalWrite(17, LOW);
}
void loop() {
// Read the key pressed
char key = keypad.getKey();
// Check if the key '1' is pressed
if (key == '1') {
digitalWrite(2, HIGH); // Turn on the LED when '1' is pressed
} else {
digitalWrite(2, LOW); // Turn off the LED for any other key
}
if (key == '2') {
digitalWrite(3, HIGH); // Turn on the LED when '2' is pressed
} else {
digitalWrite(3, LOW); // Turn off the LED for any other key
} if (key == '3') {
digitalWrite(4, HIGH); // Turn on the LED when '3' is pressed
} else {
digitalWrite(4, LOW); // Turn off the LED for any other key
} if (key == '4') {
digitalWrite(5, HIGH); // Turn on the LED when '4' is pressed
} else {
digitalWrite(5, LOW); // Turn off the LED for any other key
} if (key == '5') {
digitalWrite(6, HIGH); // Turn on the LED when '5' is pressed
} else {
digitalWrite(6, LOW); // Turn off the LED for any other key
} if (key == '6') {
digitalWrite(7, HIGH); // Turn on the LED when '6' is pressed
} else {
digitalWrite(7, LOW); // Turn off the LED for any other key
} if (key == '7') {
digitalWrite(8, HIGH); // Turn on the LED when '7' is pressed
} else {
digitalWrite(8, LOW); // Turn off the LED for any other key
} if (key == '8') {
digitalWrite(9, HIGH); // Turn on the LED when '8' is pressed
} else {
digitalWrite(9, LOW); // Turn off the LED for any other key
} if (key == '9') {
digitalWrite(10, HIGH); // Turn on the LED when '9' is pressed
} else {
digitalWrite(10, LOW); // Turn off the LED for any other key
} if (key == '0') {
digitalWrite(11, HIGH); // Turn on the LED when '0' is pressed
} else {
digitalWrite(11, LOW); // Turn off the LED for any other key
} if (key == 'A') {
digitalWrite(12, HIGH); // Turn on the LED when 'A' is pressed
} else {
digitalWrite(12, LOW); // Turn off the LED for any other key
} if (key == 'B') {
digitalWrite(13, HIGH); // Turn on the LED when 'B' is pressed
} else {
digitalWrite(13, LOW); // Turn off the LED for any other key
} if (key == 'C') {
digitalWrite(14, HIGH); // Turn on the LED when 'C' is pressed
} else {
digitalWrite(14, LOW); // Turn off the LED for any other key
} if (key == 'D') {
digitalWrite(15, HIGH); // Turn on the LED when 'D' is pressed
} else {
digitalWrite(15, LOW); // Turn off the LED for any other key
} if (key == '#') {
digitalWrite(16, HIGH); // Turn on the LED when '#' is pressed
} else {
digitalWrite(16, LOW); // Turn off the LED for any other key
} if (key == '*') {
digitalWrite(17, HIGH); // Turn on the LED when '*' is pressed
} else {
digitalWrite(17, LOW); // Turn off the LED for any other key
}
}