Alarm System

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Mjay86
 
Posts: 3
Joined: Mon Feb 12, 2018 3:06 am

Alarm System

Post by Mjay86 »

Hi Guys ,

I try to build a alarm system and i have trouble with the code.
I can turn the alarm system off when it is on or triggered buy the laser.
But i can't get it back on when it is not active.

please let me know if you need more information

i use a arduino mega 2650

Thank you

Code: Select all


#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
//* is to validate password   
//# is to reset password attempt

/////////////////////////////////////////////////////////////////

#include <Password.h>
#include <Keypad.h> 

int alarmactivated= 0;//when it is triggered status

int alarm =1;//alarm status
int k= 10;
int red = 45;
int green = 44;
int blue = 43;
int laser= 42;
int photocellPin = A1;     // the cell and 10K pulldown are connected to a0
int photocellReading;     // the analog reading from the analog resistor divider
Password password = Password( "1111" );
//

const byte ROWS = 4; // Four rows
const byte COLS = 4; //  columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[ROWS] = { 53,52,51,50 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = { 49,48,47,46, };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.


// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
  Serial.begin(9600);
  lcd.begin(16,2);
  lcd.init();
  lcd.backlight();
  Serial.begin(9600);
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
  pinMode(red,OUTPUT);
  pinMode(blue,OUTPUT);
  pinMode (laser,OUTPUT);
  digitalWrite (laser,HIGH);
 
}

void loop(){

  Serial.println("Analog reading = ");
  Serial.print(photocellReading);     // the raw analog reading
  keypad.getKey();

  if (alarm ==1){
photocellReading = analogRead(photocellPin);
   digitalWrite (laser,HIGH);
      
    lcd.setCursor(0,0);
    lcd.print("ALARM ON");
    keypad.getKey();
    digitalWrite(blue,HIGH);
    digitalWrite(green,LOW);
    digitalWrite (red,LOW);
  
    }
    if (alarm==1 && photocellReading >500){
         digitalWrite(blue,LOW);
    digitalWrite(green,LOW);
    digitalWrite (red,HIGH);
    lcd.setCursor(0,0);
    lcd.print("ALARM triggered");
    alarm = 3;
    alarmactivated =1;
    
    }
    if (alarmactivated ==1){
          digitalWrite(blue,LOW);
    digitalWrite(green,LOW);
    digitalWrite (red,HIGH);
    alarm=3;
    
    
      
    }
    
    if (alarm ==0) {
      //lcd.clear(); 
      lcd.setCursor(0,0);
      lcd.print("code to Activate");
       keypad.getKey();
       digitalWrite(blue,LOW);
       digitalWrite(green,HIGH);
       digitalWrite(red,LOW);
       digitalWrite(laser,LOW);
   
    
       
   }
 
}
    
    


//take care of some special events
void keypadEvent(KeypadEvent eKey){
  switch (keypad.getState()){
    case PRESSED:
    //lcd.clear();
    lcd.setCursor(0,1);
    lcd.print("PRESSED: ");
    lcd.setCursor(k,1);
    lcd.print(eKey);
    k++;
	Serial.print("Pressed: ");
	Serial.println(eKey);
	switch (eKey){
    case 'A': break ;
	  case '*': checkPassword();k=10; break;
	  case '#': password.reset();k=10; break;
	  default: password.append(eKey);
    
 
     }
  }
}

void checkPassword(){
  if (password.evaluate()){
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Success");
    delay(2000),
    lcd.clear();
    Serial.println("Success");
  }
  
  if (alarm ==0 && password.evaluate()){//*******That is the code that don't work for some reason , i casn't get out of the alarm 0 and change to 1
  digitalWrite(laser,HIGH); 
  int countdown = 4; // 9 seconds count down before activating the alarm
   alarm++;//// soposed to change to 1 but it dosen't work 
    while (countdown != 0) {
     lcd.clear();
     lcd.setCursor(0,0);
      lcd.print("Alarm activate in");
      lcd.setCursor(13,1);
      lcd.print(countdown);
     countdown--;
    
   delay(1000);

   }
     alarm=1;
}

if (alarmactivated ==1 &&password.evaluate()){
  delay(100);
  alarm=0;
  alarmactivated =0;
  
}
 if (alarm==1 && password.evaluate()){
  delay(100);
    alarm =0;
    alarmactivated =0;
   }   

   


// } 
  
  if(!password.evaluate()){
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Wrong");
    Serial.println("Wrong");
    //add code to run if it did not work
  }
  }

