#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_ADDR 0x3C
#define OLED_SDA 21
#define OLED_SCL 22
const int buttonRightPin = 5;
const int buttonLeftPin = 18;
const int buttonCenterPin = 19;
const unsigned long debounceDelay = 50; // Tiempo de espera para eliminar rebotes
// Variables para el debounce de cada botón
unsigned long lastDebounceTimeRight = 0;
unsigned long lastDebounceTimeLeft = 0;
unsigned long lastDebounceTimeCenter = 0;
int buttonRightState = HIGH;
int buttonLeftState = HIGH;
int buttonCenterState = HIGH;
int val = 0;
Adafruit_SSD1306 display(128, 64, &Wire, OLED_ADDR);
const int scrollSpeed = 100; // Velocidad del scroll en milisegundos
void apagado() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("");
display.display();
}
void hora() {
display.clearDisplay();
display.setTextSize(3);
display.setTextColor(SSD1306_WHITE);
display.setCursor(15, 20);
display.println("10:30");
display.display();
}
void mensaje() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Holaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
display.display();
}
void mensaje2() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Hola");
display.display();
}
bool checkButtonState(int buttonPin, int& buttonState, unsigned long& lastDebounceTime) {
int reading = digitalRead(buttonPin);
if (reading != buttonState) {
unsigned long currentMillis = millis();
if (currentMillis - debounceDelay >= lastDebounceTime) {
lastDebounceTime = currentMillis;
buttonState = reading;
if (buttonState == LOW) {
return true;
}
}
}
return false;
}
void setup() {
Serial.begin(115200);
pinMode(buttonRightPin, INPUT_PULLUP);
pinMode(buttonLeftPin, INPUT_PULLUP);
pinMode(buttonCenterPin, INPUT_PULLUP);
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
// delay(2000); // Delay inicial antes de iniciar el scroll
}
void loop() {
// Verificar el botónRight
if (checkButtonState(buttonRightPin, buttonRightState, lastDebounceTimeRight)) {
if (val == 3) {
val = 2;
} else {
val++;
}
}
// Verificar el botónLeft
if (checkButtonState(buttonLeftPin, buttonLeftState, lastDebounceTimeLeft)) {
if (val <= 1) {
val = 1;
} else {
val--;
}
}
// Verificar el botónCenter
if (checkButtonState(buttonCenterPin, buttonCenterState, lastDebounceTimeCenter)) {
if (val == 1) {
val = 0;
} else if (val == 0) {
val = 1;
}
else {
val = 1;
}
}
switch (val) {
case 0: {
apagado();
break;
}
case 1: {
hora();
break;
}
case 2: {
mensaje();
break;
}
case 3: {
mensaje2();
break;
}
}
}