#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define BUTTON_UP_DOWN 18 // Botón para mover el cursor
#define BUTTON_SELECT 19 // Botón para seleccionar la opción
int menuOption = 0;
const int totalOptions = 2;
bool lastButtonUpDownState = HIGH;
bool lastButtonSelectState = HIGH;
void setup() {
pinMode(BUTTON_UP_DOWN, INPUT_PULLUP);
pinMode(BUTTON_SELECT, INPUT_PULLUP);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.clearDisplay();
display.display();
}
void loop() {
// Leer estado del botón de subir/bajar
bool currentButtonUpDownState = digitalRead(BUTTON_UP_DOWN);
if (currentButtonUpDownState == LOW && lastButtonUpDownState == HIGH) {
menuOption++;
if (menuOption > totalOptions - 1) {
menuOption = 0;
}
delay(200); // Debounce
}
lastButtonUpDownState = currentButtonUpDownState;
// Leer estado del botón de seleccionar
bool currentButtonSelectState = digitalRead(BUTTON_SELECT);
if (currentButtonSelectState == LOW && lastButtonSelectState == HIGH) {
selectOption(menuOption);
delay(200); // Debounce
}
lastButtonSelectState = currentButtonSelectState;
// Mostrar menú
displayMenu();
}
void displayMenu() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
if (menuOption == 0) {
display.setCursor(0, 0);
display.print("> Opcion 1");
} else {
display.setCursor(10, 0);
display.print("Opcion 1");
}
if (menuOption == 1) {
display.setCursor(0, 10);
display.print("> Opcion 2");
} else {
display.setCursor(10, 10);
display.print("Opcion 2");
}
display.display();
}
void selectOption(int option) {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
switch(option) {
case 0:
display.setCursor(0, 0);
display.print("Opcion 1 seleccionada");
break;
case 1:
display.setCursor(0, 0);
display.print("Opcion 2 seleccionada");
break;
}
display.display();
delay(1000); // Pausa para ver la selección
}