Greetings all,
I'm attempting to light 6 Led's one at a time, one after another by fading them up, then back down, and on to the next LED. Using the code below, I only get the first LED to come on Fade ON, then turn completely off (NO FADE out), then repeat. Could someone please look at my code and tell me what I'm doing wrong? Be easy, I'm new to the Arduino world!!
int ledPins[] = {3,5,6,9,10,11};
int brightness = 0;
int fadeAmount = 5;
void setup()
{
int index;
for(index = 0; index <=5; index++)
{
pinMode(ledPins[index], OUTPUT);
}
}
void loop()
{
fadeoneAfteranotherLoop();
}
void fadeoneAfteranotherLoop()
{
int index;
int delayTime = 20;
for(index = 0; index <=5; index++);
{
analogWrite(ledPins[index], brightness);
When behavior like this exhibits itself, the usual question is does changing the order of the pins in the array change which one works correctly?
If it's always the same pin that works, the others are wired wrong. If it's always the same position that works, the code is wrong.
One } per line. NEVER more than one.
Use Tools + Auto Format before you post any more improperly indented code. And, use the # icon above the row of smiley faces to generate code tags that you post code between.
I'm pretty sure the code is wrong, because changing the order of the pins does not affect the behavior. The same pin still fades on, turns off (no fade) , and then fades on again. If I change the [index] in line 24 to an actual number, [0] then the LED fades on, and then fades off correctly, but doesn't advance to the next LED.
int ledPins[] = {
11,10,9,6,5,3};
int brightness = 0;
int fadeAmount = 5;
void setup()
{
int index;
for(index = 0; index <=5; index++)
{
pinMode(ledPins[index], OUTPUT);
}
}
// the loop routine runs over and over again forever:
void loop()
{
fadeoneAfteranotherLoop();
}
void fadeoneAfteranotherLoop()
{
int index;
int delayTime = 20;
for(index = 0; index <=5; index++);
{
analogWrite(ledPins[index], brightness);
brightness = brightness + fadeAmount;
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
delay(delayTime);
}
}
I figured out the error of my ways, but haven't come up with a fix yet.
The problem is that the fade function is constantly changing the brightness from 0 -> 255, then reversing it from 255 -> 0, then repeating so it never advances. Is there a way I could make it count cycles from 0 ->255 -> 0, and then advance to the next LED?
The problem is that the fade function is constantly changing the brightness from 0 -> 255, then reversing it from 255 -> 0, then repeating so it never advances
I'm not following that. brightness does indeed increase and decrease, but only as the for loop iterates. As the for loop iterates, it should be applying brightness to each pin in turn. The pins will increase in brightness in steps of 25, with no two at the same level at any time, which may not have been what you wanted, but, still, all 6 LEDs should be fading up and down.