unsigned long timerDuration = 210000; // 3.5 minutes in milliseconds
unsigned long flashDuration = 30000; // 30 seconds Flashing duration after the initial timer in milliseconds
unsigned long startTime = 0; // Variable to store the start time
int ledPin = 13; // LED connected to digital pin 13
int doorSwitchPin = 2; // door switch connected to digital pin 2
bool doorOpened = false; // Flag to track door state
bool timerStarted = false; // Flag to track timer state
bool flashMode = false; // Flag to indicate flashing mode
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as an output
pinMode(doorSwitchPin, INPUT_PULLUP); // Set the door switch pin as input with internal pull-up resistor
}
void loop() {
// Check the state of the door switch
if (digitalRead(doorSwitchPin) == LOW) {
doorOpened = true; // door is opened
} else {
doorOpened = false; // door is closed
}
if (doorOpened && !timerStarted) {
//door is opened and timer is not started
startTime = millis(); // Save the current time as the start time
timerStarted = true; // Set the flag to indicate timer started
}
if (timerStarted) {
unsigned long currentTime = millis(); // Get the current time
if (!flashMode) {
unsigned long elapsedTime = currentTime - startTime; // Calculate elapsed time
if (elapsedTime < timerDuration) { // Countdown timer
// LED off during the countdown
digitalWrite(ledPin, LOW);
} else {
// Timer finished, turn on the LED
digitalWrite(ledPin, HIGH);
flashMode = true; // Set the flag for flashing mode
startTime = currentTime; // Reset the start time for flashing
}
} else {
unsigned long flashElapsedTime = currentTime - startTime; // Calculate elapsed time for flashing
if (flashElapsedTime < flashDuration) {
// Flash the LED every 1 second
if (flashElapsedTime % 1000 < 500) {
digitalWrite(ledPin, HIGH); // Turn on the LED for half of the second
} else {
digitalWrite(ledPin, LOW); // Turn off the LED for the other half of the second
}
} else {
flashMode = false; // End flashing mode
digitalWrite(ledPin, LOW); // Turn off the LED
}
}
}
}