#include <Adafruit_SSD1306.h>
#include <Arduino.h>
#include <SPI.h>
#include <Wire.h>
#include <array>
#include <Adafruit_GFX.h>
#define TYPING_BUTTON 15
#define BACKSPACE_BUTTON 4
#define POSITION_BUTTON 17
#define BUTTON_DELAY 250
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3c ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
char text[] = "0000000000";
unsigned int pos = 0;
unsigned long next_pos_time;
const byte maxPos = sizeof text - 1;
bool typing = false;
void displayText(const char *c) {
oled.clearDisplay();
oled.setTextColor(SSD1306_WHITE);
oled.setCursor(0, 50);
oled.setTextSize(2);
oled.print(c);
oled.display();
}
bool is_button_pressed(uint8_t pin) {
return digitalRead(pin) == LOW;
}
void setup() {
Serial.begin(115200);
delay(2000);
pinMode(TYPING_BUTTON, INPUT_PULLUP);
pinMode(BACKSPACE_BUTTON, INPUT_PULLUP);
pinMode(POSITION_BUTTON, INPUT_PULLUP);
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("SSD1306 allocation failed");
return;
}
oled.clearDisplay();
oled.display();
}
int i;
void loop() {
oled.setCursor(0, 0);
displayText(text);
if (typing && next_pos_time <= millis()) {
typing = false;
pos++;
}
if (is_button_pressed(BACKSPACE_BUTTON)) {
if (typing) {
typing = false;
text[pos] = '/';
} else if (pos>0) {
pos--;
}
text[pos] = '/';
delay(BUTTON_DELAY);
}
if (is_button_pressed(TYPING_BUTTON)) {
if (text[pos] == '9') text[pos] = '/';
text[pos]++;
next_pos_time = millis() + 1250;
typing = true;
delay(BUTTON_DELAY);
}
}