#include <Keypad.h>
class KeypadHandler {
private:
static const uint8_t ROWS = 4;
static const uint8_t COLS = 4;
char keymap[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
Keypad* keypad;
uint8_t *rowPins;
uint8_t *colPins;
public:
KeypadHandler(uint8_t *rowPins, uint8_t *colPins)
: rowPins(rowPins), colPins(colPins) {}
void init() {
keypad = new Keypad(makeKeymap(keymap), rowPins, colPins, ROWS, COLS);
}
char getKey() {
return keypad->getKey();
}
};
uint8_t colPins[KeypadHandler::COLS] = { 5, 4, 3, 2 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[KeypadHandler::ROWS] = { 9, 8, 7, 6 }; // Pins connected to R1, R2, R3, R4
KeypadHandler keypadHandler(rowPins, colPins);
void setup() {
Serial.begin(9600);
keypadHandler.init();
}
void loop() {
char key = keypadHandler.getKey();
if (key != NO_KEY) {
Serial.println(key);
}
}
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}