#include "U8glib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST); // Fast I2C / TWI
const int ledPins[] = {3, 4, 5, 6, 7, 8}; // LED pins
const int buttonPin = 2; // Button connected to D2
const int numLeds = sizeof(ledPins) / sizeof(ledPins[0]);
const int flashDuration = 10000; // Total duration to run everything (10 seconds)
const int progressDuration = 9000; // Duration for the progress bar to fill (9 seconds)
const int flashInterval = 200; // Flash interval (0.2 seconds)
const int updateInterval = 50; // Update interval for smoother progress
bool isLedsOn = false; // To track if LEDs are currently flashing
int progress = 0; // Progress for the display
void setup() {
// Set all LED pins as outputs
for (int i = 0; i < numLeds; i++) {
pinMode(ledPins[i], OUTPUT);
}
// Set the button pin as input with internal pull-up
pinMode(buttonPin, INPUT_PULLUP);
u8g.setFont(u8g_font_tpssb);
u8g.setColorIndex(1); // Set to white (default for U8glib)
}
void loop() {
// Read the button state
bool currentButtonState = digitalRead(buttonPin);
// Check if the button is pressed
if (currentButtonState == LOW && !isLedsOn) { // Button pressed and LEDs are not flashing
isLedsOn = true; // Set the flag to indicate LEDs are flashing
unsigned long startTime = millis(); // Record the start time
unsigned long lastUpdateTime = startTime; // Time of last display update
while (millis() - startTime < flashDuration) { // Loop for the total duration
// Calculate elapsed time
unsigned long elapsedTime = millis() - startTime;
// Update progress for the display (fills in 9 seconds)
if (elapsedTime < progressDuration) {
progress = (elapsedTime * 108) / progressDuration; // Calculate progress
} else {
progress = 108; // Fill the progress bar completely after 9 seconds
}
// Turn on all LEDs for the flash interval
for (int i = 0; i < numLeds; i++) {
digitalWrite(ledPins[i], HIGH);
}
delay(flashInterval); // Wait for 0.2 seconds
// Turn off all LEDs
for (int i = 0; i < numLeds; i++) {
digitalWrite(ledPins[i], LOW);
}
delay(flashInterval); // Wait for 0.2 seconds
// Update the display every updateInterval milliseconds for smoother progress
if (millis() - lastUpdateTime >= updateInterval) {
u8g.firstPage();
do {
u8g.drawStr(25, 50, "Progress Bar");
u8g.drawFrame(0, 10, 128, 20);
u8g.drawBox(10, 15, progress, 10);
} while (u8g.nextPage());
lastUpdateTime = millis(); // Update the last update time
}
}
// After 10 seconds, turn off all LEDs
for (int i = 0; i < numLeds; i++) {
digitalWrite(ledPins[i], LOW);
}
// Display "Waaagh!" after progress
u8g.firstPage();
do {
// Clear the display and show the message
u8g.setColorIndex(1); // Set color to white for the text
u8g.drawStr(25, 50, "Waaagh!"); // Display "Waaagh!"
} while (u8g.nextPage());
// Clear the display
delay(2000); // Display the message for 2 seconds before clearing
u8g.firstPage();
do {
// Do nothing to create a blank screen
} while (u8g.nextPage());
isLedsOn = false; // Reset the flag
progress = 0; // Reset progress
}
}