I just started studying Arduino. I'm trying to create a led-loop that fades simultaneously back and forth (1 led at a time). However, after the first revolution, I get the following bug.
Here is my code:
int ledPin[] = {3,5,6,9,10,11}; //Analog Pin
int pinCount = 6;
int led = 0;
void setup() {
//set pin sequence status
for(int led = 0; led < pinCount; led++) {
pinMode(ledPin[led], OUTPUT);
}
}
void loop() {
//Led fade in and out simultaneously
led = 0;
for(led = 0; led < pinCount; led++) {
fade_in(led,5);
fade_out(led,5);
}
//Reverse sequence
led = pinCount - 2;
for(led = pinCount - 2 ; led < pinCount; led--) {
fade_in(led,5);
fade_out(led,5);
}
}
//USER DEFINED FUNCTION
//telling which led to fade and at amount with acceleration
void fade_in(int led,int amount) {
int ledPin[] = {3,5,6,9,10,11};
int timer = 255/amount + 1;
for(int brightness = 0; brightness <= 255; brightness = brightness + amount) {
analogWrite(ledPin[led], brightness);
delay(timer);
timer = timer - 1;}
timer = 255/amount + 1;
}
//reverse sequence
void fade_out(int led, int amount) {
int ledPin[] = {3,5,6,9,10,11};
int timer = 255/amount + 1;
for (int brightness = 255; brightness >= 0; brightness = brightness - amount)
analogWrite(ledPin[led], brightness);
delay(timer);
timer = timer - 1;
timer = timer = 255/amount + 1;
}
I would gladly appreciate any helps (criticism is welcome, no harsh feelings).
P/s: sorry for my sloppiness for this is my first time using this forum as well as Arduino platform.
I'm not quite sure if I can follow what the problem is. But I have some questions/comments
You have an array of led pins at the top of your code; that array is known everywhere in the sketch (you can do a google for C++ scope), so why do you define arrays with the same names again in your fade_in and fade_out functions?
In loop(), you first set led to a value; that is not needed because you do the same thing in the for-loop() that follows.
Your problem (whatever it is) might be the reverse sequence for-loop. Fill in the numbers
you start with led == 4; that's smaller than pinCount so you will do a fade_in and a fade_out
next led becomes 3, so the next fade_in / fade_out
eventually led becomes negative (which is still smaller than pinCount)
led will become positive again after it becomes -32678 and that is where the for-loop ends
You can not have negative indices for your arrays; the pin number that will be read from the array will be some random number and next you try to control e.g. a led on pin 12345.
I can make some suggestions about the code. As for feels, leave those somewhere safe. They won't help you with tech and for most people they only serve to cloud the obvious and screw you up so hard it's down.
// const variables never change
const int ledPin[] = {3,5,6,9,10,11}; //Analog Pin
const int pinCount = 6;
int led = 0;
void setup() {
//set pin sequence status
for(int led = 0; led < pinCount; led++) {
pinMode(ledPin[led], OUTPUT);
}
}
void loop() {
//Led fade in and out simultaneously
led = 0;
for(led = 0; led < pinCount; led++) {
fade_in(led,5);
fade_out(led,5);
}
//Reverse sequence
led = pinCount - 2;
for(led = pinCount - 2 ; led < pinCount; led--) {
fade_in(led,5);
fade_out(led,5);
}
}
//USER DEFINED FUNCTION
//telling which led to fade and at amount with acceleration
void fade_in(int led,int amount)
{ // I moved the braces to make it easier for me to see the pairings
// int ledPin[] = {3,5,6,9,10,11}; <<=== you have globals with the same name -- this is a bad idea
// the globals will work with the code you have
int timer = 255/amount + 1; // you might want to make this + 50 or + 100 instead of + 1
for(int brightness = 0; brightness <= 255; brightness = brightness + amount)
{
analogWrite(ledPin[led], brightness);
delay(timer);
timer = timer - 1;
}
// timer = 255/amount + 1; <<==== why bother, timer goes when the function ends, it's a local variable
}
//reverse sequence
void fade_out(int led, int amount)
{
// int ledPin[] = {3,5,6,9,10,11}; <<=== you have globals with the same name -- this is a bad idea, use the globals
int timer = 255/amount + 1;
for (int brightness = 255; brightness >= 0; brightness = brightness - amount)
{ // you forgot this brace
analogWrite(ledPin[led], brightness);
delay(timer);
timer = timer - 1;
} // you forgot this brace
// timer = timer = 255/amount + 1;
}