#include <Keypad.h>
const byte ROWS = 4; // Number of rows in the keypad
const byte COLS = 4; // Number of columns in the keypad
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
// Connect keypad ROW0, ROW1, ROW2, and ROW3 to these ESP32 pins.
byte rowPins[ROWS] = {32, 33, 25, 26}; // Connect to the row pinouts of the keypad
// Connect keypad COL0, COL1, COL2, and COL3 to these ESP32 pins.
byte colPins[COLS] = {27, 14, 12, 13}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Pin connected to the LED
const int ledPin = 2; // Adjust pin number as per your setup
// Variable to store the LED state
volatile bool ledState = LOW;
// Interrupt flag
volatile bool interruptFlag = false;
void IRAM_ATTR isr() {
interruptFlag = true;
}
void setup() {
// Set LED pin as an output
pinMode(ledPin, OUTPUT);
// Initialize keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Attach interrupt to pin connected to the keypad "D" key
attachInterrupt(digitalPinToInterrupt(colPins[3]), isr, FALLING);
// Initialize serial communication
Serial.begin(9600);
}
void loop() {
// Check if interrupt flag is set
char key = keypad.getKey();
Serial.println(key);
if (interruptFlag) {
// Toggle LED state
ledState = !ledState;
// Update LED
digitalWrite(ledPin, ledState);
// Print a message to Serial Monitor
Serial.println("LED Toggled");
delay(100); // Debounce delay to prevent multiple detections for one press
// Reset interrupt flag
interruptFlag = false;
}
}