I'm having ago at building my own van alarm just to monitor the back of the van, But the PIR sensor is sometimes giving a false activation, I've tried to cure it in software but still get false activation
I've not installed it the van yet and all the testing is been done in my workshop.
I have a flag set so if it's gets activated at some point it then records it to the memory so I know.
I power up the alarm and my last count on the display say is 2 I leave the workshop and do not re-enter and when I do I expect it to go up by one when I enter but the display say may be one or three extra. I know it has been triggered while no one is in there because I can here the bleeper bleeping.
Here is my code
// Include libraries
#include <LiquidCrystal_I2C.h> // I2C LCD display
#include <Wire.h>
#include <EEPROMex.h>//Include EEPROMX writes float values
/////////////////////////////
//VARS
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 60;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
int ledState = LOW;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 2000;
int data;
int Trigger_PIR = 0;
boolean lockLow = true;
boolean takeLowTime;
int time_flag = 0; //Timer flag
unsigned long ActivateTime;
unsigned long previousMillis; // will store last time LED was updated
int Trigger_time = 0;
int Trigger_time1 = 0;
// constants won't change :
const long interval = 1000;
long AlramTime = 1000UL * 60; //sound alram for 10 seconds
int pirPin = 2; //the digital pin connected to the PIR sensor's output
int ledPin = 5;
int Side_door_Switch = 7;
int Rear_door_switch = 8;
/*-----( Declare objects )-----*/
// (Create an instance of a radio, specifying the CE and CS pins. )
/*-----( Declare Variables )-----*/
LiquidCrystal_I2C lcd(0x03F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
void setup() {
lcd.begin (20, 4); // for 20 X 4 LCD module
lcd.setBacklightPin(3, POSITIVE); //set back light pin
lcd.setBacklight(HIGH); //Turn the back light on the LCD
pinMode(pirPin, INPUT);
pinMode(Rear_door_switch, INPUT);
digitalWrite(Rear_door_switch, HIGH);
pinMode(Side_door_Switch, INPUT);
digitalWrite(Side_door_Switch, HIGH);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin, LOW);
// EEPROM.writeInt(10, Trigger_time);
// EEPROM.writeInt(25, Trigger_time1);
// EEPROM.writeInt(15, Trigger_PIR);
Trigger_time = EEPROM.readInt(10); //debugging only monitoring
Trigger_time1 = EEPROM.readInt(25); //debugging only monitoring
Trigger_PIR = EEPROM.readInt(15); //debugging only monitoring
///LCD DISPLAY for debugging only removed one sorted
lcd.setCursor(0, 0);
lcd.print("====================");
lcd.setCursor(0, 1);
lcd.print("STEVE'S VAN GUARD");
lcd.setCursor(0, 2);
lcd.print("====================");
//calibatre the sensor for 1 minute
for (int i = 0; i < calibrationTime; i++) {
lcd.setCursor(0, 3);
lcd.print("CALIBRAITING PIR");
delay(1000);
}
lcd.clear();
}
void loop() {
//#### Rear door switch monitor
if (digitalRead(Rear_door_switch) == LOW) {
time_flag = 1; //set the down falg to 1
Trigger_time = Trigger_time + 1;
ActivateTime = millis();
lcd.setCursor(0, 2);
lcd.print("REAR DOOR TRIGGER");
Alram_triiger();
Alarm_status ();
}
//#### Rear door switch monitor
if (digitalRead(Side_door_Switch) == LOW) {
time_flag = 1; //set the down falg to 1
Trigger_time1 = Trigger_time1 + 1;
ActivateTime = millis();
lcd.setCursor(0, 2);
lcd.print("SIDE DOOR TRIGGER");
Alram_triiger();
Alarm_status ();
}
//#### PIR sensor monitor
boolean tripped = digitalRead(pirPin) == HIGH;
if (tripped==1){
//wait
delay(100);
//read the pin again
tripped = digitalRead(pirPin) == HIGH;
if (tripped==1){
lcd.setCursor(0, 2);
lcd.print("PIR TRIGGER ");
//lcd.clear();
time_flag = 1; //set the down falg to 1
Trigger_PIR = Trigger_PIR + 1;
ActivateTime = millis();
Alram_triiger();
// Alarm_status ();
}
}
//for debuggin only
lcd.setCursor(0, 1);
lcd.print("Rear Door:");
lcd.print(Trigger_time);
lcd.setCursor(0, 2);
lcd.print("side Door:");
lcd.print(Trigger_time1);
lcd.setCursor(0, 3);
lcd.print("PIR :");
lcd.print(Trigger_PIR);
lcd.setCursor(8, 3);
lcd.print("pin :");
lcd.print(tripped);
}
//##alram triigered come here,only for testing not yet complete
void Alarm_status() {
while (time_flag) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
// This is where I will turn a sounder on mounted in side once all working
// but for the time been just send out serial data
lcd.setCursor(0, 0);
lcd.print("ALRAM ACTIVE ");
if ((time_flag == 1) && (millis() - ActivateTime >= AlramTime)) {
if (time_flag == 1)
delay(50);
time_flag = 0;
lcd.clear();
return;
}
}
}
//## write the trigger to memory
void Alram_triiger() {
EEPROM.writeInt(10, Trigger_time);
EEPROM.writeInt(15, Trigger_PIR);
EEPROM.writeInt(25, Trigger_time1);
delay(50);//Trigger_PIR
Trigger_time = EEPROM.readInt(10);
Trigger_PIR = EEPROM.readInt(15);
Trigger_time1 = EEPROM.readInt(25);
delay(25);
}
Is there any way or better way to try and prevent this ?
Thanks
Steve