Initial commit
commit
65e3b758c9
@ -0,0 +1,142 @@
|
||||
/* $Id: Tone.h 113 2010-06-16 20:16:29Z bhagman@roguerobotics.com $
|
||||
|
||||
A Tone Generator Library
|
||||
|
||||
Written by Brett Hagman
|
||||
http://www.roguerobotics.com/
|
||||
bhagman@roguerobotics.com
|
||||
|
||||
This library is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*************************************************/
|
||||
|
||||
#ifndef _Tone_h
|
||||
#define _Tone_h
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/*************************************************
|
||||
* Public Constants
|
||||
*************************************************/
|
||||
|
||||
#define B0 31
|
||||
#define C1 33
|
||||
#define CS1 35
|
||||
#define D1 37
|
||||
#define DS1 39
|
||||
#define E1 41
|
||||
#define F1 44
|
||||
#define FS1 46
|
||||
#define G1 49
|
||||
#define GS1 52
|
||||
#define A1 55
|
||||
#define AS1 58
|
||||
#define B1 62
|
||||
#define C2 65
|
||||
#define CS2 69
|
||||
#define D2 73
|
||||
#define DS2 78
|
||||
#define E2 82
|
||||
#define F2 87
|
||||
#define FS2 93
|
||||
#define G2 98
|
||||
#define GS2 104
|
||||
#define A2 110
|
||||
#define AS2 117
|
||||
#define B2 123
|
||||
#define C3 131
|
||||
#define CS3 139
|
||||
#define D3 147
|
||||
#define DS3 156
|
||||
#define E3 165
|
||||
#define F3 175
|
||||
#define FS3 185
|
||||
#define G3 196
|
||||
#define GS3 208
|
||||
#define A3 220
|
||||
#define AS3 233
|
||||
#define B3 247
|
||||
#define C4 262
|
||||
#define CS4 277
|
||||
#define D4 294
|
||||
#define DS4 311
|
||||
#define E4 330
|
||||
#define F4 349
|
||||
#define FS4 370
|
||||
#define G4 392
|
||||
#define GS4 415
|
||||
#define A4 440
|
||||
#define AS4 466
|
||||
#define B4 494
|
||||
#define C5 523
|
||||
#define CS5 554
|
||||
#define D5 587
|
||||
#define DS5 622
|
||||
#define E5 659
|
||||
#define F5 698
|
||||
#define FS5 740
|
||||
#define G5 784
|
||||
#define GS5 831
|
||||
#define A5 880
|
||||
#define AS5 932
|
||||
#define B5 988
|
||||
#define C6 1047
|
||||
#define CS6 1109
|
||||
#define D6 1175
|
||||
#define DS6 1245
|
||||
#define E6 1319
|
||||
#define F6 1397
|
||||
#define FS6 1480
|
||||
#define G6 1568
|
||||
#define GS6 1661
|
||||
#define A6 1760
|
||||
#define AS6 1865
|
||||
#define B6 1976
|
||||
#define C7 2093
|
||||
#define CS7 2217
|
||||
#define D7 2349
|
||||
#define DS7 2489
|
||||
#define E7 2637
|
||||
#define F7 2794
|
||||
#define FS7 2960
|
||||
#define G7 3136
|
||||
#define GS7 3322
|
||||
#define A7 3520
|
||||
#define AS7 3729
|
||||
#define B7 3951
|
||||
#define C8 4186
|
||||
#define CS8 4435
|
||||
#define D8 4699
|
||||
#define DS8 4978
|
||||
|
||||
|
||||
/*************************************************
|
||||
* Definitions
|
||||
*************************************************/
|
||||
|
||||
class Tone
|
||||
{
|
||||
public:
|
||||
void begin(uint8_t tonePin);
|
||||
bool isPlaying();
|
||||
void play(uint16_t frequency, uint32_t duration = 0);
|
||||
void stop();
|
||||
|
||||
private:
|
||||
static uint8_t _tone_pin_count;
|
||||
uint8_t _pin;
|
||||
int8_t _timer;
|
||||
};
|
||||
|
||||
#endif
|
@ -0,0 +1,151 @@
|
||||
#include "Tone.h";
|
||||
|
||||
const int door1 = 4;
|
||||
const int door2 = 5;
|
||||
|
||||
const int unarmedLed = 2;
|
||||
const int armUnarm = 3;
|
||||
const int spkr = 12;
|
||||
const int armedLed = 13;
|
||||
|
||||
const int pass = 113;
|
||||
const int securityPoints = 2;
|
||||
|
||||
bool isArmed = 1;
|
||||
bool soundAlarm = 0;
|
||||
|
||||
int maxWarnLevel = 250;
|
||||
int warnDelayRef = 300;
|
||||
int warnIncrement = 10;
|
||||
|
||||
int warnLevel = 0;
|
||||
int isOpen[securityPoints] = {false, false};
|
||||
|
||||
int initMelody[] = {C4, E4, G4, E4, G4, C5};
|
||||
int initDurations[] = {200, 100, 100, 100, 100, 100};
|
||||
|
||||
int unarmMelody[] = {C5, D5, E5, G5, C6};
|
||||
int unarmDurations[] = {50, 50, 50, 50, 50};
|
||||
|
||||
int armMelody[] = {C3, D3, G3, B3, C4};
|
||||
int armDurations[] = {50, 50, 50, 50, 50};
|
||||
|
||||
int alarmNote = CS5;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
|
||||
pinMode(door1, INPUT_PULLUP);
|
||||
pinMode(door2, INPUT_PULLUP);
|
||||
pinMode(armUnarm, INPUT_PULLUP);
|
||||
pinMode(unarmedLed, OUTPUT);
|
||||
pinMode(armedLed, OUTPUT);
|
||||
|
||||
Serial.println("Initialized.");
|
||||
|
||||
playMelody(initMelody, initDurations, 200);
|
||||
|
||||
if(isArmed == 1) {
|
||||
Serial.println("Currently ARMED.");
|
||||
digitalWrite(armedLed, HIGH);
|
||||
} else {
|
||||
Serial.println("Currently UNARMED.");
|
||||
digitalWrite(unarmedLed, HIGH);
|
||||
};
|
||||
};
|
||||
|
||||
void playMelody(int melody[], int noteDur[], int noteDelay[]) {
|
||||
for(int i = 0; i < sizeof(melody); i++) {
|
||||
tone(spkr, melody[i], noteDur[i]);
|
||||
delay(noteDelay);
|
||||
noTone(spkr);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
// - Security Points -
|
||||
|
||||
// door1
|
||||
if(digitalRead(door1) == LOW && isOpen[0] == false) {
|
||||
Serial.println("Door1 has opened.");
|
||||
isOpen[0] = true;
|
||||
if(isArmed == true) soundAlarm = 1;
|
||||
} else if(digitalRead(door1) == HIGH && isOpen[0] == true) {
|
||||
isOpen[0] = false;
|
||||
soundAlarm = 0;
|
||||
Serial.println("Door1 has closed.");
|
||||
};
|
||||
|
||||
// door2
|
||||
if(digitalRead(door2) == LOW && isOpen[1] == false) {
|
||||
Serial.println("Door2 has opened.");
|
||||
isOpen[1] = true;
|
||||
if(isArmed == true) soundAlarm = 1;
|
||||
} else if(digitalRead(door2) == HIGH && isOpen[1] == true) {
|
||||
isOpen[1] = false;
|
||||
soundAlarm = 0;
|
||||
Serial.println("Door2 has closed.");
|
||||
};
|
||||
|
||||
// - Arm/Unarm -
|
||||
|
||||
if(digitalRead(armUnarm) == LOW) {
|
||||
if(isArmed == 1) {
|
||||
Serial.println("To unarm, please enter the password.");
|
||||
Serial.print("password> ");
|
||||
while(!Serial.available()) {
|
||||
digitalWrite(unarmedLed, HIGH);
|
||||
delay(250);
|
||||
digitalWrite(unarmedLed, LOW);
|
||||
delay(250);
|
||||
};
|
||||
if(Serial.available()) {
|
||||
String readString;
|
||||
while(Serial.available()) {
|
||||
char c = Serial.read();
|
||||
readString += c;
|
||||
delay(2);
|
||||
};
|
||||
|
||||
if(readString.length() > 0) {
|
||||
int n = readString.toInt();
|
||||
if(n == pass) {
|
||||
isArmed = 0;
|
||||
playMelody(unarmMelody, unarmDurations, 50);
|
||||
Serial.println("Successfully UNARMED.");
|
||||
} else Serial.println("Wrong password!");
|
||||
readString="";
|
||||
};
|
||||
};
|
||||
} else {
|
||||
Serial.println("Successfully ARMED.");
|
||||
playMelody(armMelody, armDurations, 50);
|
||||
isArmed = 1;
|
||||
};
|
||||
delay(1000);
|
||||
};
|
||||
|
||||
// - Activate unarmed/armed LEDs -
|
||||
|
||||
if(isArmed == 1) {
|
||||
digitalWrite(armedLed, HIGH);
|
||||
digitalWrite(unarmedLed, LOW);
|
||||
} else {
|
||||
digitalWrite(unarmedLed, HIGH);
|
||||
digitalWrite(armedLed, LOW);
|
||||
};
|
||||
|
||||
// - Sound alarm -
|
||||
|
||||
if(soundAlarm == 1) {
|
||||
if(warnLevel < maxWarnLevel) warnLevel = warnLevel + warnIncrement;
|
||||
Serial.println(warnLevel);
|
||||
digitalWrite(armedLed, HIGH);
|
||||
tone(spkr, alarmNote);
|
||||
delay(warnDelayRef - warnLevel);
|
||||
noTone(spkr);
|
||||
digitalWrite(armedLed, LOW);
|
||||
delay(warnDelayRef - warnLevel);
|
||||
} else warnLevel = 0;
|
||||
}
|
Loading…
Reference in New Issue