#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include <WiFiUdp.h>
// Constants for OLED display
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define BUTTON_PIN_1 33 // Example button pin 1
#define BUTTON_PIN_2 25 // Example button pin 2
int selectedMenuItem = 0;
int selectedSubMenuItem = 0;
void setup() {
Serial.begin(115200);
// Initialize OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;) {
// Don't proceed, loop forever
}
}
pinMode(BUTTON_PIN_1, INPUT_PULLUP);
pinMode(BUTTON_PIN_2, INPUT_PULLUP);
display.clearDisplay();
display.setTextSize(1); // Set text size to 2 (adjust as needed)
display.setTextColor(SSD1306_WHITE);
// Initial menu
displayMenu();
}
void loop() {
// Check button 1 for navigation
if (digitalRead(BUTTON_PIN_1) == LOW) {
if (selectedSubMenuItem == 0) {
selectedMenuItem = (selectedMenuItem + 1) % 3; // Number of main menu items
} else {
selectedSubMenuItem = (selectedSubMenuItem + 1) % 5; // Number of sub-menu items plus Back
}
displayMenu();
delay(500); // Debounce delay
}
// Check button 2 for submenu or action
if (digitalRead(BUTTON_PIN_2) == LOW) {
if (selectedSubMenuItem == 0) {
// Enter submenu
selectedSubMenuItem = 1;
} else if (selectedSubMenuItem == 5) {
// Go back to main menu
selectedSubMenuItem = 0;
} else {
// Perform action or go back to the main menu
selectedSubMenuItem = 0;
}
displayMenu();
delay(500); // Debounce delay
}
}
void displayMenu() {
display.clearDisplay();
display.setCursor(0, 0);
if (selectedSubMenuItem == 0) {
display.print("Settings");
display.setCursor(0, 20); // Adjust vertical position
if (selectedMenuItem == 0) {
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Invert text color for selected item
}
display.println("1 - Temperature");
display.setTextColor(SSD1306_WHITE);
if (selectedMenuItem == 1) {
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Invert text color for selected item
}
display.println("2 - Schedule");
display.setTextColor(SSD1306_WHITE);
if (selectedMenuItem == 2) {
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Invert text color for selected item
}
display.println("3 - About");
display.setTextColor(SSD1306_WHITE);
display.display();
} else {
displaySubMenu();
}
}
void displaySubMenu() {
display.setCursor(0, 0);
switch (selectedMenuItem) {
case 0:
display.print("Temperature");
display.setCursor(0, 20); // Adjust vertical position
if (selectedSubMenuItem == 1) {
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Invert text color for selected item
}
display.println("- Set Temperature");
display.setTextColor(SSD1306_WHITE);
if (selectedSubMenuItem == 2) {
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Invert text color for selected item
}
display.println("- Temperature Units");
display.setTextColor(SSD1306_WHITE);
if (selectedSubMenuItem == 3) {
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Invert text color for selected item
}
display.println("- Back");
display.setTextColor(SSD1306_WHITE);
break;
case 1:
display.print("Schedule");
display.setCursor(0, 20); // Adjust vertical position
if (selectedSubMenuItem == 1) {
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Invert text color for selected item
}
display.println("- Set Schedule");
display.setTextColor(SSD1306_WHITE);
if (selectedSubMenuItem == 2) {
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Invert text color for selected item
}
display.println("- Edit Schedule");
display.setTextColor(SSD1306_WHITE);
if (selectedSubMenuItem == 3) {
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Invert text color for selected item
}
display.println("- Back");
display.setTextColor(SSD1306_WHITE);
break;
case 2:
display.println("About");
// Add About content here
break;
}
display.display();
}