#define ECHO_PIN 2
#define TRIG_PIN 15
#define LED_RED 5
#define LED_ORANGE 21
#define LED_GREEN 25
#define BUZZER_PIN_1 19
#define BUZZER_PIN_2 12
unsigned long previousMillis = 0; // Store the last time we updated the readings
const long interval = 1000; // Interval for distance measurements
unsigned long buzzerStartMillis = 0; // Start time for the buzzer
const unsigned long buzzerDuration = 15000; // Duration to buzz in milliseconds (15 seconds)
const unsigned long cooldownDurationYellow = 1800000; // Cooldown duration in milliseconds for yellow (30 minutes)
bool isBuzzingYellow = false; // State to track if the yellow buzzer is currently buzzing
bool isBuzzingRed = false; // State to track if the red buzzer is currently buzzing
bool isCoolingDownYellow = false; // State to track if the yellow buzzer is in cooldown
bool isCoolingDownRed = false; // State to track if the red buzzer is in cooldown
unsigned long cooldownStartMillisYellow = 0; // Start time for yellow cooldown
unsigned long cooldownStartMillisRed = 0; // Start time for red cooldown
void setup() {
Serial.begin(115200);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(LED_RED, OUTPUT);
pinMode(LED_ORANGE, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(BUZZER_PIN_1, OUTPUT);
pinMode(BUZZER_PIN_2, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis(); // Get the current time
// Check if it's time to take a distance reading
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis; // Save the last time we measured
// Trigger the ultrasonic sensor
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read the echo time
int duration = pulseIn(ECHO_PIN, HIGH);
float distance = duration * 0.034 / 2; // Calculate distance in cm
Serial.print("Distance: ");
Serial.println(distance);
// Check distance and activate corresponding outputs
if (distance > 200) {
Serial.println("Safe Zone...");
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_ORANGE, LOW);
digitalWrite(LED_RED, LOW);
noTone(BUZZER_PIN_1);
noTone(BUZZER_PIN_2);
isBuzzingYellow = false; // Reset the yellow buzzer state
isBuzzingRed = false; // Reset the red buzzer state
}
else if (distance <= 200 && distance > 100) {
Serial.println("Warning Zone!");
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_ORANGE, HIGH);
digitalWrite(LED_RED, LOW);
// Only activate the yellow buzzer if it has not buzzed in the last 15 seconds
if (!isBuzzingYellow && !isCoolingDownYellow) {
tone(BUZZER_PIN_1, 1000); // Activate first buzzer at 1000 Hz
buzzerStartMillis = currentMillis; // Record when buzzing started
isBuzzingYellow = true; // Set the yellow buzzer state to buzzing
}
}
else if (distance <= 100) {
Serial.println("Danger Zone! Alarm Activated!!!");
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_ORANGE, LOW);
digitalWrite(LED_RED, HIGH);
// Activate the red buzzer if it has not buzzed yet
if (!isBuzzingRed) {
tone(BUZZER_PIN_2, 1000); // Activate second buzzer at 1000 Hz
buzzerStartMillis = currentMillis; // Record when buzzing started
isBuzzingRed = true; // Set the red buzzer state to buzzing
}
}
}
// Check if the yellow buzzer duration has elapsed
if (isBuzzingYellow && (currentMillis - buzzerStartMillis >= buzzerDuration)) {
noTone(BUZZER_PIN_1); // Turn off the yellow buzzer after 15 seconds
isBuzzingYellow = false; // Reset the yellow buzzer state
isCoolingDownYellow = true; // Start cooldown period for yellow
cooldownStartMillisYellow = currentMillis; // Record when cooldown started
}
// Check if the red buzzer should be stopped (only if it was activated)
if (isBuzzingRed && (currentMillis - buzzerStartMillis >= buzzerDuration)) {
noTone(BUZZER_PIN_2); // Turn off the red buzzer after 15 seconds
isBuzzingRed = false; // Reset the red buzzer state
isCoolingDownRed = true; // Start cooldown period for red
cooldownStartMillisRed = currentMillis; // Record when cooldown started
}
// Manage cooldown for yellow buzzer
if (isCoolingDownYellow && (currentMillis - cooldownStartMillisYellow >= cooldownDurationYellow)) {
isCoolingDownYellow = false; // Reset cooldown state for yellow
}
// Manage cooldown for red buzzer (not affecting yellow)
if (isCoolingDownRed && (currentMillis - cooldownStartMillisRed >= cooldownDurationYellow)) {
isCoolingDownRed = false; // Reset cooldown state for red
}
}