#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
float RLDR;
float Vout;
#define NUM_LEDS 4
#define LED_PIN 2
#define SENSOR_PIN A1
#define BUZZER_PIN 6 // Digital pin for the buzzer
#define CONTINUE_BUTTON_PIN 7 // Digital pin for the continue button
#define RESET_BUTTON_PIN 8 // Digital pin for the reset button
Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
unsigned long startTime = 0;
unsigned long elapsedTime = 0;
boolean timing = false;
boolean buzzerActive = false;
unsigned long buzzerStartTime = 0;
boolean continuePressed = false;
boolean resetPressed = false;
void setup() {
lcd.begin(16, 2);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
pinMode(BUZZER_PIN, OUTPUT); // Set the buzzer pin as an output
digitalWrite(BUZZER_PIN, LOW); // Turn off the buzzer
pinMode(CONTINUE_BUTTON_PIN, INPUT_PULLUP); // Set the continue button pin as input with internal pull-up resistor
pinMode(RESET_BUTTON_PIN, INPUT_PULLUP); // Set the reset button pin as input with internal pull-up resistor
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(SENSOR_PIN);
Vout = (sensorValue * 0.0048828125);
RLDR = (10000.0 * (5 - Vout))/Vout;
lcd.clear();
// Display light status
lcd.setCursor(0, 0);
lcd.print("Light: ");
if (((RLDR / 500) >= 0) && ((RLDR / 500) <= 10)) {
// Dark
lcd.print("Dark");
colorWipe(strip.Color(255, 0, 255), 50); // Animate purple
if (!timing) {
startTime = millis();
timing = true;
buzzerStartTime = startTime;
buzzerActive = true;
updateTimeDisplay();
}
} else if (((RLDR / 500) > 10) && ((RLDR / 500) <= 14)) {
// Dim
lcd.print("Dim ");
colorWipe(strip.Color(255, 0, 255), 50); // Animate purple
if (!timing) {
startTime = millis();
timing = true;
buzzerStartTime = startTime;
buzzerActive = true;
updateTimeDisplay();
}
} else if (((RLDR / 500) > 14) && ((RLDR / 500) <= 50)) {
// Bright
lcd.print("Bright");
colorWipe(strip.Color(0, 0, 0), 50); // Turn off
if (timing) {
lcd.setCursor(0, 1);
lcd.print("C or R?");
while (true) {
if (digitalRead(CONTINUE_BUTTON_PIN) == LOW) {
continuePressed = true;
break;
} else if (digitalRead(RESET_BUTTON_PIN) == LOW) {
resetPressed = true;
break;
}
}
}
}
// Update elapsed time and buzzer if timing is true
if (timing) {
unsigned long currentTime = millis();
elapsedTime += currentTime - startTime;
startTime = currentTime;
updateTimeDisplay();
// Check if it's time to activate the buzzer
unsigned long buzzerElapsedTime = currentTime - buzzerStartTime;
if (buzzerElapsedTime >= 60000) {
// Activate the buzzer for 3 seconds
updateBuzzer();
delay(3000);
buzzerStartTime = currentTime;
}
}
// Handle button actions
if (continuePressed) {
continuePressed = false; // Reset the flag
// Check if timing is paused
if (!timing) {
startTime = millis() - elapsedTime; // Adjust the start time to account for the paused time
timing = true;
buzzerStartTime = startTime;
buzzerActive = true;
}
}
if (resetPressed) {
resetPressed = false; // Reset the flag
// Reset all variables and states
timing = false;
elapsedTime = 0;
buzzerActive = false;
buzzerStartTime = 0;
}
delay(500);
}
void colorWipe(uint32_t c, uint8_t wait) {
for (uint16_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
void updateBuzzer() {
if (buzzerActive) {
tone(BUZZER_PIN, 1000, 3000); // Activate the buzzer for 3 seconds
} else {
noTone(BUZZER_PIN); // Turn off the buzzer
}
}
void updateTimeDisplay() {
unsigned long minutes = (elapsedTime / 60000) % 60;
unsigned long seconds = (elapsedTime / 1000) % 60;
lcd.setCursor(0, 1);
lcd.print("Time: ");
if (minutes < 10) {
lcd.print("0");
}
lcd.print(minutes);
lcd.print(":");
if (seconds < 10) {
lcd.print("0");
}
lcd.print(seconds);
}