User avatar
kcl1s
 
Posts: 1512
Joined: Tue Aug 30, 2016 12:06 pm

Re: Alarm System

Post by kcl1s »

OK. First I got a compile error for a comma instead instead of a semicolon on the delay line

Code: Select all

  if (password.evaluate()) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Success");
    delay(2000),
    lcd.clear();
    Serial.println("Success");
  }
Here is your main problem. This code sets alarm to (1) if true

Code: Select all

  if (alarm == 0 && password.evaluate()) { //*******That is the code that don't work for some reason , i casn't get out of the alarm 0 and change to 1
    digitalWrite(laser, HIGH);
    int countdown = 4; // 9 seconds count down before activating the alarm
    alarm++;//// soposed to change to 1 but it dosen't work
    while (countdown != 0) {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Alarm activate in");
      lcd.setCursor(13, 1);
      lcd.print(countdown);
      countdown--;

      delay(1000);

    }
    alarm = 1;
  }
Just a few lines down this if statement will always change it back to (0)

Code: Select all

  if (alarm == 1 && password.evaluate()) {
    delay(100);
    alarm = 0;
    alarmactivated = 0;
  }
Hope this helps. Just curious, why are you a on an Adafruit support forum but don't mention using any of their parts?

Fellow Hobbyist
Keith

User avatar
Mjay86
 
Posts: 3
Joined: Mon Feb 12, 2018 3:06 am

Re: Alarm System

Post by Mjay86 »

Hi,

Thank you for your reply

kcl1s wrote:OK. First I got a compile error for a comma instead instead of a semicolon on the delay line

Code: Select all

  if (password.evaluate()) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Success");
    delay(2000),
    lcd.clear();
    Serial.println("Success");
  }
Thanks

kcl1s wrote: Here is your main problem. This code sets alarm to (1) if true

Code: Select all

  if (alarm == 0 && password.evaluate()) { //*******That is the code that don't work for some reason , i casn't get out of the alarm 0 and change to 1
    digitalWrite(laser, HIGH);
    int countdown = 4; // 9 seconds count down before activating the alarm
    alarm++;//// soposed to change to 1 but it dosen't work
    while (countdown != 0) {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Alarm activate in");
      lcd.setCursor(13, 1);
      lcd.print(countdown);
      countdown--;

      delay(1000);

    }
    alarm = 1;
  }

If the alarm is set on 0 and the password is correct shouldn't the alarm be back to 1 ?
I don't understand why that is the main problem ?!
kcl1s wrote: Just a few lines down this if statement will always change it back to (0)

Code: Select all

  if (alarm == 1 && password.evaluate()) {
    delay(100);
    alarm = 0;
    alarmactivated = 0;
  }
But it only changes back to 0 with the correct password or not ?
kcl1s wrote: Just curious, why are you a on an Adafruit support forum but don't mention using any of their parts?
sorry i didn't know i have to mention the parts i bought
I bough
Lcd display (used in that code )
i2c LCD backpack (used in that code )
4 Channels Wireless Remote Control 315MHz(i will use it later in my project)
5mm RGB Led module (used in that code )
5mW Red Laser module - Low power consumption (used in that code )
R Motion sensor module (i will use it later in the project )
Membrane keypads 16 buttons (used in that code )
MEGA2560 R3 (used )
all parts are from adafruit .

User avatar
kcl1s
 
Posts: 1512
Joined: Tue Aug 30, 2016 12:06 pm

Re: Alarm System

Post by kcl1s »

Hi,
It is the combination of the 2 if statements that is the problem.
This if is OK

Code: Select all

if (alarm == 0 && password.evaluate())
if true it sets alarm=1 which I assumes is what you want but then the code continues and the second if

Code: Select all

 if (alarm==1 && password.evaluate())
will always be true ( if the code executed the first if ) which then sets alarm back to 0.

If you only want one of the if choices to run you might want to check out the else if statement. https://www.arduino.cc/reference/en/lan ... ture/else/
That way you only execute the first true statement then it skips the rest.

Hope this helps
Keith

User avatar
Mjay86
 
Posts: 3
Joined: Mon Feb 12, 2018 3:06 am

Re: Alarm System

Post by Mjay86 »

Thanks a lot . I fixed it , I can't believe it was that easy to fix .

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”