#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
#define BUTTON_UP_PIN 13
#define BUTTON_DOWN_PIN 4
#define BUTTON_SELECT_PIN 5
// Define menu options
#define MENU_SIZE 2
const int menuValues[MENU_SIZE] = {120, 60}; // Voltage values in volts
const char* menuLabels[MENU_SIZE] = {"120V", "60V"};
int selectedValueIndex = 0; // Index of the currently selected value
int selectedVoltage = 0;
void setup() {
Serial.begin(115200);
pinMode(BUTTON_UP_PIN, INPUT_PULLUP);
pinMode(BUTTON_DOWN_PIN, INPUT_PULLUP);
pinMode(BUTTON_SELECT_PIN, INPUT_PULLUP);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
delay(500);
display.clearDisplay(); // Clear the display once during setup
display.setTextColor(WHITE);
}
void loop() {
// Clear display before drawing each menu
display.clearDisplay();
// Display menu options and current selected value
display.setTextSize(2);
display.setCursor(0, 0);
for (int i = 0; i < MENU_SIZE; ++i) {
display.print(" ");
if (i == selectedValueIndex) {
display.print("> "); // Arrow indicating current selection
display.setTextColor(WHITE, BLACK); // Invert color for the selected item
} else {
display.print(" ");
display.setTextColor(WHITE);
}
display.print(menuLabels[i]);
display.println();
}
// Display currently set value at the bottom center
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(SCREEN_WIDTH / 4, SCREEN_HEIGHT - 10);
display.print("Selected: ");
display.print(selectedVoltage);
display.print("V");
display.display();
// Handle button presses
if (digitalRead(BUTTON_UP_PIN) == LOW) {
selectedValueIndex = (selectedValueIndex + 1) % MENU_SIZE; // Move to the next value
delay(200); // Button debounce delay
}
if (digitalRead(BUTTON_DOWN_PIN) == LOW) {
selectedValueIndex = (selectedValueIndex + MENU_SIZE - 1) % MENU_SIZE; // Move to the previous value
delay(200); // Button debounce delay
}
if (digitalRead(BUTTON_SELECT_PIN) == LOW) {
// Perform action based on the selected value
int selectedValue = menuValues[selectedValueIndex];
// Do something with the selected value (e.g., save it to a variable)
Serial.print("Selected value: ");
Serial.println(selectedValue);
selectedVoltage = selectedValue;
delay(200); // Button debounce delay
}
}