Question Regarding a Distance Alarm

Basically I am creating an alarm that is triggered when my distance sensor reads a value under a certain threshold. I got the activation code working, where you have to press 4 buttons in a correct sequence to activate the alarm. Now what I am trying to do is to disable the alarm, while it is running (playing a tone and red LED is on), with the same code that you turned it on with. I can’t really think of an easy way to do that, so I just tried to take the code that reads the buttons and checks if the sequence is correct and put it into the alam(); portion of my code. This returns with:

Alarm_final:205: error: a function-definition is not allowed here before ‘{’ token
Alarm_final:258: error: expected `}’ at end of input

Any better ideas on how to get this to work or how to fix the error code?

Also, this is not my entire code, I wasn’t able to put it all in the post because it would exceed the 9000 character limit. If you need to know more about what I am trying to do, please message me, I am really stuck on this one.

This is my code:

#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
float sinVal;
int toneVal;
boolean triggered = false;
int calibrationTime = 30;        
long unsigned int lowIn;         
long unsigned int pause = 5000;  
boolean lockLow = true;
boolean takeLowTime;  
int pirPin = 5; 
DistanceSRF04 Dist;
int distance;
int button1 = 8; //first button is on pin 8
int button2 = 9; //second is on pin 9
int button3 = 10; //third is pin 10
int button4 = 11; //fourth is pin 11
int redLed = 4; //red LED is on pin 4
int greenLed = 12; //green LED is pin 12
int pinSpeaker = 6; //speaker LED is pin 13

int code[] = {1,2,3,4}; //the desired code is entered in this array,
                        //separated by commas

int entered[4]; //create a new empty array for the code entered by
                //the user (has 4 elements)

void setup(){ //run once at sketch startup
  Serial.begin(9600); //begin Serial

  pinMode(button1, INPUT); //button 1 is an input
  pinMode(button2, INPUT); //button 2 is an input
  pinMode(button3, INPUT); //button 3 is an input
  pinMode(button4, INPUT); //button 4 is an input
  pinMode(redLed, OUTPUT); //the red LED is an output
  pinMode(greenLed, OUTPUT); // the green LED is an output
  pinMode(pinSpeaker,OUTPUT);//the speakerpin is an output
  setupLights(); //run the setupLights routine
  setupLights(); //run it again
  delay(650); //delay (only for effect, :P not needed)
  digitalWrite(redLed, HIGH); //turn the red LED on
  Dist.begin(2,3);

  //give the sensor some time to calibrate
  //Serial.print("calibrating sensor ");
   // for(int i = 0; i < calibrationTime; i++){
     // Serial.print(".");
      //delay(1000);
      //}
   // Serial.println(" done");
   // Serial.println("SENSOR ACTIVE");
    //delay(50);
}

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 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
   
     for (int x = 0; x++;);
     alarmCode();
  }
  
  else { //if you (or the intruder) get the code wrong
    flash(); //call the flash function
    }
}    
void flash(){ // this is basically the blink example, look at that for an explantion of this, I wont insult your intelligence... oh wait, I already did that earlier when explaining the green LED flashing... sorry 'bout that
  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);
}

void setupLights(){ // a funky display run at reset
  digitalWrite(redLed, HIGH); //turn both
  digitalWrite(greenLed, HIGH); //LEDs on
  delay(75); //wait
  digitalWrite(redLed, LOW); //turn them
  digitalWrite(greenLed, LOW); //off again
  delay(75); //wait
}

void alarmCode(){
  digitalWrite(greenLed,HIGH);
  for (int i = 0; i<10000; i++){ 
    if(triggered == true){
      alarm();
    }
    else{
      delay(50);// Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
    }
      if(distance < 50){
         triggered = true;
      }
    }
  }
  
void alarm(){
  digitalWrite(greenLed,LOW);
  digitalWrite(redLed,HIGH);
  for (int i=0; i<100000; i++){
    tone(6, 3000);
 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
  }
  
}
    checkEntered1(1); //call checkEntered and pass it a 1

Really? I'd have never guessed.

  if (entered[0] != 0)
  {
  }
  else if(entered[0] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet

If the value is not 0, is there a snowballs chance in hell that it will change to 0 before you check again?

     for (int x = 0; x++;);

You REALLY need to explain this. A for loop has THREE clauses and NO semicolon.

You REALLY need to explain this. A for loop has THREE clauses and NO semicolon.

In response to that, it was just an idea I had to use an x value, so that when the code is checked twice (correct sequence entered while the alarm is running (x=2)), it will reset the code. Well that was my thinking at least and I really have not gotten to the point where I need to test that. Otherwise, that has not given me any issues.

My main issue is trying to get the deactivation sequence read while the alarm is going off so it can be deactivated. Basically, having this code everything works as it is suppose to (the speaker doesn’t work but I will deal with that later).

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
float sinVal;
int toneVal;
boolean triggered = false;
int calibrationTime = 30;        
long unsigned int lowIn;         
long unsigned int pause = 5000;  
boolean lockLow = true;
boolean takeLowTime;  
int pirPin = 5; 
DistanceSRF04 Dist;
int distance;
int button1 = 8; //first button is on pin 8
int button2 = 9; //second is on pin 9
int button3 = 10; //third is pin 10
int button4 = 11; //fourth is pin 11
int redLed = 4; //red LED is on pin 4
int greenLed = 12; //green LED is pin 12
int pinSpeaker = 6; //speaker LED is pin 13

int code[] = {1,2,3,4}; //the desired code is entered in this array,
                        //separated by commas

int entered[4]; //create a new empty array for the code entered by
                //the user (has 4 elements)

void setup(){ //run once at sketch startup
  Serial.begin(9600); //begin Serial

  pinMode(button1, INPUT); //button 1 is an input
  pinMode(button2, INPUT); //button 2 is an input
  pinMode(button3, INPUT); //button 3 is an input
  pinMode(button4, INPUT); //button 4 is an input
  pinMode(redLed, OUTPUT); //the red LED is an output
  pinMode(greenLed, OUTPUT); // the green LED is an output
  pinMode(pinSpeaker,OUTPUT);//the speakerpin is an output
  setupLights(); //run the setupLights routine
  setupLights(); //run it again
  delay(650); //delay (only for effect, :P not needed)
  digitalWrite(redLed, HIGH); //turn the red LED on
  Dist.begin(2,3);

  //give the sensor some time to calibrate
  //Serial.print("calibrating sensor ");
   // for(int i = 0; i < calibrationTime; i++){
     // Serial.print(".");
      //delay(1000);
      //}
   // Serial.println(" done");
   // Serial.println("SENSOR ACTIVE");
    //delay(50);
}

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 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
   
     for (int x = 0; x++;);
     alarmCode();
  }
  
  else { //if you (or the intruder) get the code wrong
    flash(); //call the flash function
    }
}    
void flash(){ // this is basically the blink example, look at that for an explantion of this, I wont insult your intelligence... oh wait, I already did that earlier when explaining the green LED flashing... sorry 'bout that
  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);
}

void setupLights(){ // a funky display run at reset
  digitalWrite(redLed, HIGH); //turn both
  digitalWrite(greenLed, HIGH); //LEDs on
  delay(75); //wait
  digitalWrite(redLed, LOW); //turn them
  digitalWrite(greenLed, LOW); //off again
  delay(75); //wait
}

void alarmCode(){
  digitalWrite(greenLed,HIGH);
  for (int i = 0; i<10000; i++){ 
    if(triggered == true){
      alarm();
    }
    else{
      delay(50);// Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
    }
      if(distance < 50){
         triggered = true;
      }
    }
  }
  
void alarm(){
  digitalWrite(greenLed,LOW);
  digitalWrite(redLed,HIGH);
  for (int i=0; i<100000; i++){
    tone(6, 3000);
  }
  }

But when I try to introduce the deactivation sequence into the void alarm(); by basically putting everything from the original void loop() function into it, it refuses to work. There is probably a much easier way that I have not thought of yet.

That compile error doesn't necessarily mean your thinking is flawed. Why not try to fix the compile error: your logic may well be good.

So, look at the error.... has to do with curly braces, so check if they're balanced. You have about a gazillion of them, so best way to check is to park your cursor next to one and it will highlight its partner. If you do that, you'll find that this guy....

void alarm(){

... seems to have no partner and I think you need to whack a } on here:

else if (digitalRead(button4) == LOW){ //if button4 is pressed
    checkEntered1(4); //call checkEntered1 and pass it a 4
    delay(250); //wait
  }

}

}  //add a new one?

When I tried to compile I get errors due to not having the libraries, but at least the error you quoted went away when I added the missing }

I find it useful, with so many ifs and fors and functions, to label the closing braces with a comment to help keep track visually eg:

} //end of if
}   //end of loop

So my advice is to sort the compile error, then see if the logic actually does what you expect.

Jimbo, thanks for the help but it did not solve my problem. I went through the code and made sure that every { was closed and in the correct spot and they were (unless I missed something, which is possible). Also, the code in my first post is not all of it, it could not fit with all of the characters. I attached the code to this post if you want to give it another look, otherwise, thanks again for the help.

Alarm_final.ino (8.82 KB)

I think I may have figured it out, I was trying to put functions within functions and then call those functions. I just needed to include

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
  }

within the void alarm() function. I haven't had a chance to test, but the error codes went away.