-
-
Save anonymous/5d4b430ded3ab24a1f65 to your computer and use it in GitHub Desktop.
Tilt activated alarm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Fake Hide-A-Key Rock Intruder Alarm | |
written for an Arduino Uno | |
by Sarah Pavis | |
*/ | |
// include the LCD screen library code | |
#include <LiquidCrystal.h> | |
// initialize the library with the numbers of the interface pins | |
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); | |
// fix the pin numbers for the tilt switch pin and reset button | |
const int tiltPin = 6; | |
const int alarmPin = 7; | |
// variables to hold the states of the tiltPin | |
int tiltState = 0; | |
int prevTiltState = 0; | |
// variable for the alarm reset buttoon | |
int alarmButton = 0; | |
// time check variables for determining LED blink | |
unsigned long currentTime; | |
long prevTime = 0; | |
long blinkRate = 200; | |
// the pin locations of the LEDs | |
// could be called out below but it's easier to change the program later on | |
// if the locations are kept as variables up here | |
int upperLight = 9; | |
int lowerLight = 8; | |
// variables to hold the high or low LED states | |
int upperLightState; | |
int lowerLightState; | |
// a variable to hold the time the alarm was first activated | |
long alarmTime; | |
// 30 sec alarm duration after which it resets to standby mode | |
long alarmLength = 30000; | |
// a flag variable to be activated the first time the tilt switch changes state | |
boolean intruder = false; | |
void setup() { | |
// open the serial port for debugging and alarm logs | |
Serial.begin(9600); | |
// set the LED pins as output | |
pinMode(lowerLight, OUTPUT); | |
pinMode(upperLight, OUTPUT); | |
// set the tilt switch and button pins as an input | |
pinMode(tiltPin, INPUT); | |
pinMode(alarmPin, INPUT); | |
// clears anything previously on the LCD | |
lcd.clear(); | |
// establishes the LCD screen as 16 columns by 2 rows | |
lcd.begin(16, 2); | |
// initial screen before thee alarm is activated | |
// ideally the LCD would be off completely | |
// but it's useful to have a neutral message here | |
// for clarity and debugging | |
lcd.print("Alarm is in"); | |
lcd.setCursor(0, 1); | |
lcd.print("standby mode."); | |
} | |
void loop() { | |
// checks the states of each sensor and | |
// sets the values to variables | |
tiltState = digitalRead(tiltPin); | |
alarmButton = digitalRead(alarmPin); | |
// if the device has been tilted | |
// then register that there is an intruder | |
if (tiltState != prevTiltState) { | |
intruder = true; | |
} | |
// alarm routine | |
if (intruder == true) { | |
lcd.setCursor(0, 0); | |
lcd.print("Intruder Alert"); | |
lcd.setCursor(0, 1); | |
lcd.print("Alarm Activated"); | |
// get the current time and set it | |
currentTime = millis(); | |
// this should set the current time as the start of the alarm | |
// but it resets the variable every time it loops | |
// i need to refactor this so that alarmTime is constant | |
alarmTime = millis(); | |
// blink the LEDs without delaying the program | |
if ((currentTime - prevTime) > blinkRate) { | |
if (upperLightState == HIGH) { | |
lowerLightState = HIGH; | |
upperLightState = LOW; | |
} | |
else { | |
lowerLightState = LOW; | |
upperLightState = HIGH; | |
} | |
digitalWrite(upperLight, upperLightState); | |
digitalWrite(lowerLight, lowerLightState); | |
prevTime = currentTime; | |
} | |
// if the reset button is pushed or the alarm goes off for too long | |
// then reset the alarm | |
if (alarmLength <= currentTime - alarmTime || alarmButton == HIGH) { | |
intruder = false; | |
} | |
// alarm reset sequence | |
if (intruder == false) { | |
lcd.clear(); | |
lcd.begin(16, 2); | |
lcd.print("Alarm is in"); | |
lcd.setCursor(0, 1); | |
lcd.print("standby mode."); | |
digitalWrite(upperLight, LOW); | |
digitalWrite(lowerLight, LOW); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment