Hey Guys,
I'm trying to get 4 sets of LEDs to be controlled by PIR motion sensors and LDRs. I would like the motion sensors to activate the LEDs for 'x' period of time and during the time the LEDs are on, the LDRs would control the brightness of the LEDs. I've got the parts to work separately (i think?) but when I combine it is where I'm having problems. The 1 set of LED I've got hooked up to the LDR stays on constantly. I've also tired to add a delay to get the LEDs to stay on for 3 second when they are activated by the motion sensor but that isn't doing what I had hoped. What am I doing wrong here? Any help would be appreciated, thanks in advance!
/* PIR control LEDS but LDR overright PIR control
-this is not what I want. would like LEDs to dim or brighten
while activated by PIR. LEDs would be off if PIR are not
activated
*/
#define LDR 2
long val;
int roomlight;
int ledPin1 = 9; // Sets LED variable
int ledPin2 = 12;
int ledPin3 = 10;
int ledPin4 = 11;
int switchPin1 = 6; // Sets Motion Sensor variable
int switchPin2 = 5;
int value = 0; // Motion Sensor data variable
void setup() {
Serial.begin(9600);
pinMode (ledPin3, OUTPUT);
}
void loop() {
/* {int value = analogRead(0);
if (value > 50) value = 225; //set 100 as on
if (value < 50) value = 10; //set 200 or above as low brightness
//analogWrite(13, value);*/
//val = 1023 - analogRead(0);
roomlight = analogRead(2);
Serial.print("roomlight=");
Serial.println(roomlight);
//Serial.println("||");
//if (val > 50) val = val+225; //set 100 as on
//if (val < 50) val = val+0; //set 200 or above as low brightness
//val = ((roomlight * 255) / 900);
//val = (val + 100); //Error Correction
if (roomlight > 1000) val = 255;
else if (roomlight > 990) val = 200;
else if (roomlight > 980) val = 150;
else if (roomlight > 970) val = 100;
else if (roomlight > 965) val = 50;
else if (roomlight > 965) val = 40;
else if (roomlight > 960) val = 30;
else if (roomlight > 950) val = 20;
else if (roomlight > 850) val = 10;
else if (roomlight > 300) val = 5;
else val = 0;
Serial.print("================");
Serial.println(val);
//Serial.print("Before map: {");
//Serial.print(val);
//Serial.println("}");
analogWrite(ledPin3, val);
delay(500);
{
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(switchPin1, INPUT);
pinMode(switchPin2, INPUT);
}
{
value = digitalRead(switchPin1);// Get data from Moton Sensor
if (HIGH == value) {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
delay(3000)
} else {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
}
{
value = digitalRead(switchPin2);// Get data from Moton Sensor
if (HIGH == value) {
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, HIGH);
delay(3000)
} else {
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
}
}
}
}