Hi all...this is my first attempt at writing my own sketch (although I have borrowed some). The idea is to have a motion sensor detect motion, and turn light on full for 3 minutes. Then I would like the light to slowly fade off. My sketch never seems to get to the fade stage. It detects motion, turns the light on for 5 seconds ( I have it set for 5 seconds for trial purposes so I don't have to sit and wait for 3 minutes each time!) then turns the light off. It never gets to the fade part of the sketch, and I am completely at my noob wits end as to why it doesn't fade. Thanks for any input!!
/*
This is a sketch that I would like to have a motion sensor detect motion, turn on
lights under bed to full on for 3 minutes, then fade off.
*/
int val = LOW; //set the placeholder for the sensor to low.
int led = 6; //assign led to pin 6.
int pir = 8; //assign PIR sensor to pin 8.
int brightness = 255; //set brightness variable to full.
int fadeAmount = 3; //establish the amount the led will fade each time.
void setup() {
pinMode(pir, INPUT); //establish pir sensor as input.
pinMode(led, OUTPUT); //establish led as output.
Serial.begin(9600);
}
void loop() {
val = digitalRead(pir); //read the pir sensor
if (val == HIGH) {
analogWrite(led, brightness); //if motion is detected, turn led on full for an established amount of time.
delay(500); //set to 5 seconds for trial...I would like it to turn on full for 3 minutes.
if (brightness > 0) {
brightness = brightness - fadeAmount; //after the led is on full I would like it to fade off
analogWrite(led, brightness);
delay(30);
}
digitalWrite(led, LOW); //once the led is off, reset the variables to begin sequence again.
brightness = 255;
val = LOW;
Right...1/2 second...forgot a zero. I will look at the blink without delay sketch. I'd still like to know why this sketch doesn't fade the led. I've gone over the logic and can't figure it out and would love to lnow for future reference!!
ok...I see what you're saying. I thought with the if statement "if (brightness>0)" that it would continue to loop around until the if statement was no longer true. So it would continue to fade (subtracting the fadeAmount each time) until brightness was <=0. Then it would to the next part of the code, which was turning the led off, resetting the values of the variables to the original values with which to begin the loop cycle again. Have I misunderstood how the if statement works? How can I adjust my code to get it to do what I want it do?
Again, my goal is to have the led turn on full for a set amount of time when motion is detected, then fade to off.
Thanks!!
So I've got the "while" command working, it has created another problem. Now the led just continues to flash on, pause, then fade. It doesn't seem to be waiting for a "HIGH" signal from the PIR sensor. It just continues in the loop. I've tried changing my sketch, but nothing seems to help. I've gone over the code with my limited knowledge and can't seem to find how/where it's getting stuck in the loop. Here's the code:
/*
This is a sketch that I would like to have a motion sensor detect motion, turn on
lights under bed to full on for 3 minutes, then fade off.
*/
int val = LOW; //set the placeholder for the sensor to low.
int led = 6; //assign led to pin 6.
int pir = 8; //assign PIR sensor to pin 8.
int brightness = 0; //set brightness variable.
int fadeAmount = 3; //establish the amount the led will fade each time.
void setup() {
pinMode(pir, INPUT); //establish pir sensor as input.
pinMode(led, OUTPUT); //establish led as output.
Serial.begin(9600);
}
void loop() {
val = digitalRead(pir); //read the pir sensor
if (val == HIGH) {
brightness=255;
analogWrite(led, brightness); //if motion is detected, turn led on full for an established amount of time.
delay(5000); //set to 5 seconds for trial...I would like it to turn on full for 3 minutes.
while(brightness > 0) {
brightness = brightness - fadeAmount; //after the led is on full I would like it to fade off
analogWrite(led, brightness);
delay(30);
}
digitalWrite(led, LOW); //once the led is off, reset the variables to begin sequence again.
val = LOW;
}
delay(100);
}
Thanks for any help!!! You have no idea how much I appreciate your analysis and assistance!!