#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {13, 12, 27, 26}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {25, 33, 32, 34}; //connect to the column pinouts of the keypad
void setup() {
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
// display nilai awal (0)
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(30, 7);
display.print("Program Keypad");
display.display();
pinMode(rowPins[0], INPUT_PULLUP);
pinMode(rowPins[1], INPUT_PULLUP);
pinMode(rowPins[2], INPUT_PULLUP);
pinMode(rowPins[3], INPUT_PULLUP);
pinMode(colPins[0], OUTPUT);
pinMode(colPins[1], OUTPUT);
pinMode(colPins[2], OUTPUT);
pinMode(colPins[3], OUTPUT);
}
void loop() {
// scan each column of the keypad
for (byte col = 0; col < COLS; col++) {
digitalWrite(colPins[col], LOW);
// check each row of the keypad
for (byte row = 0; row < ROWS; row++) {
if (digitalRead(rowPins[row]) == LOW) {
// key pressed, print to OLED display
display.clearDisplay();
display.setCursor(0, 0);
display.print("Key Pressed:");
display.setCursor(0, 10);
display.print(keys[row][col]);
display.display();
delay(500); // debounce
}
}
digitalWrite(colPins[col], HIGH);
}
}