#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Inisialisasi OLED display
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Konfigurasi pin keypad
const byte ROWS = 4;
const byte COLS = 4;
const int debounceDelay = 50;
byte rowPins[ROWS] = {12, 13, 14, 15};
byte colPins[COLS] = {25, 26, 27, 33};
char keymap[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
bool caps_lock = false;
String text = "";
void setup() {
Serial.begin(115200);
// Inisialisasi display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
// Inisialisasi keypad
for (byte r = 0; r < ROWS; r++) {
pinMode(rowPins[r], INPUT_PULLUP);
}
for (byte c = 0; c < COLS; c++) {
pinMode(colPins[c], OUTPUT);
digitalWrite(colPins[c], HIGH);
}
}
void loop() {
char key = getKey();
if (key) {
Serial.println(key);
handleKey(key);
updateDisplay();
delay(debounceDelay);
}
}
char getKey() {
char key = '\0';
for (byte c = 0; c < COLS; c++) {
digitalWrite(colPins[c], LOW);
for (byte r = 0; r < ROWS; r++) {
if (digitalRead(rowPins[r]) == LOW) {
key = keymap[r][c];
}
}
digitalWrite(colPins[c], HIGH);
}
return key;
}
void handleKey(char key) {
if (key >= '0' && key <= '9') {
text += key;
} else if (key >= 'A' && key <= 'D') {
if (caps_lock) {
text += (key - 'A' + 'a');
} else {
text += key;
}
} else if (key == '*') {
caps_lock = !caps_lock;
} else if (key == '#') {
text += ' ';
} else if (key == '<') { // Anda dapat menambahkan lebih banyak simbol opsional di sini
text += '.';
}
// Anda dapat menambahkan fungsi untuk tombol Enter, Backspace, dll.
}
void updateDisplay() {
display.clearDisplay();
display.setCursor(0, 0);
display.print(text);
display.display();
}