#define relayPin 1 // enable/disable relay powering the siren
|
|
#define switchPin 2 // tampering switch sensor
|
|
#define buttonPin 3 // button to be held down to enable/disable alarm
|
|
#define ledPin 4 // status LED to show when alarm is enabled
|
|
#define ringTime 20 // time to ring the siren for (in seconds)
|
|
int enableStatus = 1; // variable to store alarm status (1 = enabled); enabled at boot
|
|
int countDown = 2; // times to repeat siren blip sound in sound() function
|
|
|
|
void setup() {
|
|
// here we assign pins as in/out and pull-up the inputs
|
|
pinMode(relayPin, OUTPUT);
|
|
digitalWrite(relayPin, LOW);
|
|
pinMode(switchPin, INPUT_PULLUP);
|
|
digitalWrite(switchPin, HIGH);
|
|
pinMode(buttonPin, INPUT_PULLUP);
|
|
digitalWrite(buttonPin, HIGH);
|
|
pinMode(ledPin, OUTPUT);
|
|
digitalWrite(ledPin, LOW);
|
|
sound(); // give audible feedback at boot
|
|
delay(10000); // wait 10s, then exit setup
|
|
|
|
}
|
|
|
|
void loop() {
|
|
|
|
LED();
|
|
|
|
ButtonCtrl();
|
|
|
|
|
|
//Main alarm logic follows
|
|
if (enableStatus == 1 && (digitalRead(switchPin) == LOW)){
|
|
delay(500); //switch debounce
|
|
if (digitalRead(switchPin) == LOW){
|
|
digitalWrite(relayPin, HIGH); //sound the siren!
|
|
delay(ringTime*1000); //let the siren sound for the duration of defined ringTime
|
|
digitalWrite(relayPin, LOW); //now switch off the siren
|
|
digitalWrite(ledPin, LOW); //switch off status LED
|
|
enableStatus = 0; //disable the alarm after sounding
|
|
}
|
|
}
|
|
}
|
|
|
|
void sound() {
|
|
// sound alert function used at boot and alarm enabling
|
|
if (enableStatus == 1) {
|
|
while (countDown > 0) {
|
|
digitalWrite(relayPin, HIGH); //sound the siren!
|
|
delay(5);
|
|
digitalWrite(relayPin, LOW); //stop the siren
|
|
delay(500);
|
|
countDown--;
|
|
}
|
|
}
|
|
}
|
|
|
|
void LED() {
|
|
// status LED control logic
|
|
if (enableStatus == 1) {
|
|
digitalWrite(ledPin, HIGH); //turn on status LED
|
|
}
|
|
else {
|
|
digitalWrite(ledPin, LOW); //turn off status LED
|
|
}
|
|
}
|
|
|
|
void ButtonCtrl() {
|
|
// check if button is pressed, switch state accordingly
|
|
if ((digitalRead(buttonPin) == LOW) && enableStatus != 1){
|
|
delay(500); //button debounce
|
|
if (digitalRead(buttonPin) == LOW){
|
|
enableStatus = 1;
|
|
countDown = 2;
|
|
sound();
|
|
}
|
|
}
|
|
else if ((digitalRead(buttonPin) == LOW) && enableStatus != 0){
|
|
delay(500); //button debounce
|
|
if (digitalRead(buttonPin) == LOW){
|
|
countDown = 1;
|
|
sound();
|
|
enableStatus = 0;
|
|
}
|
|
}
|
|
}
|