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!