#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h> // library required for I2C communication
#define TASTER_PIN 2 // Define the pin number for the button
#define DEBOUNCE_DELAY 50 // Define the debounce delay time in milliseconds
#define ANIMATION_DURATION 10000 // Define the duration of the animation in milliseconds
#define LAST_DISPLAY_DURATION 2000 // Define the duration of the last display in milliseconds
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); // initialization for the used OLED display
// images from https://lopaka.app/
static const unsigned char image_Alert_9x8_bits[] U8X8_PROGMEM = {0x10,0x00,0x38,0x00,0x28,0x00,0x6c,0x00,0x6c,0x00,0xfe,0x00,0xee,0x00,0xff,0x01};
static const unsigned char logo_bitmap[] U8X8_PROGMEM = { /* Your logo bitmap data */ };
static const unsigned char logo_width = 128; // Width of the logo in pixels
static const unsigned char logo_height = 64; // Height of the logo in pixels
int progress = 0; // progress of the progressbar
char buffer[32]; // helper buffer to construct a string to be displayed
bool animationRunning = false; // flag to indicate if animation is running
unsigned long animationStartTime = 0; // variable to store the start time of the animation
bool lastDisplayShowing = false; // flag to indicate if the last display is showing
unsigned long lastDisplayStartTime = 0; // variable to store the start time of the last display
void setup(void) {
pinMode(TASTER_PIN, INPUT_PULLUP); // Set the button pin as input with internal pull-up resistor
u8g2.begin(); // start the u8g2 library
}
void loop(void) {
if (!animationRunning && !lastDisplayShowing) { // If animation is not running and the last display is not showing
if (digitalRead(TASTER_PIN) == LOW) { // Check if button is pressed
startAnimation(); // Start the animation
} else {
u8g2.setPowerSave(1); // Turn off the display
}
}
if (animationRunning) { // If animation is running
updateAnimation(); // Update the animation
}
if (lastDisplayShowing) { // If the last display is showing
updateLastDisplay(); // Update the last display
}
}
void startAnimation() {
animationRunning = true; // Set animation flag to true
animationStartTime = millis(); // Record the start time of the animation
u8g2.setPowerSave(0); // Turn on the display
}
void updateAnimation() {
unsigned long currentTime = millis(); // Get the current time
// Check if animation duration has passed
if (currentTime - animationStartTime >= ANIMATION_DURATION) {
animationRunning = false; // Stop the animation
lastDisplayShowing = true; // Start showing the last display
lastDisplayStartTime = currentTime; // Record the start time of the last display
progress = 100; // Set progress to 100%
return;
}
u8g2.clearBuffer(); // clear the internal memory
// code from https://lopaka.app/
u8g2.setBitmapMode(1);
u8g2.drawFrame(12, 21, 104, 20);
u8g2.drawBox(14, 23, progress, 16); // draw the progressbar fill
u8g2.setFont(u8g2_font_helvB08_tr);
sprintf(buffer, "Progress: %d%%", progress); // construct a string with the progress variable
u8g2.drawStr(33, 53, buffer); // display the string
u8g2.setFont(u8g2_font_haxrcorp4089_tr);
u8g2.drawStr(0, 7, "DEPOLYMERIZATION");
u8g2.drawLine(0, 9, 127, 9);
u8g2.drawXBMP( 22, 45, 9, 8, image_Alert_9x8_bits);
u8g2.sendBuffer(); // transfer internal memory to the display
// increase the progress value to go over 0-100
progress = (currentTime - animationStartTime) * 100 / ANIMATION_DURATION;
}
void updateLastDisplay() {
unsigned long currentTime = millis(); // Get the current time
// Check if last display duration has passed
if (currentTime - lastDisplayStartTime >= LAST_DISPLAY_DURATION) {
lastDisplayShowing = false; // Stop showing the last display
u8g2.clearBuffer(); // Clear the display buffer
u8g2.setPowerSave(1); // Turn off the display
delay(1000); // Delay to allow user to see the cleared display
progress = 0; // Reset progress to 0%
return;
}
// Display the last display
u8g2.clearBuffer(); // clear the internal memory
u8g2.setBitmapMode(1);
u8g2.drawFrame(12, 21, 104, 20);
u8g2.drawBox(14, 23, 100, 16); // draw the progressbar fill to 100%
u8g2.setFont(u8g2_font_helvB08_tr);
u8g2.drawStr(33, 53, "Progress: 100%"); // display "Progress: 100%"
u8g2.setFont(u8g2_font_haxrcorp4089_tr);
u8g2.drawStr(0, 7, "DEPOLYMERIZATION");
u8g2.drawLine(0, 9, 127, 9);
u8g2.drawXBMP( 22, 45, 9, 8, image_Alert_9x8_bits);
u8g2.sendBuffer(); // transfer internal memory to the display
}