This is a copy of another topic
https://forum.arduino.cc/index.php?topic=481404.0
I know it is better to learn Arduino, but I will not be using Arduino for anything else in the future, and I dont have much time now.
I have added two PIR sensors to this circuit with an LCD.
Both PIR sensors are working fine and LCD is showing the correct PIR - only if a single PIR is active.
Problem-1. If the PIR1 and PIR2 gets active at the same time, and PIR1 goes off - it will still work as both PIRs are on, both LED will be on and buzzer will sound!!
In short when both sensors are active, Arduino cant sense which one went off later!!
Problem-2. second issue is the LCD is not displaying some text correctly.
If possible. It will be great if the system can display PIR1 status on line 1 and PIR2 status on line 2.
Can anyone help please to fix the two problems and add the extra feature ?
Here is a virtual circuit link to see the sketch and connections.
Here is the code
#include <LiquidCrystal.h>
int ledPin = 13; // choose the pin for the LED
int ledPinB = 12; // choose the pin for the LED 2
int inputPin = 6; // choose the input pin (for PIR sensor)
int inputPinB = 7; // choose the input pin (for PIR sensor 2)
int pirState = LOW; // we start, assuming no motion detected
int pirStateB = LOW; // we start, assuming no motion detected PIR2
int val1 = 0; // variable for reading the pin status
int val2 = 0; // variable for reading the pin status
int pinSpeaker = 10; // Set up a speaker on a PWM pin (digital 9, 10, or 11)
LiquidCrystal lcd(9, 8, 5, 4, 3, 2);
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(ledPinB, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
pinMode(inputPinB, INPUT); // declare sensor as input
pinMode(pinSpeaker, OUTPUT);
Serial.begin(9600);
lcd.setCursor(0, 0); // Set LCD cursor position (column, row)
lcd.print("Security"); // Print text to LCD
lcd.setCursor(0, 1); // Set LCD cursor position (column,row)
lcd.print("System"); // Print text to LCD
delay(1000); // wait 1s Delay to read text
lcd.clear(); // clear LCD display
lcd.setCursor(0, 0); // Set LCD cursor position (column, row)
lcd.print("System Booting");
delay(1000); // Delay to read text
lcd.clear(); // Clear LCD
lcd.setCursor(0, 0);
lcd.print("Processing...");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Waiting For");
lcd.setCursor(0, 1);
lcd.print("Motion");
}
void loop(){
val1 = digitalRead(inputPin); // read input value
val2 = digitalRead(inputPinB); // read input value
if (val1 == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
playTone(300, 160);
delay(150);
if (pirState == LOW) {
// just turned on
Serial.println("Motion detected!");
lcd.clear() ;
lcd.setCursor(0, 0); // Set LCD cursor position (column 0, row 0)
lcd.print("PIR 1 Active!!");
// only want to print on the output change, not state
pirState = HIGH;
}
}
else if (val2 == HIGH) { // check if the input is HIGH
digitalWrite(ledPinB, HIGH); // turn LED ON
playTone(300, 160);
delay(150);
if (pirStateB == LOW) {
// just turned on
Serial.println("Motion detected!");
lcd.clear() ;
lcd.setCursor(0, 1); // Set LCD cursor position (column 0, row 0)
lcd.print("PIR 2 Active!!");
// only want to print on the output change, not state
pirStateB = HIGH;
}
}
else {
digitalWrite(ledPin, LOW); // turn LED OFF
digitalWrite(ledPinB, LOW); // turn LED OFF
playTone(0, 0);
delay(300);
if (pirState == HIGH){
// just turned off
Serial.println("Motion ended!");
lcd.clear() ;
lcd.setCursor(3, 0);
lcd.print("Waiting For");
lcd.setCursor(3, 1);
lcd.print("Motion."); // only want to print on the output change, not state
// only want to print on the output change, not state
pirState = LOW;
}
}
}
// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
duration *= 1000;
int period = (1.0 / freq) * 1000000;
long elapsed_time = 0;
while (elapsed_time < duration) {
digitalWrite(pinSpeaker,HIGH);
delayMicroseconds(period / 2);
digitalWrite(pinSpeaker, LOW);
delayMicroseconds(period / 2);
elapsed_time += (period);
}
}
No need to use the same sketch, anyone may add a completely new one!
All I need is two PIRs, two LEDs, one LCD and one buzzer - without bugs like my code.
believe me , I have already spend hours on this and couldn't fix it.