Hi all,
First of all, I'd just like to say hi as I am a new member here. I am fairly new to arduino, and only have a limited experience with coding in general so I thought this forum would be a great place to come for help with my projects.
This first project I'm working on is fairly simple. Basically, I just have a motion detector that, once it detects motion, will fade on LED lights over a period of maybe 2 seconds. As long as motion is detected, the lights will stay on. When motion is no longer detected, there is a 15 second delay before the lights fade off over a period of maybe 3 seconds.
I started off with a basic code to just turn the lights on and off based on the above description and it has worked flawlessly so far.
int LEDPin = 11; //LED output
int pirPin = 2; //Motion detection input
int motionDetect = 0; //Motion detection variableint count = 0; //Counter for turning off lights
void setup(){
pinMode(LEDPin, OUTPUT); // LED lighting output assign
pinMode(pirPin, INPUT); // Motion detection input assign
}//--------------------------------------------------------------------------------------------------------------------------------
//A motion sensor will detect movement, which will fade on the LED lights, and keep them lit as long as motion is detected.
//When motion is no longer detected, a counter will begin. Once the counter has reached 15 with no motion detected, the lights will fade off.//----------------------------------------------------------------------------------------------------------------------------------
void loop()
{
motionDetect = digitalRead(pirPin);
if (motionDetect == HIGH){
analogWrite(LEDPin, 255);
count = 0;
}
else {
delay(1000);
count = count+1;
}
while(count > 15){
analogWrite(LEDPin, 0);
count = 0;
}
}
The trouble I'm having is when I try to incorporate a fading feature to the LED lights. I had tried some slightly more complicated equations that I found from various tutorials on how to make a "breathing" LED light, but I just couldn't get it to work properly so I tried something more simple to see if I could that working first, and then maybe build back to up more complicated equations. So I just added a simple loop, where every pass through the loop will increase the brightness of the LED light, but for whatever reason, even this won't work. I've tried numerous variations of this type of loop, but I just cant see to get it to work properly.
int LEDPin = 11; //LED output
int pirPin = 2; //Motion detection input
int motionDetect = 0; //Motion detection variableint count = 0; //Counter for turning off lights
int brightness = 0; //Brightness variablevoid setup(){
pinMode(LEDPin, OUTPUT); // LED lighting output assign
pinMode(pirPin, INPUT); // Motion detection input assign
}//--------------------------------------------------------------------------------------------------------------------------------
//A motion sensor will detect movement, which will fade on the LED lights, and keep them lit as long as motion is detected.
//When motion is no longer detected, a counter will begin. Once the counter has reached 15 with no motion detected, the lights will fade off.//----------------------------------------------------------------------------------------------------------------------------------
void loop()
{
motionDetect = digitalRead(pirPin);
if (motionDetect == HIGH && brightness < 255){
analogWrite(LEDPin, brightness);
brightness = brightness + 1;
delay(100);
}if (motionDetect == HIGH){
analogWrite(LEDPin, 255);
count = 0;
}
else {
delay(1000);
count = count+1;
}
while(count > 15){
analogWrite(LEDPin, 0);
count = 0;
}
}
In this particular version, for instance, when motion is detected, the lights will fade in for just a couple steps and then suddenly jump to full max brightness. This is the closest I've gotten to my goal, but it's still not where I want it to be.
Does anybody have any idea what I'm doing wrong in my sketch? I'm open to other ideas as well, if somebody knows of a better way to create a smooth transition from lights off to lights at max brightness over a 2 -3 second period, and vice versa.
Thanks in advance for any help you guys can provide.