const int blueLed = 4; // Blue LED for system active indicator
const int greenLed = 5; // Green LED (currently unused)
const int doors = 2; // Door sensor input
const int mainSwitch = 3; // Main switch input
const int alarm = 12; // Alarm output
// Duration for alarm to sound (in milliseconds)
const int alarmDuration = 5000; // Adjust as needed
void setup() {
pinMode(mainSwitch, INPUT_PULLUP); // Set up switch with internal pull-up resistor
pinMode(doors, INPUT_PULLUP); // Set up door sensor with internal pull-up resistor
pinMode(greenLed, OUTPUT); // Set up LEDs as outputs
pinMode(alarm, OUTPUT);
pinMode(blueLed, OUTPUT);
digitalWrite(alarm, LOW); // Ensure alarm starts off initially
}
void loop() {
int switchState = digitalRead(mainSwitch);
int doorState = digitalRead(doors);
// If the main switch is on, activate the system
if (switchState == HIGH) {
digitalWrite(blueLed, HIGH); // Turn on blue LED to signal active system
// If the door is open, trigger the alarm
if (doorState == LOW) {
digitalWrite(alarm, HIGH); // Turn on the alarm
delay(alarmDuration); // Sound the alarm for the specified duration
digitalWrite(alarm, LOW); // Turn off the alarm after the duration
} else {
digitalWrite(alarm, LOW); // Keep the alarm off if the door is closed
}
} else {
digitalWrite(blueLed, LOW); // Turn off blue LED if the system is disarmed
digitalWrite(alarm, LOW); // Ensure alarm is off when the system is off
}
}