Two motion sensors connected to LED's (Solved)

Hello,
I have a code that will turn an led on and off based on the output of a motion sensor.
What I am trying to accomplish, is to add another sensor and another LED to the system. (So basically double it). But I can't figure it out.

This is the working code for 1 sensor and 1 led. I also added the pins I would like to use for the second sensor and led at the top.

int sensor1 = 4;  // Pin of IR Motion Sensor
int light1 = 8;  // Pin of led1
int sensor2 = 2;
int light2 = 10;
 
void setup(){
  Serial.begin(9600);
  pinMode(light1, OUTPUT);  // Set Pin connected to ligh1 as an OUTPUT
  digitalWrite(light1, LOW);  // Set Pin to LOW to turn Relay OFF
}
 
void loop(){
  
  while (digitalRead(sensor1) == HIGH) {  // If Motion detected 
       digitalWrite(light1, HIGH);  // Turn light ON
       Serial.println("Green Light (Light1) ON");
       delay(500);
  }
  
       digitalWrite(light2, LOW);// Turn light OFF
       Serial.println("Relay is OFF");
       delay(500);  
}

I got the first light to work without the while loop. But I still can't get the second sensor/light to work.
This is what I have ( I left the prints and delays commented)

char incomingByte; 
int sensor1 = 4;  // Pin of IR Motion Sensor
int light1 = 8;  // Pin of led1
int sensor2 = 5;
int light2 = 10;
 
void setup(){
  Serial.begin(9600);
  pinMode(light1, OUTPUT);  // Set Pin connected to ligh1 as an OUTPUT
  pinMode(light2, OUTPUT); 
  digitalWrite(light1, LOW);  // Set Pin to LOW to turn light OFF
  digitalWrite(light2, LOW);
}
 
void loop(){
  if (digitalRead(sensor1) == HIGH){   // If Motion detected 
       digitalWrite(light1, HIGH);  // Turn light ON
       //Serial.println("Green Light (Light1)is ON");
       //delay(500);
 
       digitalWrite(light1, LOW);// Turn light OFF
       //Serial.println("Light 1 is OFF");
       //delay(500);  
  }

  if (digitalRead(sensor2) == HIGH){   // If Motion detected 
       digitalWrite(light2, HIGH);  // Turn light ON
       //Serial.println("Red Light (Light2)is ON");
       //delay(500);

       digitalWrite(light2, LOW);// Turn light OFF
       //Serial.println("Light 2 is OFF");
       //delay(500);  
  }

}

I have tried different pins , different sensors. And no luck, So im pretty sure the code is bad.

xasuma:
I have tried different pins , different sensors. And no luck, So im pretty sure the code is bad.

Use your arrays!!!!

//char incomingByte;
int sensor1 = 4;  // Pin of IR Motion Sensor
int light1 = 8;  // Pin of led1
int sensor2 = 5;
int light2 = 10;

struct MySensor {
  int sensorPin;
  int ledPin;
};

MySensor mySensor[] = {
  {sensor1, light1},
  {sensor2, light2}
};
int lastState[] = {LOW, LOW};

void setup()
{
  Serial.begin(9600);
  for (int i = 0; i < sizeof(mySensor) / sizeof(mySensor[0]); i++)
  {
    pinMode(mySensor[i].ledPin, OUTPUT);
    pinMode(mySensor[i].sensorPin, INPUT);  // added
    digitalWrite(mySensor[i].ledPin, LOW);
  }
}

void loop()
{
  for (int i = 0; i < sizeof(mySensor) / sizeof(mySensor[0]); i++)
  {
    int sensorState = digitalRead(mySensor[i].sensorPin);
    if(lastState[i] != sensorState)
    {
      if(sensorState == HIGH)
      {
        digitalWrite(mySensor[i].ledPin, HIGH);
        Serial.print(F("Sensor "));
        Serial.print(i);
        Serial.println(F(" Tripped."));
      }
      else
      {
        digitalWrite(mySensor[i].ledPin, LOW);
        Serial.print(F("Sensor "));
        Serial.print(i);
        Serial.println(F(" No Longer Tripped."));
      }
      lastState[i] = sensorState;
    }
  }
}

