#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SSD1306_I2C_ADDRESS 0x3C // Define the I2C address
#define SCREEN_ADDRESS SSD1306_I2C_ADDRESS
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET, SCREEN_ADDRESS);
const int ledPin = 2;
const int controlPin = 3; // Pin to control start/stop
unsigned long previousMillis = 0;
const long interval = 500; // Blink every 500 milliseconds (0.5 seconds)
int blinkCount = 0;
bool counting = true; // Variable to control counting
void setup() {
display.begin(SSD1306_I2C_ADDRESS, SCREEN_ADDRESS);
display.display();
delay(2000); // Pause for 2 seconds
display.clearDisplay();
display.display();
pinMode(ledPin, OUTPUT);
pinMode(controlPin, INPUT_PULLUP); // Assuming HIGH means stop, LOW means start
}
void loop() {
// Check the state of the control pin
if (digitalRead(controlPin) == HIGH) {
counting = false; // Stop counting
} else {
counting = true; // Start counting
}
if (counting) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// Save the last time we blinked the LED
previousMillis = currentMillis;
// Toggle the LED state
if (digitalRead(ledPin) == HIGH) {
digitalWrite(ledPin, LOW);
} else {
digitalWrite(ledPin, HIGH);
blinkCount++;
}
// Update OLED display
updateDisplay();
}
}
}
void updateDisplay() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Blink Time:");
int hours = blinkCount / 3600;
int minutes = (blinkCount % 3600) / 60;
int seconds = blinkCount % 60;
display.setTextSize(2);
display.setCursor(10, 20);
display.print(hours);
display.print(":");
if (minutes < 10) display.print("0");
display.print(minutes);
display.print(":");
if (seconds < 10) display.print("0");
display.print(seconds);
display.display();
}