Can someone please help me with the code for 2 PIR sensors and 2 led's for smart street lighting
Welcome to the forum
What have you tried so far ?
I think the problem is in the code if you can help me.
Then post the code that you have a problem with and describe the problem
Why does nobody read and follow the advice they are directed to when they join the forum ?
Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'
Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination
```cpp
#include <LiquidCrystal_I2C.h>
int led = 13;
int led2 = 5;
int sensor = 3;
int sensor2 = 7;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(sensor2, INPUT);
pinMode(sensor, INPUT);
Serial.begin(9600);
}
void loop() {
int sensor2val = digitalRead(sensor2);
int sensorval = digitalRead(sensor);
if (sensor2val == HIGH) {
digitalWrite(led2, HIGH);
delay(100);
lcd.init();
lcd.clear();
lcd.backlight(); // Make sure backlight is on
// Print a message on both lines of the LCD.
lcd.setCursor(2, 0); //Set cursor to character 2 on line 0
lcd.print("MOTION");
lcd.setCursor(2, 1); //Move cursor to character 2 on line 1
lcd.print("DETECTED");
delay(5000);
} else if (sensorval == HIGH) {
digitalWrite(led, HIGH);
delay(100);
lcd.init();
lcd.clear();
lcd.backlight(); // Make sure backlight is on
// Print a message on both lines of the LCD.
lcd.setCursor(2, 0); //Set cursor to character 2 on line 0
lcd.print("MOTION");
lcd.setCursor(2, 1); //Move cursor to character 2 on line 1
lcd.print("DETECTED");
delay(5000);
} else if (sensor2val == LOW) {
digitalWrite(led2, LOW);
lcd.init();
lcd.clear();
lcd.backlight(); // Make sure backlight is on
// Print a message on both lines of the LCD.
lcd.setCursor(2, 0); //Set cursor to character 2 on line 0
lcd.print("no motion");
lcd.setCursor(2, 1); //Move cursor to character 2 on line 1
lcd.print("DETECTED");
} else if (sensor2val == LOW) {
digitalWrite(led, LOW);
lcd.init();
lcd.clear();
lcd.backlight(); // Make sure backlight is on
// Print a message on both lines of the LCD.
lcd.setCursor(2, 0); //Set cursor to character 2 on line 0
lcd.print("no motion");
lcd.setCursor(2, 1); //Move cursor to character 2 on line 1
lcd.print("DETECTED");
}
}
is it ok? I'm new and in a bit of hurry sorry. BTW the problem is the 2nd led won't turn off after detecting motion if you can help
You have 2 else if statements with the same condition (sensor2val == LOW). The 2nd one will NEVER get executed.
You do realize ??? that when you have an if-else-if structure that only code for ONE of the conditions is executed and that is the first true condition that is encountered.
So can you suggest something else please. I'm pretty new to Arduino if you can give me a solution it'll be really helpful
'tis does seem like a rather common infection. Maybe the forum s/w should send an email with all that must read and the link and at the end a temporary password BEFORE posting is allowed.
Or not. I'm pretty much of the opinion that half the questions are homework or school projects and half of what left is strange, weird, and ill-conceived wants; example, "I want to build ..." No research, no concept, just "I want..."
When we old timers die off, programming will just be cut&paste and the Arduino AI will grind out answers.
What I really want to know is why schools are not teaching web research using Internet search?
What I want to know is why web research is so complicated you would even need to teach it. Good grief, when I was in college there was no Google. I had Kernighan and Ritchie and a host of other references. I still assembled and programmed personal projects by library research.
I would love to help more but the only description of what you want the code to do is the following:
That is not enough to help you restructure your code. A clear and concise description of how you want your project to behave is necessary to help.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.

