So basically I have a trip wire style alarm, where if you were to set the alarm by inputting the proper sequence set by you and it was tripped, a speaker will go off and a red LED will go off. I have it so that re entering the same sequence that turns the alarm on will turn it off. However, I can not figure out how to get it to work if the wrong sequence is entered in wrong once while the alarm is off. I fixed this with the turning the alarm on portion just by resetting the code. However, while the alarm is running, I can't just reset the code and have the alarm still work. Basically, if you input the wrong sequence while the alarm is running and try to input the right sequence, nothing will happen. The alarm will continue to go off until you manually hit the reset button.
I removed non-essential code and left what I am having issues with (because of the 9000 character limit).
Basically in the alarm(); function, it only check the button presses once and does not bother resetting the array when you try to input the correct sequence (if I haven't explained that once already =P).
#include <DistanceSRF04.h>
#include <NewPing.h>
#include <NewTone.h>
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters).
#define ALARM 3
#define ECHO_PIN 2
#define TRIGGER_PIN 3
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
DistanceSRF04 Dist;
int code[] = {1,2,3,4}; //the desired code is entered in this array,
int entered[4]; //create a new empty array for the code entered by
int alarmoff[4];
void loop(){ //run repeatedly
if (digitalRead(button1) == LOW){ //if button1 is pressed
checkEntered1(1); //call checkEntered and pass it a 1
delay(250);//wait, needed for correct functioning, otherwise
//buttons are deemed to be pressed more than once
}
else if (digitalRead(button2) == LOW){ //if button2 is pressed
checkEntered1(2); //call checkEntered1 and pass it a 2
delay(250); //wait
}
else if (digitalRead(button3) == LOW){ //if button3 is pressed
checkEntered1(3); //call checkEntered1 and pass it a 3
delay(250); //wait
}
else if (digitalRead(button4) == LOW){ //if button4 is pressed
checkEntered1(4); //call checkEntered1 and pass it a 4
delay(250); //wait
}
}
void checkEntered1(int button /* define the 1,2,3 or 4 as an integer called button */){ //check the first element of the entered[] array
if (entered[0] != 0){ //if it is not a zero, i.e. it has already been inputted
checkEntered2(button); //move on to checkEntered2, passing it "button"
}
else if(entered[0] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
entered[0] = button; //set the first element as the button that has been pressed
Serial.print("1: ");Serial.println(entered[0]); //for debugging
}
}
void checkEntered2(int button){ //check the second element of the entered[] array
if (entered[1] != 0){ //if it is not a zero, i.e. it has already been inputted
checkEntered3(button); //move on to checkEntered3, passing it "button"
}
else if(entered[1] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
entered[1] = button; //set the second element as the button that has been pressed
Serial.print("2: ");Serial.println(entered[1]); //for debugging
}
}
void checkEntered3(int button){ //check the third element of the entered[] array
if (entered[2] != 0){ //if it is not a zero, i.e. it has already been inputted
checkEntered4(button); //move on to checkEntered4, passing it "button"
}
else if (entered[2] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
entered[2] = button; //set the third element as the button that has been pressed
Serial.print("3: ");Serial.println(entered[2]); //for debugging
}
}
void checkEntered4(int button){ //check the fourth element of the entered[] array
if (entered[3] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
entered[3] = button; //set the final element as the button that has been pressed
Serial.print("4: ");Serial.println(entered[3]); //for debugging
delay(100); //allow time for processing
compareCode(); //call the compareCode function
}
}
void checkEntered5(int button /* define the 1,2,3 or 4 as an integer called button */){ //check the first element of the entered[] array
if (alarmoff[0] != 0){ //if it is not a zero, i.e. it has already been inputted
checkEntered6(button); //move on to checkEntered2, passing it "button"
}
else if(alarmoff[0] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
alarmoff[0] = button; //set the first element as the button that has been pressed
Serial.print("5: ");Serial.println(alarmoff[0]); //for debugging
}
}
void checkEntered6(int button /* define the 1,2,3 or 4 as an integer called button */){ //check the first element of the entered[] array
if (alarmoff[1] != 0){ //if it is not a zero, i.e. it has already been inputted
checkEntered7(button); //move on to checkEntered2, passing it "button"
}
else if(alarmoff[1] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
alarmoff[1] = button; //set the first element as the button that has been pressed
Serial.print("6: ");Serial.println(alarmoff[1]); //for debugging
}
}
void checkEntered7(int button /* define the 1,2,3 or 4 as an integer called button */){ //check the first element of the entered[] array
if (alarmoff[2] != 0){ //if it is not a zero, i.e. it has already been inputted
checkEntered8(button); //move on to checkEntered2, passing it "button"
}
else if(alarmoff[2] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
alarmoff[2] = button; //set the first element as the button that has been pressed
Serial.print("7: ");Serial.println(alarmoff[2]); //for debugging
}
}
void checkEntered8(int button){ //check the fourth element of the entered[] array
if (alarmoff[3] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
alarmoff[3] = button; //set the final element as the button that has been pressed
Serial.print("4: ");Serial.println(alarmoff[3]); //for debugging
delay(100); //allow time for processing
compareCode1(); //call the compareCode function
}
}
void compareCode(){ //checks if the code entered is correct by comparing the code[] array with the entered[] array
if ((entered[0]==code[0]) && (entered[1]==code[1]) && (entered[2]==code[2]) && (entered[3]==code[3])){ //if all the elements of each array are equal
alarmCode();
}
else { //if you (or the intruder) get the code wrong
flash(); //call the flash function
}
}
void compareCode1(){ //checks if the code entered is correct by comparing the code[] array with the entered[] array
if ((alarmoff[0]==code[0]) && (alarmoff[1]==code[1]) && (alarmoff[2]==code[2]) && (alarmoff[3]==code[3])){ //if all the elements of each array are equal
asm volatile (" jmp 0");
}
else { //if you (or the intruder) get the code wrong
flash1(); //call the flash function
}
}
}
void flash(){
digitalWrite(redLed, LOW);
delay(250);
digitalWrite(redLed, HIGH);
delay(250);
digitalWrite(redLed, LOW);
delay(250);
digitalWrite(redLed, HIGH);
delay(250);
digitalWrite(redLed, LOW);
delay(250);
digitalWrite(redLed, HIGH);
delay(250);
digitalWrite(redLed, LOW);
delay(250);
digitalWrite(redLed, HIGH);
delay(250);
asm volatile (" jmp 0");
}
void flash1(){
digitalWrite(redLed, LOW);
delay(250);
digitalWrite(redLed, HIGH);
delay(250);
digitalWrite(redLed, LOW);
delay(250);
digitalWrite(redLed, HIGH);
delay(250);
digitalWrite(redLed, LOW);
delay(250);
digitalWrite(redLed, HIGH);
delay(250);
digitalWrite(redLed, LOW);
delay(250);
digitalWrite(redLed, HIGH);
delay(250);
alarm();
}
void alarm(){
digitalWrite(greenLed,LOW);
digitalWrite(redLed,HIGH);
for (int i=0; i<10000; i++){
analogWrite(6, 50);
Serial.print("x");
if (digitalRead(button5) == LOW){ //if button1 is pressed
checkEntered5(1); //call checkEntered and pass it a 1
delay(250);//wait, needed for correct functioning, otherwise
//buttons are deemed to be pressed more than once
}
else if (digitalRead(button6) == LOW){ //if button2 is pressed
checkEntered5(2); //call checkEntered1 and pass it a 2
delay(250); //wait
}
else if (digitalRead(button7) == LOW){ //if button3 is pressed
checkEntered5(3); //call checkEntered1 and pass it a 3
delay(250); //wait
}
else if (digitalRead(button8) == LOW){ //if button4 is pressed
checkEntered5(4); //call checkEntered1 and pass it a 4
delay(250); //wait
}
}
}