#include <Adafruit_SSD1306.h> // OLED display library
#define OLED_RESET 10 // OLED reset pin
Adafruit_SSD1306 display(OLED_RESET); // Create display object
const int buttonPin = 10; // Button pin
int buttonState = 0; // Current button state
int lastButtonState = 0; // Last button state
unsigned long startTime = 0; // Start time for countdown
const int countdownDuration = 10; // Countdown duration in seconds
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Initialize display
display.clearDisplay(); // Clear display
display.setTextColor(WHITE, BLACK); // Set text color
display.setTextSize(1); // Set text size
display.setCursor(0,0); // Set cursor position
display.println("Press button"); // Display message
display.println("to start timer"); // Display message
display.display(); // Show message
pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with internal pull-up resistor
}
void loop() {
buttonState = digitalRead(buttonPin); // Read button state
if (buttonState != lastButtonState && buttonState == LOW) { // If button state changed from HIGH to LOW
startTime = millis(); // Record start time
display.clearDisplay(); // Clear display
display.setCursor(0,0); // Set cursor position
display.println("Sequence 1"); // Display message
display.println("Countdown:"); // Display message
display.display(); // Show message
}
lastButtonState = buttonState; // Update last button state
if (startTime > 0) { // If countdown has started
int remainingTime = countdownDuration - (millis() - startTime) / 1000; // Calculate remaining time
if (remainingTime >= 0) { // If countdown not over
display.setCursor(0, 20); // Set cursor position
display.print(remainingTime); // Display remaining time
display.print(" "); // Display message
display.display(); // Show message
} else { // If countdown over
startTime = 0; // Reset start time
display.clearDisplay(); // Clear display
display.setCursor(0,0); // Set cursor position
display.println("Sequence 1"); // Display message
display.println("Countdown done!"); // Display message
display.display(); // Show message
}
}
}