Dual RCWL-0516 programming

New Arduino user here so be gentle

I am an electrical technician and have decided to look into the world of Arduino and circuit building and working on various little projects to keep the mind active, sadly my knowledge of programming and coding is non-existent but I am reading up on it and learning slowly.

I tried a new project which is a motion sensor, using the RCWL-0516 microwave motion sensor with the objective of setting up a detector to monitor two separate locations and alert if movement is detected at either one of the sensors.

The circuit is fine and functioning properly with power applied to the board, however, I have encountered a problem in my coding. Currently if one of the sensors detects movement then both LEDs light which ruins the point of just one LED lighting for the specific sensor picking up motion.

Any advice or help as to where the problem lies in my coding would be much appreciated if I have missed any information needed to help understand the problem or how to provide a solution let me know

#define SensorPin1 2
#define SensorPin2 4
#define LEDPin1 12
#define LEDPin2 13


void setup() {
  // put your setup code here, to run once:
pinMode (SensorPin1, INPUT);
pinMode (SensorPin2, INPUT);
pinMode (LEDPin1, OUTPUT);
pinMode (LEDPin2, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
int sensorValue = digitalRead(SensorPin1);
if(sensorValue == HIGH){
  digitalWrite(LEDPin1, HIGH);
 
}
 else {
  digitalWrite(LEDPin1, LOW);
 }
 {
  int sensorValue = digitalRead(SensorPin2);
 }
if(sensorValue == HIGH){
  digitalWrite(LEDPin2, HIGH);
 
}
 else {
  digitalWrite(LEDPin2, LOW);
}}

Welcome to the forums! First bit of advise - read the sticky posts at the top of the forum about how to properly post your code using code tags so other people can help you. A picture of code is not useful.

Apologies, I am not the most computer literate, I have altered to show the coding in the post

Kaiplex:

 ...

{
  int sensorValue = digitalRead(SensorPin2);
}

For some reason you decided to redeclare int sensorValue in the middle of your code. Apparently, the compiler told you that you cannot declare the same variable again in the same scope. And in order to fix this error you enclosed the second declaration into a pair of {}.

The compiler error disappeared, but the program did not behave the way you wanted it to behave. This is what happens when you “fix” compilation errors by random and misguided modifications of the code.

By adding this extra pair of {}, you effectively “isolated” this second sensorValue variable from the outside world. Your ifs don’t see this second variable. All of your ifs see the first bariable and work with the first variable. This is why both of your LEDs react to the first sensor only.

There are several ways to fix it. If you want to use the same sensorValue variable in both cases, then don’t attempt to redeclare it. Simply reassign it

int sensorValue = digitalRead(SensorPin1); // declaration, first sensor read

if(sensorValue == HIGH)
  digitalWrite(LEDPin1, HIGH);
else
  digitalWrite(LEDPin1, LOW);

sensorValue = digitalRead(SensorPin2); // assignment, second sensor read

if(sensorValue == HIGH)
  digitalWrite(LEDPin2, HIGH);
else
  digitalWrite(LEDPin2, LOW);

Or you can decide to use two different variables

int sensorValue1 = digitalRead(SensorPin1); // declaration, first sensor read

if(sensorValue1 == HIGH)
  digitalWrite(LEDPin1, HIGH);
else
  digitalWrite(LEDPin1, LOW);

int sensorValue2 = digitalRead(SensorPin2); // declaration, second sensor read

if(sensorValue2 == HIGH)
  digitalWrite(LEDPin2, HIGH);
else
  digitalWrite(LEDPin2, LOW);

But it is quite obvious that you don’t really need to use branching in your code

int sensorValue = digitalRead(SensorPin1); // declaration, first sensor read
digitalWrite(LEDPin1, sensorValue);

sensorValue = digitalRead(SensorPin2); // assignment, second sensor read
digitalWrite(LEDPin2, sensorValue);

Yes, I did get a compiler error when I first wrote the program, I had a feeling that the extra "{" I inserted might be the problem, I assumed the error was because the two separate commands had to somehow be separated from each other to run individually, rookie mistakes I guess.

Thank you for the help, I'll learn from these mistakes :slight_smile:

Kaiplex:
Yes, I did get a compiler error when I first wrote the program, I had a feeling that the extra "{" I inserted might be the problem, I assumed the error was because the two separate commands had to somehow be separated from each other to run individually, rookie mistakes I guess.

Extra {} around the variable declaration were indeed a wrong thing to do.

However, the extra {} that you have in your if statements are perfectly fine. You don't have to remove them. I removed them in my version just because I prefer to omit them in such trivial cases. It is a matter of personal preference.

E.g. this is fine

if(sensorValue1 == HIGH)
{
  digitalWrite(LEDPin1, HIGH);
}
else
{
  digitalWrite(LEDPin1, LOW);
}

If you like it better - use it.