compiles but not tested

EDIT: added pin mode for inputs

When you find yourself tempted to suffix variable names with numbers, it is probably time to learn about arrays and how to use them.

Your code does not have any else statements to go with the if statements, so the lights turn on and off again very quickly. However, the two lights (and their sensors) should work the same.

Have you written any test programs to verify that your sensors work? Even swapping your sensors with each other could be helpful.

Have you written any test programs to verify that your lights work? even swapping your lights with each other could be helpful.

Have you verified your wiring? I cannot see it and no schematic or wiring diagram was provided.

Thank you for properly posting your code.

BulldogLowell:
...

Thank you for taking the time to do all that. But I can't get that code to work, and its a bit too advanced for me to really get it today at least. (With this code, one sensor worked and the other one didn't, just as my code)

vaj4088:
...

I have made sure the sensors and the leds work. And I built a wiring diagram: (Below)


I didn't mention that I also have a bluetooth module attached to it as well.

The reason why I don't have the else's to the if's , is that because without them, I can turn the lights on with my phone using the bluetooth module, whenever I add the else statements, the lights turn on and off right away.

This is all of my code:

char incomingByte; 
int sensor1 = 4;  // Pin of IR Motion Sensor
int light1 = 8;  // Pin of led1
int sensor2 = 5;
int light2 = 10;
 
void setup(){
  Serial.begin(9600);
  pinMode(light1, OUTPUT);  // Set Pin connected to ligh1 as an OUTPUT
  pinMode(light2, OUTPUT); 
  digitalWrite(light1, LOW);  // Set Pin to LOW to turn light OFF
  digitalWrite(light2, LOW);
}
 
void loop(){
  //this section is to allow to turn
  if (Serial.available() > 0) { 
incomingByte = Serial.read(); 

if(incomingByte == '3') {digitalWrite(light1, HIGH); }
if(incomingByte == '7') {digitalWrite(light1, LOW); }

if(incomingByte == '4') {digitalWrite(light2, HIGH); }
if(incomingByte == '8') {digitalWrite(light2, LOW); }
  
  }

    if (digitalRead(sensor2) == HIGH){   // If Motion detected 
       digitalWrite(light2, HIGH);  // Turn light ON
       Serial.println("Red Light (Light2)is ON");
       delay(500);
    
       digitalWrite(light2, LOW);// Turn light OFF
       //Serial.println("Light 2 is OFF");
       //delay(500);  
  }
  
  if (digitalRead(sensor1) == HIGH){   // If Motion detected 
       digitalWrite(light1, HIGH);  // Turn light ON
       Serial.println("Green Light (Light1)is ON");
       delay(500);
 
       digitalWrite(light1, LOW);// Turn light OFF
       //Serial.println("Light 1 is OFF");
       //delay(500);  
  }


  
}

