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
}
}