#include <Arduino.h>
#include <U8glib.h>
#include <U8g2lib.h>
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE); // Set the appropriate OLED display type
#define NUM_MENU_ITEMS 5
const char *menuItems[NUM_MENU_ITEMS] = {"Time", "Date", "Games", "Music", "Radio"};
int selectedMenuItem = 0;
bool inSubMenu = false;
int upButton = 2; // Pin for the up button
int downButton = 3; // Pin for the down button
int backButton = 4; // Pin for the back button
int selectButton = 5; // Pin for the select button
int scrollOffset = 0;
byte hours = 0;
byte minutes = 0;
byte seconds = 0;
const int PIN_BUTTON_MODE = 3;
const int PIN_BUTTON_SET = 2;
const int BUTTON_MODE_DEBOUNCE_TIME = 250;
const int BUTTON_SET_DEBOUNCE_TIME = 10;
const int MODE_SHOW_TIME = 0;
const int MODE_SET_SECONDS = 3;
const int MODE_SET_MINUTES = 2;
const int MODE_SET_HOURS = 1;
unsigned long elapsedButtonModeMillis = 0;
unsigned long previousButtonModeMillis = 0;
unsigned long elapsedButtonSetMillis = 0;
unsigned long previousButtonSetMillis = 0;
char timeString[9];
unsigned long currentMillis = 0;
int elapsedTimeUpdateMillis = 0;
unsigned long previousTimeUpdateMillis = 0;
float percentageOfSecondElapsed = 0;
byte currentMode = MODE_SHOW_TIME;
U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
void drawMenu() {
u8g.setFont(u8g_font_profont15);
for (int i = 0; i < 3; i++) {
int index = (scrollOffset + i) % NUM_MENU_ITEMS;
if (index == selectedMenuItem) {
u8g.drawStr(0, (i + 1) * 20, ("> " + String(menuItems[index])).c_str());
} else {
u8g.drawStr(0, (i + 1) * 20, (" " + String(menuItems[index])).c_str());
}
}
}
void drawSelectedMenu() {
u8g.setFont(u8g_font_profont15);
u8g.drawStr(0, 20, "Selected Item:");
u8g.drawStr(0, 40, menuItems[selectedMenuItem]);
}
// Functions corresponding to menu items
void timeFunction() {
// Replace this with the functionality for the "Time" menu item
Serial.println("Time menu item selected");
}
void dateFunction() {
// Replace this with the functionality for the "Date" menu item
Serial.println("Date menu item selected");
}
void gamesFunction() {
// Replace this with the functionality for the "Games" menu item
Serial.println("Games menu item selected");
}
void musicFunction() {
// Replace this with the functionality for the "Music" menu item
Serial.println("Music menu item selected");
}
void radioFunction() {
// Replace this with the functionality for the "Radio" menu item
Serial.println("Radio menu item selected");
}
void setup() {
u8g.begin();
pinMode(upButton, INPUT_PULLUP);
pinMode(downButton, INPUT_PULLUP);
pinMode(backButton, INPUT_PULLUP);
pinMode(selectButton, INPUT_PULLUP);
Serial.begin(115200); // Initialize serial communication for debugging
}
void loop() {
u8g.firstPage();
do {
if (!inSubMenu) {
drawMenu();
} else {
if (selectedMenuItem == 0) { // "Time" menu item
// Call the time display loop function
timeDisplayLoop();
} else {
drawSelectedMenu();
}
}
} while (u8g.nextPage());
if (digitalRead(upButton) == LOW) {
delay(200); // Debounce delay
selectedMenuItem = (selectedMenuItem - 1 + NUM_MENU_ITEMS) % NUM_MENU_ITEMS;
scrollOffset = selectedMenuItem;
}
if (digitalRead(downButton) == LOW) {
delay(200); // Debounce delay
selectedMenuItem = (selectedMenuItem + 1) % NUM_MENU_ITEMS;
scrollOffset = selectedMenuItem - 2;
if (scrollOffset < 0) {
scrollOffset = 0;
}
}
if (digitalRead(backButton) == LOW) {
delay(200); // Debounce delay
inSubMenu = false;
}
if (digitalRead(selectButton) == LOW) {
delay(200); // Debounce delay
if (!inSubMenu) {
// Enter submenu and call the corresponding function for the selected item
inSubMenu = true;
switch (selectedMenuItem) {
case 0:
timeFunction();
break;
case 1:
dateFunction();
break;
case 2:
gamesFunction();
break;
case 3:
musicFunction();
break;
case 4:
radioFunction();
break;
}
} else {
// Exit submenu and go back to the main menu
inSubMenu = false;
}
}
delay(50); // Delay to reduce button reading frequency
}
void timeDisplayLoop() {
// Call the loop function from the second sketch
loop();
}