This is the wiring diagram:
(I couldn't find the proper resistor values, nor the right sensor).I used 330ohms instead, and for the sensors assumed (red wire=vcc, black wire = gnd , green wire = out)

Right now only 1 light works. If i change the wires, then I can make the other light work, but both do not work at the same time.

I'd appreciate the help.

I am confused. The code refers to a green light and a red light, but the wiring diagram shows two red lights.

I am confused. The code turns an LED on by setting a pin HIGH, but the wiring diagram shows that an LED is turned on by setting a pin LOW.

When you say that only one light works at a time, are you referring to control by PIR or control by Bluetooth?

How do you know that "...the sensors and the leds work." ? How do you know that the pins of the Arduino work? I am also concerned about the assumptions being made about the sensors.

Some protoboards break up the power supply buses into sections. Have you verified that BOTH sensors and BOTH LEDs have +5 volts and ground available to them?

vaj4088:
I am confused. The code refers to a green light and a red light, but the wiring diagram shows two red lights.

Yes, that was my bad. I couldn't find different color for the leds quickly so I made them both red. Light 1 is technically a green led.

I am confused. The code turns an LED on by setting a pin HIGH, but the wiring diagram shows that an LED is turned on by setting a pin LOW.

Where do you see this at? I don't see it. The diagram is exactly how I wired it. Maybe this is the part I got wrong and I can't see it.

When you say that only one light works at a time, are you referring to control by PIR or control by Bluetooth?

I refer to the control by PIR. The bluetooth part works perfectly. So when One sensor detects movement, that light goes on as intended. But the other sensor won't work.

How do you know that "...the sensors and the leds work." ? How do you know that the pins of the Arduino work? I am also concerned about the assumptions being made about the sensors.

Since one sensor/light works, I have switched the sensors and the leds around. And whichever sensor/light that I put as 'light1' in my code, works. But if I take those same components and put them where 'light2' should be, they don't work. And from my phone over bluetooth, I can turn either/both leds on and off perfectly. (Therefore I know the components work, and the problem is either in the wiring, or in the code).

Some protoboards break up the power supply buses into sections. Have you verified that BOTH sensors and BOTH LEDs have +5 volts and ground available to them?

I just did, and yes. Both should be getting vcc and ground as expected.

I Want to rule some things out. First, the code. Does something looks wrong with my code? (It is my first time writing arduino, so it the thing I feel the least confident in).

char incomingByte; 
int sensor1 = 4;  // Pin of IR Motion Sensor
int light1 = 8;  // Pin of led1
int sensor2 = 5;
int light2 = 10;
 
void setup(){
  Serial.begin(9600);
  pinMode(light1, OUTPUT);  // Set Pin connected to ligh1 as an OUTPUT
  pinMode(light2, OUTPUT); 
  digitalWrite(light1, LOW);  // Set Pin to LOW to turn light OFF
  digitalWrite(light2, LOW);
}
 
void loop(){
  //this section is to allow to turn lights on an off over bluetooth
  if (Serial.available() > 0) { 
incomingByte = Serial.read(); 

if(incomingByte == '3') {digitalWrite(light1, HIGH); }
if(incomingByte == '7') {digitalWrite(light1, LOW); }

if(incomingByte == '4') {digitalWrite(light2, HIGH); }
if(incomingByte == '8') {digitalWrite(light2, LOW); }
  
  }

//one sensor/light (this is the one that doesnt work)
    if (digitalRead(sensor2) == HIGH){   // If Motion detected 
       digitalWrite(light2, HIGH);  // Turn light ON
       //Serial.println("Red Light (Light2)is ON");
      // delay(500);
    
       digitalWrite(light2, LOW);// Turn light OFF
       //Serial.println("Light 2 is OFF");
       //delay(500);  
  }

 //another sensor/light (this one works) 
  if (digitalRead(sensor1) == HIGH){   // If Motion detected 
       digitalWrite(light1, HIGH);  // Turn light ON
       //Serial.println("Green Light (Light1)is ON");
       //delay(500);
 
       digitalWrite(light1, LOW);// Turn light OFF
       //Serial.println("Light 1 is OFF");
       //delay(500);  
  }


  
}

I appreciate the time you are taking to help me out! I am sorry for all the trouble.

EDIT: Its working!!! Apparently nothing was wrong . . Just my sensors are really cheaply made. I have 5 sensors, and just 1 of them was working properly. The other 4 all had the vcc/jvcc loose. . Anyways! Thank you all!

The diagram shows one end of the LEDs connected to +5 volts via the resistors, so the Arduino would have to set the other end LOW to turn on an LED.

In any case, I am glad that you solved it. Good Luck!