#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>
#define OLED_RESET -1
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// declare an SH1106 display object connected to I2C
Adafruit_SH1106 display(OLED_RESET);
unsigned long startTime = 0;
unsigned long blinkTime1 = 0;
unsigned long blinkTime2 = 0;
unsigned long interval = 250;
const int countdownDuration = 10; // Countdown duration in seconds
int remainingTime = countdownDuration - (millis() - startTime) / 1000; // Calculate remaining time
int LastremainingTime;
int seq = 1;
int seconds;
long start_time = 0;
String sequence = "stop";
// 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
int OLEDState; //OLED
void setup() {
display.begin(SH1106_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);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
pinMode(buttonPin6, INPUT);
pinMode(buttonPin7, INPUT);
}
void loop()
{
button4();
button5();
button6();
button7();
buttonState1 = digitalRead(buttonPin1); // Read button state
if (buttonState1 != lastButtonState1 && buttonState1 == HIGH && sequence == "stop")
{
sequence1();
}
sequence2();
sequence3();
sequence4();
sequence5();
sequenceReset();
sequenceEnd();
seconds = (millis() - start_time) / 1000;
int buttonState1 = digitalRead(buttonPin1);
int buttonState2 = digitalRead(buttonPin2);
int buttonState3 = digitalRead(buttonPin3);
int buttonState4 = digitalRead(buttonPin4);
int buttonState5 = digitalRead(buttonPin5);
int buttonState6 = digitalRead(buttonPin6);
int buttonState7 = digitalRead(buttonPin7);
}
/*void OLEDblink()
{
blinkTime1 = millis();
if ((blinkTime1 - blinkTime2) > interval)
{
blinkTime2 = blinkTime1;
if (OLEDState == 0)
{
OLEDState = 1;
}
else
{
OLEDState = 0;
}
if (OLEDState == 1)
{
display.setCursor(0,50); // Set cursor position
display.println("Sequence 5 blinking"); // Display message
display.display();
}
else if (OLEDState == 0)
{
display.setCursor(0,50); // Set cursor position
display.println(" "); // Display message
display.display();
}
}
}
*/
void button4()
{
buttonState4 = digitalRead(buttonPin4); // Read button state
if (buttonState4 != lastButtonState4 && buttonState4 == HIGH) { // 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
timer();
}
void button5()
{
buttonState5 = digitalRead(buttonPin5); // Read button state
if (buttonState5 != lastButtonState5 && buttonState5 == HIGH)
{ // 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
timer();
}
void button6()
{
buttonState6 = digitalRead(buttonPin6); // Read button state
if (buttonState6 != lastButtonState6 && buttonState6 == HIGH) { // 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
timer();
}
void button7()
{
buttonState7 = digitalRead(buttonPin7); // Read button state
if (buttonState7 != lastButtonState7 && buttonState7 == HIGH) { // 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
timer();
}
void sequence1()
{
// If button state changed from HIGH to LOW
startTime = millis(); // Record start time
start_time = 0;
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
timer();
seq = 2;
sequence = "start";
lastButtonState1 = buttonState1; // Update last button state
}
void sequence2()
{
if (seq == 2 && (seconds > 10 && seconds <=20))
{
startTime = millis();
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
timer();
seq = 3;
}
}
void sequence3()
{
if (seq == 3 && (seconds > 20 && seconds <= 30))
{
startTime = millis();
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
timer();
seq = 4;
}
}
void sequence4()
{
if (seq == 4 && (seconds > 30 && seconds <= 40))
{
startTime = millis();
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
timer();
seq = 5;
}
}
void sequence5()
{
if (seq == 5 && (seconds > 40 && seconds <= 50))
{
startTime = millis();
display.clearDisplay(); // Clear display
display.setCursor(0,0); // Set cursor position
display.println("Sequence 5"); // Display message
display.println("Countdown:"); // Display message
display.display(); // Show message
timer();
seq = 6;
}
}
void sequenceEnd()
{
buttonState3 = digitalRead(buttonPin3); // Read button state
if (buttonState3 != lastButtonState3 && buttonState3 == HIGH) { // If button state changed from HIGH to LOW
startTime = 0; // Record start time
display.clearDisplay(); // Clear display
display.setCursor(0,0); // Set cursor position
display.println("Stopped"); // Display message
display.display(); // Show message
}
lastButtonState3 = buttonState3; // Update last button state
}
void sequenceReset()
{
buttonState2 = digitalRead(buttonPin2); // Read button state
if (buttonState2 != lastButtonState2 && buttonState2 == HIGH && sequence == "start")
{
// If button state changed from HIGH to LOW
buttonState1 = HIGH;
seq = 1;
sequence = "stop";
sequence1();
}
lastButtonState1 = buttonState1;
lastButtonState2 = buttonState2; // Update last button state
}
void timer()
{
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 (remainingTime == 0)
{
display.clearDisplay();
display.setCursor(0, 20); // Set cursor position
display.print("Sequence End"); // Display remaining time
display.display(); // Show message
}
if (remainingTime % 2 > 0)
{
display.setCursor(0,50); // Set cursor position
display.println("Sequence 5 blinking"); // Display message
display.display();
}
else
{
display.setCursor(0,50); // Set cursor position
display.println(" "); // Display message
display.display();
}
}
}