#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SDA_PIN 9
#define SCL_PIN 10
#define POT_PIN 3
#define BUTTON_PIN 1
#define BUZZER_PIN 0
#define MENU_BUTTON_PIN 2
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int scaledValue = 0;
bool changing = false;
unsigned long lastButtonPressTime = 0;
const unsigned long debounceDelay = 200;
unsigned long lastBlinkTime = 0;
const unsigned long blinkInterval = 320;
bool displayBlinkState = false;
int menuLevel = 0;
int LastVolumeSaved = 0;
int timess = 0;
int lastint = 0;
int previousPotValue = -1; // Initialize with an out-of-bounds value
bool isInSleepMode = false; // Flag to indicate sleep mode
void setup() {
Serial.begin(115200);
delay(1000);
Wire.begin(SDA_PIN, SCL_PIN);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(MENU_BUTTON_PIN, INPUT_PULLUP);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("SSD1306 allocation failed");
for (;;);
}
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(2);
displayLoadingAnimation();
}
void loop() {
checkButton();
checkPotentiometer(); // Read potentiometer for sleep mode
updateDisplay();
timess += 1;
Serial.println(timess);
Serial.println(lastint);
sleepmode();
}
void sleepmode() {
if (lastint < timess - 50) {
if (!isInSleepMode) {
menuLevel = 0; // Reset to menu level 0
display.fillScreen(SSD1306_BLACK); // Fill the display with black
display.display(); // Update the display to show black screen
isInSleepMode = true; // Set sleep mode flag
}
} else {
isInSleepMode = false; // Exit sleep mode when conditions are met
}
}
void checkButton() {
if (isInSleepMode == false) {
if (digitalRead(BUTTON_PIN) == LOW && menuLevel == 2) {
if (millis() - lastButtonPressTime > debounceDelay) {
// Toggle the changing state on button press
changing = !changing;
Serial.print("State: ");
Serial.println(changing);
if (changing) {
// When enabling changing, save the current scaled value
LastVolumeSaved = scaledValue;
Serial.print("Saved Volume: ");
Serial.println(LastVolumeSaved);
}
lastButtonPressTime = millis();
}
}
// Reset lastint if button pressed
if (digitalRead(BUTTON_PIN) == LOW) {
lastint = timess;
}
if (digitalRead(MENU_BUTTON_PIN) == LOW) {
lastint = timess;
if (millis() - lastButtonPressTime > debounceDelay) {
menuLevel = (menuLevel + 1) % 3;
changing = false; // Reset changing when switching menu
Serial.print("State: ");
Serial.println(changing);
Serial.print("Menu Level: ");
Serial.println(menuLevel);
lastButtonPressTime = millis();
}
}
}
}
void checkPotentiometer() {
// Read the potentiometer value for sleep mode only
int potValue = analogRead(POT_PIN);
if (menuLevel != 2) { // Not in volume menu
// Here you can keep track of the potValue for sleep mode
// This is where you'd handle sleep mode logic if needed.
if (potValue != previousPotValue) {
previousPotValue = potValue; // Update the last potentiometer value
lastint = timess;
}
return; // Exit if in volume menu
}
// Update scaledValue only if changing is true
if (changing) {
scaledValue = map(potValue, 0, 4095, 0, 100);
}
}
void updateDisplay() {
if (isInSleepMode) {
return; // Do not update display in sleep mode
}
display.clearDisplay(); // Optional: clear the display
if (menuLevel == 0) {
displayDefaultMenu();
}
else if (menuLevel == 1) {
displayWoLMenu();
}
else {
displayVolumeMenu();
}
display.display();
}
void displayWoLMenu() {
display.setTextSize(2);
display.setCursor(15,SCREEN_HEIGHT / 2 - 18);
display.println(F("Ready to"));
display.setCursor(50,SCREEN_HEIGHT / 2 -1);
display.print(F("WoL"));
display.setTextSize(1);
display.setCursor(0,54);
display.print(F("Wake Menu"));
}
void displayVolumeMenu() {
int digitCount = (scaledValue == 0) ? 1 : (log10(scaledValue) + 1);
if (changing && millis() - lastBlinkTime >= blinkInterval) {
displayBlinkState = !displayBlinkState;
lastBlinkTime = millis();
if (!displayBlinkState) {
makeBuzzerSound(680, 200); // Call with specific note and duration
}
}
if (changing){
display.setTextSize(1);
display.setCursor(0,54);
display.print(F("Save Menu"));
} else {
display.setTextSize(1);
display.setCursor(0,54);
display.print(F("Change Menu"));
}
display.setTextSize(2);
display.setCursor(24, SCREEN_HEIGHT / 2 - 18);
display.println(F("Volume:"));
int valueWidth = digitCount * 12;
int totalWidth = valueWidth + 12;
int volumeX = (SCREEN_WIDTH - totalWidth) / 2;
display.setCursor(volumeX, SCREEN_HEIGHT / 2 - 2);
if (changing && displayBlinkState) {
for (int i = 0; i < digitCount; i++) {
display.setTextSize(2);
display.print("_"); // Print underscores for blinking
}
display.print("%");
} else {
display.setTextSize(2);
display.print(scaledValue);
display.print("%");
}
}
void displayDefaultMenu() {
display.clearDisplay(); // Clear the display for fresh drawing
// Simulating text size 1.8 for the welcome message
display.setTextSize(2); // Use size 2 for the welcome text
display.setCursor(SCREEN_WIDTH / 5.5, SCREEN_HEIGHT / 2 - 18); // Adjust vertical position
display.println(F("Welcome"));
display.setCursor(10, SCREEN_HEIGHT / 2 - 2); // Adjust vertical position
display.println(F("I am Echo"));
display.setTextSize(1); // Set text size to 1 for the menu indication
display.setCursor(0, 54);
display.print(F(" Menu")); // Menu indication
display.display(); // Refresh the display to show the changes
}
void makeBuzzerSound(int note, unsigned long duration) {
tone(BUZZER_PIN, note);
delay(duration);
noTone(BUZZER_PIN);
}
void playBuzzerSequence() {
const int notes[] = {180, 300, 400};
for (int i = 0; i < 2; i++) {
for (int note : notes) {
makeBuzzerSound(note, 100);
delay(200);
}
}
}
void displayLoadingAnimation() {
unsigned long startTime = millis();
const char* loadingTexts[] = { "Loading...", "Loading.", "Loading.." };
int loadingState = 0;
while (millis() - startTime < 10500) {
if (millis() % 800 < 100) {
loadingState = (loadingState + 1) % 3;
}
display.clearDisplay();
display.setCursor((SCREEN_WIDTH - strlen(loadingTexts[loadingState]) * 12) / 2, SCREEN_HEIGHT / 2 - 12);
display.println(loadingTexts[loadingState]);
display.display();
delay(100);
}
display.clearDisplay();
display.display();
playBuzzerSequence();
}
Loading
esp32-c3-devkitm-1
esp32-c3-devkitm-1