#include <Keypad.h>
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
// Define the keymap
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
// Define the keypad connection pins
byte rowPins[ROWS] = {19, 18, 5, 17}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {16, 4, 0, 2}; // Connect to the column pinouts of the keypad
// Create the Keypad object
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
// Define the pin connected to the LED
const int ledPin = 15;
void setup() {
Serial.begin(9600); // Initialize serial communication
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW); // Ensure LED is initially turned off
}
void loop() {
// Get the key from the keypad
char key = keypad.getKey();
// Check if a key is pressed
if (key != NO_KEY) {
// Print the pressed key
Serial.print("Key pressed: ");
Serial.println(key);
// Check which key is pressed
if (key == '1') {
digitalWrite(ledPin, HIGH); // Turn on the LED
Serial.println("LED turned ON");
} else if (key == '0') {
digitalWrite(ledPin, LOW); // Turn off the LED
Serial.println("LED turned OFF");
}
}
}