#include <Servo.h>
Servo myFan;
const int andGatePin = 2;
// LED Pins
const int redLed = 13; // Warning (Fan Moving)
const int greenLed = 12; // Safe (Fan Stopped)
// Variables for the Fan
int angle = 0;
int step = 2; // Speed of fan
unsigned long lastServoTime = 0;
// Variables for Blinking
int ledState = LOW;
unsigned long lastBlinkTime = 0;
const long blinkSpeed = 300; // How fast the LEDs blink (milliseconds)
void setup() {
myFan.attach(9);
pinMode(andGatePin, INPUT);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
myFan.write(0); // Start at 0
}
void loop() {
unsigned long currentMillis = millis();
int isSystemOn = digitalRead(andGatePin);
// --- BLINK LOGIC TIMER ---
// Every 300ms, flip the "ledState" from LOW to HIGH or vice versa
if (currentMillis - lastBlinkTime >= blinkSpeed) {
lastBlinkTime = currentMillis;
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
}
// --- MAIN DECISION ---
if (isSystemOn == HIGH) {
// === DANGER ZONE (Fan Moving) ===
// 1. Blink Red LED
digitalWrite(redLed, ledState);
digitalWrite(greenLed, LOW); // Green must be OFF
// 2. Move Fan Smoothly (without stopping the code)
if (currentMillis - lastServoTime >= 15) {
lastServoTime = currentMillis;
angle = angle + step;
if (angle >= 180 || angle <= 0) {
step = -step; // Reverse direction
}
myFan.write(angle);
}
}
else {
// === SAFE ZONE (Fan Stopped) ===
// 1. Blink Green LED
digitalWrite(greenLed, ledState);
digitalWrite(redLed, LOW); // Red must be OFF
// 2. Stop Fan
myFan.write(0);
angle = 0;
}
}A
B