#include <Wire.h>
#include <U8g2lib.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C // I2C address for the OLED
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset= */ OLED_RESET);
unsigned long startTime = 0;
const unsigned long duration = 30000; // 30 seconds in milliseconds
void setup() {
// Initialize the OLED display
u8g2.begin();
// Start the timer
startTime = millis();
}
void loop() {
// Calculate remaining time
unsigned long currentTime = millis();
unsigned long elapsedTime = currentTime - startTime;
unsigned long remainingTime = max(0, duration - elapsedTime);
unsigned int secondsRemaining = remainingTime / 1000;
// Calculate progress
float progress = (float)elapsedTime / duration;
// Ensure progress does not exceed 1.0
progress = min(progress, 1.0);
// Display on the OLED
u8g2.firstPage();
do {
// Display "RC LUDO FUEL PUMP" in small font, centered
u8g2.setFont(u8g2_font_profont10_tf);
u8g2.drawStr(2, 7, "RC LUDO FUEL PUMP");
// Display "FILL START" in medium font, centered
u8g2.setFont(u8g2_font_profont22_tf);
int textWidth = u8g2.getStrWidth("FILL START");
u8g2.drawStr((SCREEN_WIDTH - textWidth) / 2, 28, "FILL START");
// Display the remaining seconds on the second line
u8g2.setFont(u8g2_font_profont22_tf);
char secondsString[15]; // Adjusted size to accommodate the largest possible string
snprintf(secondsString, sizeof(secondsString), "Timer %us", (unsigned int)secondsRemaining);
textWidth = u8g2.getStrWidth(secondsString);
u8g2.drawStr((SCREEN_WIDTH - textWidth) / 2, 47, secondsString);
// Draw the progress bar frame
u8g2.drawFrame(10, 54, SCREEN_WIDTH - 20, 10);
// Draw the progress bar
int barWidth = (SCREEN_WIDTH - 20 - 2) * progress;
u8g2.drawBox(12, 56, barWidth, 6);
} while (u8g2.nextPage());
// If 30 seconds elapsed, reset the timer
if (elapsedTime >= duration) {
startTime = currentTime;
}
// Update every 100 milliseconds
delay(100);
}