Last active
May 5, 2019 16:51
-
-
Save soypat/b92e5491d6d47cfbeae2ab5918a5a1c8 to your computer and use it in GitHub Desktop.
Arduino alarm system for home. Intended to work with Hall Effect door sensor. Works with IR remote.
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
// IR Setup | |
#include <IRremote.h> | |
const int RECV_PIN = 7; | |
IRrecv irrecv(RECV_PIN); | |
decode_results results; | |
// Rest of setup | |
const int doorPin = 12; // Analog input pin that the potentiometer is attached to | |
//const int resetPin = 4; % Legacy Button reset | |
const int alarmPin = 8; | |
const int savedpin = 1110; // PIN as in password, code, secretnumbercombination etc | |
unsigned long alarmTime=0,toneTime=0,alarmOnTime=0; | |
bool alarmOn=false; | |
bool alarmTripped=false; | |
bool alarmActive=false; | |
bool alarmOverride=false; | |
bool doorOpen; | |
bool dataAvailable=false; | |
//int resetPush=0; // Legacy button reset | |
int irValue; | |
int pass1,pass2,pass3,pass4=10000,passCount=0; //By setting pass4 equal to 10000 an incomplete password input will always trigger a reset (and fail) | |
unsigned long thetime; | |
int overrideFactor=1; | |
void setup() { | |
// Serial.begin(9600); | |
irrecv.enableIRIn(); | |
irrecv.blink13(true);//Creo que no hace falta | |
// pinMode(resetPin,INPUT); | |
pinMode(alarmPin,OUTPUT); | |
} | |
void loop() { | |
// read the analog in value: | |
int pin=10000; | |
doorOpen = check_door(); | |
Serial.println(doorOpen); | |
// Pre alarma | |
if (doorOpen and alarmTime==0 and !alarmOn and alarmActive) { | |
alarmTime=millis(); | |
alarmTripped=true; | |
reset_password_entry(); | |
} | |
if (alarmTripped and millis()-toneTime>1000 and !alarmOn) { | |
if (!alarmOverride) { | |
tone(alarmPin,600,20); | |
toneTime=millis(); | |
} | |
if (millis()-alarmTime>(7500*overrideFactor)) { // | |
noTone(alarmPin); | |
delay(10); | |
// digitalWrite(alarmPin,HIGH); | |
// tone(alarmPin,4000); | |
digitalWrite(alarmPin,HIGH); | |
alarmOn=true; | |
alarmOnTime=millis(); | |
} | |
} | |
//Post Alarma-> alarmStatus=true | |
// resetPush=digitalRead(resetPin); //legacy button reset. | |
// if (alarmOn and resetPush==1) {reset_alarm();} | |
irValue= storeIR(); | |
// Serial.print(irValue); | |
if (alarmTripped and irValue==11) { //Silences pre-alarm beeps. Waits for password. | |
alarmOverride=true; | |
overrideFactor=3; | |
dataAvailable=false; | |
} | |
pin=pass1*1000+pass2*100+pass3*10+pass4; | |
// Serial.println(pin); | |
// delay(10); | |
if (dataAvailable and irValue<10) { | |
passCount++; | |
dataAvailable=false; | |
switch (passCount) { | |
case 1: | |
pass1=irValue; | |
little_beep(); | |
break; | |
case 2: | |
pass2=irValue; | |
little_beep(); | |
break; | |
case 3: | |
pass3=irValue; | |
little_beep(); | |
break; | |
case 4: | |
pass4=irValue; | |
little_beep(); | |
break; | |
default: | |
reset_password_entry(); | |
} | |
} | |
if (dataAvailable and irValue==10) { //Pressed play button -> Submit 4 digit PIN or Password | |
pin=pass1*1000+pass2*100+pass3*10+pass4; | |
dataAvailable=false; | |
if (pin==savedpin) { | |
// Serial.println("ALARM DEACTIVATED."); | |
reset_alarm(); | |
alarmActive=false; //Shut down alarm completely | |
} else { | |
reset_password_entry(); | |
// Serial.println("Bad Password."); | |
} | |
} | |
// thetime=millis(); | |
// Serial.println(thetime); | |
// delay(50); | |
if (dataAvailable and irValue==20 and !alarmTripped) { | |
alarmActive=true; | |
tone(alarmPin,600,50); | |
delay(100); | |
tone(alarmPin,600,50); | |
dataAvailable=false; | |
alarmOn=false; | |
delay(12000); | |
happy_beep(); | |
} | |
if (dataAvailable and irValue==30) { | |
reset_password_entry(); | |
} | |
if (alarmOn){ //makes sure sound confirmations don't interrupt alarm ringing. Vital piece of code here. | |
digitalWrite(alarmPin,HIGH); | |
if (millis()-alarmOnTime>2*60*1000){ | |
reset_alarm(); | |
} | |
} | |
dataAvailable=false; | |
} | |
////////////FUNCTIONS down there | |
bool check_door(void) { | |
bool doorIsOpen=false; | |
int sum=0,j=0,counts=20; | |
for (int i=0; i<=counts; i++) { | |
j=digitalRead(doorPin); | |
sum=sum+j; | |
} | |
if (sum>counts/2) { | |
return true; | |
} | |
return false; | |
// int value; | |
// value=analogRead(doorPin); | |
// if (value>950) {return true;} | |
// else {return false;} | |
} | |
void reset_alarm(void) { | |
alarmTripped=false; | |
alarmOn=false; | |
overrideFactor=1; | |
alarmOverride=false; | |
digitalWrite(alarmPin,LOW); | |
noTone(alarmPin); | |
toneTime=0; | |
alarmTime=0; | |
sad_beep(); | |
} | |
int storeIR(void) { | |
int value; | |
if (irrecv.decode(&results)){ | |
irrecv.resume(); | |
value = decodeer(results.value); | |
dataAvailable=true; | |
return value; | |
} | |
return; | |
} | |
void reset_password_entry() { | |
pass1=0;pass2=0;pass3=0;pass4=10000; | |
passCount=0; | |
little_beep(); | |
delay(100); | |
little_beep(); | |
delay(100); | |
little_beep(); | |
return; | |
} | |
void sad_beep(void) { | |
delay(200); | |
tone(alarmPin,8000,100); | |
delay(200); | |
tone(alarmPin,2000,100); | |
} | |
void happy_beep(void) { | |
delay(200); | |
tone(alarmPin,2000,100); | |
delay(200); | |
tone(alarmPin,8000,100); | |
} | |
void little_beep(void){ | |
tone(alarmPin,200,15); | |
} | |
int decodeer(int bitly) { //Decoder infrarojo | |
switch (bitly) { | |
case 16724175: | |
return 1; | |
case 16718055: | |
return 2; | |
case 16743045: | |
return 3; | |
case 16716015: | |
return 4; | |
case 16726215: | |
return 5; | |
case 16734885: | |
return 6; | |
case 16728765: | |
return 7; | |
case 16730805: | |
return 8; | |
case 16732845: | |
return 9; | |
case 16738455: | |
return 0; | |
case 16761405: //PLAY BUTTON >|| | |
return 10; | |
case 16748655: // EQUALIZER (EQ) | |
return 11; | |
case 16753245: // CH- | |
return 20; | |
case 16736925: // CH | |
return 21; | |
case 16769565: // CH+ | |
return 22; | |
case 16720605: // PREV |<< | |
return 30; | |
case 16712445: // FORW >>| | |
return 31; | |
default: | |
return 999; //UNKNOWN | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment