I'm a beginner with Arduino and programming (have had it 3 days now) and was trying to write a program to fade 5 LEDs in and then out in a sequential manner. I'm running the 0021 version of the software. I've read a lot of the examples and reference area to help me learn, but I keep getting an error when I compile the code that I just can't seem to figure out. Can anyone point me to my mistake? I appreciate your help.
The code is:
const int del = 30;
const int ledArray[] = {5, 6, 9, 10, 11};
void setup()
{
for (int i=5; i<=11; i++)
{
pinMode(i, OUTPUT);
}
}
void loop()
{
for (int i=1; i<=5; i++)
{
for (int p=0; p<=255, p++)
{
analogWrite(ledArray[i], p);
delay(del);
}
for (int r=255; r>=0; r--)
{
analogWrite(ledArray[i], r);
delay(del);
}
}
}
and the error I get while compiling is:
Consecutive_Fading_LEDS2.cpp: In function 'void loop()':
Consecutive_Fading_LEDS2:15: error: expected `;' before ')' token
Wow. That was so simple I feel foolish. Based on the error message I thought it was saying I needed to have a semicolon directly before the parenthesis. That's why it didn't make any sense to me. Thanks so much! :
Okay, I still have a problem. I corrected the code and it works in fading the leds one by one but it always skips the first led. Everything's wired up fine and I even swapped leds, but no dice. And then, after about 20 seconds, the last led stops working as well. After about 45 seconds it becomes erratic, and the first led will light up, or the second one will fade in and out twice. If I hardware reset the Uno it goes back to normal (but still skipping the first led) and lights up the last led as part of the sequence. What's going on here?! Any ideas?
The array that you have defined, ledArray, requires an index to access elements in the array. Valid index values are 0 to 4, not 1 to 5. It is skipping the 0th element in the array, not the 1st element, because you are not referencing the 0th element. You are however referencing the 5th, non-existent, element in the array.
Ahhhhhh, I see now. Thanks so much. You guys absolutely rock! Would you mind explaining to me why it had erratic behaviour when skipping 0 and referencing nonexistent 5? Wouldn't it just skip the first value in the array every time? Or does referencing the nonexistent 5 cause it to read from a part of memory not within the range used by the program? I guess what I'm asking is if it's doing the same thing every time, why did it become erratic and starting having random behaviour? Thanks so much for your time and patience with neophytes like me!
What value was stored in the first position after the array? What affect does sending that value to digitalWrite have? Without having a memory map, it would be difficult to determine the answer to the first question. Without digging into the source code for digitalWrite, and an answer to the first question, it would be difficult to answer the second question.
But, like the doctor says when the patient says "it hurts if I do this", well don't do that.
Define the code correctly, and it won't be necessary to find explanations for abnormal behavior.