hello, i'm making a fun compact build for my endproject, its a box that will give a green light if opened. everyting is done exept the code. here's where stuck. i have a box that uses 2 hc-sr501 and an LCD 16x2 these are conected to 2 simple led lights to indicate if the door is open or closed. but i cant get my first sensor activated to then give some text and waits for the other sensor to detect movement to 'desinfect' your hands. can someone pls help me. this is what i got so far.
#include <Wire.h>
#include <LCD_I2C.h>
LCD_I2C lcd(0x3D);
int inputPir1 = 2;
int pirState1 = LOW;
int vpir1 = 0;
int inputPir2 = 3;
int pirState2 = LOW;
int vpir2 = 0;
int ledPin1 = 13
int ledPin2 = 12
void setup() {
// put your setup code here, to run once:
pinMode(inputPir1, INPUT);
pinMode(inputPir2, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
void loop() {
vpir1 = digitalRead (inputPir1);
if (vpir1 == HIGH){
lcd.begin(); // If you are using more I2C devices using the Wire library use lcd.begin(false)
// this stop the library(LCD_I2C) from calling Wire.begin()
lcd.backlight();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("desinfect");
lcd.setCursor(1, 1);
lcd.print("hands");
delay(1000);
if (vpir2 == HIGH);{
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
delay(5000);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin2, HIGH);
}else{
digitalWrite(ledPin1, HIGH);
}
}else{
digitalWrite(ledPin1, HIGH);
}
}
Can you get both of the PIR's to give a usable output trigger that you measure with e.g. a digital multimeter or if needs be with a signal led? So without an Arduino connected at all?
Btw, the lcd.begin() and lcd.backlight can be moved to setup(). You don't need to repeat those all the time as you go through the loop.
You're also aware I hope that during both delays (1000 resp. 5000) the system will be non-responsive to any inputs?
With the first part I mean, try testing (and understanding) how to use the PIR's without the additional complicating factor of having a microcontroller and your software. Just the PIR sensor on its own, connected to +5V and GND, and try and get it to give you 5V on its digital output pin as you move something close to the sensor. Unless you manage that, you never really know where you're stuck - in the code, or the hardware.
During any delay, the microprocessor is effectively locked. It waits until the delay is over, in the meantime doing absolutely nothing (well, for all intents and purposes) and certainly won't read its input pins etc.
i'v got everything working except that when my firts PIR activates and the text comes up on it the second one just turns on without any input`#include <Wire.h> #include <LCD_I2C.h>
LCD_I2C lcd(0x3D);
int inputPir1 = 2;
int pirState1 = LOW;
int vpir1 = 0;
int inputPir2 = 3;
int pirState2 = LOW;
int vpir2 = 0;
int ledPin1 = 13;
int ledPin2 = 12;
void setup() {
// put your setup code here, to run once:
pinMode(inputPir1, INPUT);
pinMode(inputPir2, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
Serial.begin(9600);
}
void loop() {
vpir1 = digitalRead (inputPir1);
if (vpir1 == 1);{
Serial.println("ja 1");
lcd.begin(); // If you are using more I2C devices using the Wire library use lcd.begin(false)
// this stop the library(LCD_I2C) from calling Wire.begin()
lcd.backlight();
lcd.backlight();
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("aub handen");
lcd.setCursor(1, 1);
lcd.print("desinfecteren");
delay(1000);
vpir2 = digitalRead (inputPir2);
Serial.print(vpir2);
if (vpir2 == 1);{
Serial.println("ja 2");
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
delay(5000);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin2, LOW);
lcd.clear();
}
Did you test the 2nd PIR as well without an Arduino connected to it to verify it operates as intended (and its sensitivity is set correctly for your measured distance)?
Did you test the setup by giving a high pulse to pin 3 directly from 5V (e.g. by using a dupont/jumper wire) instead of the PIR sensor so that you know your code is working as intended?
yes i did those things. but i think there's something in my code that makes it not want to read the value of the second PIR and just goes over the sensor part. i just checked and they both work perfectly fine by themselves but as soon as i put them behind each other the second one just doesn't look for any movement and goes straight to the prosess when it's high.
Ok, so you did both of the things I suggested? And the second part (i.e. using the Arduino without the second PIR sensor and 'faking' its signal instead by touching pin 3 to 5V), did that work OK or did it not give the intended behavior?
So in principle, your sensor works, and your code works. Then there bust be a problem with interfacing the sensor to the arduino. Can you show (schematic and/or photos) how you connected it all together?