#include <Keypad.h>
#include <SevSeg.h>
// Keypad setup
const byte ROWS = 4; // Number of rows
const byte COLS = 4; // Number of columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// 7-Segment Display setup
SevSeg sevseg;
char displayBuffer[5] = " "; // Holds 4 digits plus null terminator
int bufferIndex = 0;
void setup() {
// SevSeg setup
byte numDigits = 4;
byte digitPins[] = {10, 11, 12, 13}; // Pins for the digits
byte segmentPins[] = {A0, A1, A2, A3, A4, A5, 0}; // Pins for the segments
bool resistorsOnSegments = true; // 'true' means resistors are on segment pins
bool updateWithDelays = false;
bool leadingZeros = false;
bool disableDecPoint = true;
sevseg.begin(COMMON_ANODE, numDigits, digitPins, segmentPins, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint);
sevseg.setBrightness(90); // Set display brightness
}
void loop() {
char key = keypad.getKey();
if (key) {
// Handle key press
if (key >= '0' && key <= '9') {
// Add digit to buffer
if (bufferIndex < 4) {
displayBuffer[bufferIndex] = key;
bufferIndex++;
}
} else if (key == '*') {
// Clear buffer
for (int i = 0; i < 4; i++) {
displayBuffer[i] = ' ';
}
bufferIndex = 0;
}
// Display the buffer
sevseg.setChars(displayBuffer);
}
sevseg.refreshDisplay(); // Must be called repeatedly
}