Ok, I've tried solving this on my own using the Getting Started with Arduino book, and google (as well as searching this forum), but I've only succeeded in confusing myself even more.
I'm sure this is a pretty easy solve, I'm just a total beginner, and the programming course I took 10 years ago in high school isn't fresh in my memory.
I've got 6 LEDs plugged into my arduino Uno (pins 3, 5, 6, 9, 10, and 11). My final goal is to have them at (pseudo) random have them fade on and back off independently. Each LED will have a random wait time before coming back on (between 1 and 5 minutes), and the fade in time should take about 30 seconds to a minute. This is eventually going in our garden to add a bit of interesting lighting for the plants. I also plan on adding further functionality down the line, but I'm taking baby steps.
I started out with the premade example for analog fading:
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
void setup() {
// declare pin 9 to be an output:
pinMode(9, OUTPUT);
}
void loop() {
// set the brightness of pin 9:
analogWrite(9, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
I then figured out how to make them all fade at the same time:
int value1 = 0; // variable to keep the actual value
int value2 = 0; // I know these aren't used later in the code
int value3 = 0; // I assumed they would be necessary eventually
int value4 = 0;
int value5 = 0;
int value6 = 0;
int ledpin1 = 3;
int ledpin2 = 5;
int ledpin3 = 6;
int ledpin4 = 9;
int ledpin5 = 10;
int ledpin6 = 11;
void setup() {
// nothing happens in setup
}
void loop() {
// FADE IN
for(value1 = 0 ; value1 <= 255; value1+=5)
{
analogWrite(ledpin1, value1); // sets the value (range from 0 to 255)
analogWrite(ledpin2, value2); // sets the value (range from 0 to 255)
analogWrite(ledpin3, value3); // sets the value (range from 0 to 255)
analogWrite(ledpin4, value4); // sets the value (range from 0 to 255)
analogWrite(ledpin5, value5); // sets the value (range from 0 to 255)
analogWrite(ledpin6, value6); // sets the value (range from 0 to 255)
delay(300); // waits for 30 milli seconds to see the dimming effect
}
// FADE OUT
for(value = 255; value >=0; value-=5) // fade out (from max to min)
{
analogWrite(ledpin1, value1);
analogWrite(ledpin2, value2);
analogWrite(ledpin3, value3);
analogWrite(ledpin4, value4);
analogWrite(ledpin5, value5);
analogWrite(ledpin6, value6);
delay(30);
}
}
I assume I now need to assign a timer for each LED, and before each tick up or down in value, check if the timer has reached 0. I don't know how to do that. I tried a different for statement for each LED, but that made them come on one at a time. Is this a situation where I need to set up an array to monitor all 6 at once? HELP!