#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// declare an SSD1306 display object connected to I2C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// variables to be used for pushbuttons
String stopwatch = "stop", start_pause = "low", sequence = "stop";
long start_time = 0, seconds = 0;
unsigned long startTime = 0;
unsigned long period = 10000; //LED turns of after 10 seconds
const int countdownDuration = 10; // Countdown duration in seconds
int remainingTime = countdownDuration - (millis() - startTime) / 1000; // Calculate remaining time
int LastremainingTime;
// Buttons
int buttonPin1 = 7; // Start
int buttonPin2 = 8; // Restart
int buttonPin3 = 9; // Stop
int buttonPin4 = 10; // Sequence 1 button
int buttonPin5 = 11; // Sequence 2 button
int buttonPin6 = 12; // Sequence 3 button
int buttonPin7 = 13; // Sequence 4 button
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0; // For Sequence 1 if button pressed
int buttonState5 = 0; // For Sequence 2 if button pressed
int buttonState6 = 0; // For Sequence 3 if button pressed
int buttonState7 = 0; // For Sequence 4 if button pressed
int lastButtonState1 = 0; // For recording state of button 4
int lastButtonState2 = 0; // For recording state of button 4
int lastButtonState3 = 0; // For recording state of button 4
int lastButtonState4 = 0; // For recording state of button 4
int lastButtonState5 = 0; // For recording state of button 5
int lastButtonState6 = 0; // For recording state of button 6
int lastButtonState7 = 0; // For recording state of button 7
long timeSeq1 = 0; // Start time of sequence 1, in seconds
long timeSeq2 = 10; // Start time of sequence 2, in seconds
long timeSeq3 = 20; // Start time of sequence 3, in seconds
long timeSeq4 = 30; // Start time of sequence 4, in seconds
long timeSeq5 = 40; // Start time of sequence 5, in seconds
long timeEnd = 50; // End time, 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(buttonPin1, INPUT); // Set button pin as input with internal pull-up resistor
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(buttonPin4, INPUT_PULLUP);
pinMode(buttonPin5, INPUT_PULLUP);
pinMode(buttonPin6, INPUT_PULLUP);
pinMode(buttonPin7, INPUT_PULLUP);
}
void loop()
{
buttonState1 = digitalRead(buttonPin1); // Read button state
if (buttonState1 != lastButtonState1 && buttonState1 == HIGH)
{ // If button state changed from HIGH to LOW
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE, BLACK);
display.setCursor(0, 0);
display.println("Sequence 1");
display.display();
for (int i = 10; i >= 0; i--) {
display.setCursor(0, 20);
display.println("Countdown: ");
display.println(String(i) + " ");
display.print(" ");
display.display();
delay(1000);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE, BLACK);
display.setCursor(0, 0);
display.println("Sequence 2");
display.display();
for (int i = 10; i >= 0; i--) {
display.setCursor(0, 20);
display.println("Countdown: ");
display.println(String(i) + " ");
display.print(" ");
display.display();
delay(1000);
display.fillRect(0, 20, 128, 12, SSD1306_BLACK);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE, BLACK);
display.setCursor(0, 0);
display.println("Sequence 3");
display.display();
for (int i = 10; i >= 0; i--) {
display.setCursor(0, 20);
display.println("Countdown: ");
display.println(String(i) + " ");
display.print(" ");
display.display();
delay(1000);
display.fillRect(0, 20, 128, 12, SSD1306_BLACK);
}
lastButtonState1 = buttonState1;
}
buttonState4 = digitalRead(buttonPin4); // Read button state
if (buttonState4 != lastButtonState4 && buttonState4 == 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
}
lastButtonState4 = buttonState4; // 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 remaining time
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
}
}
buttonState5 = digitalRead(buttonPin5); // Read button state
if (buttonState5 != lastButtonState5 && buttonState5 == 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 2"); // Display message
display.println("Countdown:"); // Display message
display.display(); // Show message
}
lastButtonState5 = buttonState5; // 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 remaining time
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 2"); // Display message
display.println("Countdown done!"); // Display message
display.display(); // Show message
}
}
buttonState6 = digitalRead(buttonPin6); // Read button state
if (buttonState6 != lastButtonState6 && buttonState6 == 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 3"); // Display message
display.println("Countdown:"); // Display message
display.display(); // Show message
}
lastButtonState6 = buttonState6; // 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 remaining time
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 3"); // Display message
display.println("Countdown done!"); // Display message
display.display(); // Show message
}
}
buttonState7 = digitalRead(buttonPin7); // Read button state
if (buttonState7 != lastButtonState7 && buttonState7 == 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 4"); // Display message
display.println("Countdown:"); // Display message
display.display(); // Show message
}
lastButtonState7 = buttonState7; // 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 remaining time
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 4"); // Display message
display.println("Countdown done!"); // Display message
display.display(); // Show message
}
}
}