master
lux 5 years ago
parent 84ebdcc7c5
commit 765c40dfab

@ -1,5 +1,5 @@
BasedOnStyle: Google
UseTab: Always
UseTab: ForIndentation
IndentWidth: 2
TabWidth: 2
AlignConsecutiveAssignments: true

@ -1,51 +1,51 @@
#include "tone.h";
// Miscallaneous inputs/outputs
#define UNARMED_LED 2 // LED to light up when unarmed
#define ARMED_LED 13 // LED to light up when armed
#define ARM_UNARM_BUTTON 3 // button used to arm/unarm
#define SPKR 12
// Security points
const int door1 = 4;
const int door2 = 5;
// Miscallaneous inputs/outputs
const int unarmedLed = 2; // LED to light up when unarmed
const int armUnarm = 3; // button used to arm/unarm
const int spkr = 12;
const int armedLed = 13; // LED to light up when armed
// Configuration parameters
const String pass = "113"; // password
const int securityPoints = 2; // number of security points
const String pass = "113"; // password
const int securityPoints = 2; // number of security points
const int maxWarnLevel = 250; // maximum warnLevel
const int warnDelayRef = 300; // the number to subtract warnLevel from
const int warnIncrement = 7; // increment maxLevel by this number
const int maxWarnLevel = 250; // maximum warnLevel
const int warnDelayRef = 300; // the number to subtract warnLevel from
const int warnIncrement = 7; // increment maxLevel by this number
// Program booleans
bool isArmed = 1; // armed status. set to initialize as armed or unarmed
bool soundAlarm = 0; // should the program sound the alarm?
bool unarmButtonDown = 0; // boolean that prevents unarm right after rearm
// until unarm button is depressed
bool isArmed = 1; // armed status. set to initialize as armed or unarmed
bool soundAlarm = 0; // should the program sound the alarm?
bool unarmButtonDown = 0; // boolean that prevents unarm right after rearm
// until unarm button is depressed
// Program variables
int warnLevel = 0;
int isOpen[securityPoints] = { // status of security points.
false, false};
int warnLevel = 0;
int isOpen[securityPoints] = { // status of security points.
false, false};
bool waitingForPass = 0;
String readString;
unsigned long currentMillis = millis();
unsigned long currentMillis = millis();
unsigned long unarmIntTracker = 0;
unsigned long alarmIntTracker = 0;
bool flipflopUnarmState = LOW;
bool flipflopAlarmState = LOW;
bool flipflopUnarmState = LOW;
bool flipflopAlarmState = LOW;
// Melodies
int initMelody[] = {FS4, GS4, AS4, CS5, DS5, FS5};
int initMelody[] = {FS4, GS4, AS4, CS5, DS5, FS5};
int initDurations[] = {100, 100, 100, 100, 100, 100};
int unarmMelody[] = {B3, B2};
int unarmMelody[] = {B3, B2};
int unarmDurations[] = {50, 50};
int armMelody[] = {C5, C6};
int armMelody[] = {C5, C6};
int armDurations[] = {50, 50};
int alarmNote = CS5;
@ -60,9 +60,9 @@ void setup() {
pinMode(door2, INPUT_PULLUP);
// Misc. inputs/outputs
pinMode(armUnarm, INPUT_PULLUP);
pinMode(unarmedLed, OUTPUT);
pinMode(armedLed, OUTPUT);
pinMode(ARM_UNARM_BUTTON, INPUT_PULLUP);
pinMode(UNARMED_LED, OUTPUT);
pinMode(ARMED_LED, OUTPUT);
Serial.println("Initialized.");
@ -70,21 +70,23 @@ void setup() {
if (isArmed == 1) {
Serial.println("Currently ARMED.");
digitalWrite(armedLed, HIGH);
digitalWrite(ARMED_LED, HIGH);
} else {
Serial.println("Currently UNARMED.");
digitalWrite(unarmedLed, HIGH);
digitalWrite(UNARMED_LED, HIGH);
}
}
// Play an array of tones.
void playMelody(int melodyLength, int *melody, int *noteDur, int noteDelay) {
for (int i = 0; i < melodyLength; i++) {
tone(spkr, melody[i], noteDur[i]);
tone(SPKR, melody[i], noteDur[i]);
delay(noteDelay);
noTone(spkr);
noTone(SPKR);
}
}
// Switches an LED's power on/off whenever called.
void flipLed(int ledPin, bool &state) {
if (state == LOW) {
state = HIGH;
@ -104,7 +106,7 @@ void loop() {
isOpen[0] = true;
if (isArmed == true) soundAlarm = 1;
} else if (digitalRead(door1) == HIGH && isOpen[0] == true) {
isOpen[0] = false;
isOpen[0] = false;
soundAlarm = 0;
Serial.println("Door1 has closed.");
}
@ -115,15 +117,15 @@ void loop() {
isOpen[1] = true;
if (isArmed == true) soundAlarm = 1;
} else if (digitalRead(door2) == HIGH && isOpen[1] == true) {
isOpen[1] = false;
isOpen[1] = false;
soundAlarm = 0;
Serial.println("Door2 has closed.");
}
// - Arm/Unarm -
if ((digitalRead(armUnarm) == LOW || waitingForPass == 1) &&
unarmButtonDown == 0) {
if ((digitalRead(ARM_UNARM_BUTTON) == LOW || waitingForPass == 1) &&
unarmButtonDown == 0) {
if (isArmed == 1 || waitingForPass == 1) {
if (waitingForPass == 0) {
waitingForPass = 1;
@ -148,7 +150,7 @@ void loop() {
playMelody(2, armMelody, unarmDurations, noteDelay);
Serial.println("Wrong password!");
}
readString = "";
readString = "";
waitingForPass = 0;
}
}
@ -158,11 +160,11 @@ void loop() {
// async blink unarm led every 250ms
if (currentMillis - unarmIntTracker >= 1000 && waitingForPass == 1) {
unarmIntTracker = currentMillis;
flipLed(unarmedLed, flipflopUnarmState);
flipLed(UNARMED_LED, flipflopUnarmState);
}
} else {
isArmed = 1;
waitingForPass = 0;
isArmed = 1;
waitingForPass = 0;
unarmButtonDown = 1;
playMelody(2, armMelody, armDurations, noteDelay);
@ -171,7 +173,7 @@ void loop() {
}
// - Button safety -
if (digitalRead(armUnarm) == HIGH && unarmButtonDown == 1) {
if (digitalRead(ARM_UNARM_BUTTON) == HIGH && unarmButtonDown == 1) {
unarmButtonDown = 0;
}
@ -183,11 +185,11 @@ void loop() {
// - Activate unarmed/armed LEDs -
if (isArmed == 1 && waitingForPass == 0 && soundAlarm == 0) {
digitalWrite(armedLed, HIGH);
digitalWrite(unarmedLed, LOW);
digitalWrite(ARMED_LED, HIGH);
digitalWrite(UNARMED_LED, LOW);
} else if (waitingForPass == 0 && soundAlarm == 0) {
digitalWrite(unarmedLed, HIGH);
digitalWrite(armedLed, LOW);
digitalWrite(UNARMED_LED, HIGH);
digitalWrite(ARMED_LED, LOW);
}
// - Sound alarm -
@ -200,16 +202,16 @@ void loop() {
if (warnLevel < maxWarnLevel) { warnLevel = warnLevel + warnIncrement; };
flipLed(armedLed, flipflopAlarmState);
flipLed(ARMED_LED, flipflopAlarmState);
if (flipflopAlarmState == HIGH) {
tone(spkr, alarmNote);
tone(SPKR, alarmNote);
} else {
noTone(spkr);
noTone(SPKR);
}
}
} else {
warnLevel = 0;
noTone(spkr);
noTone(SPKR);
}
}
Loading…
Cancel
Save