OK sorry Marco. The code was a couple of posts back. I was not sure about re-posting stuff, but here it is:
/*
Fade up and down on multiple pins
Code written by Chagrin August 2012
*/
#define LED1 3 // assign the first LED to pin 3
#define LED2 5 // assign the second LED to pin 5, etc...
#define LED3 6
#define MAXPINNUMBER 6 // LED3 is the highest pin number -- number 6
#define MILLISPERDIMSTEP 10 // LED dimming is recalculated every X millis
// Group LED commands into a structure (struct)
struct command {
byte pin; // 0 to 255
unsigned int time; // 0 to 65535
byte brightness; // 0 to 255
char dimrate; // -127 to 128
};
// The stored state of each LED; holds state of dimming.
struct state {
unsigned int time;
byte brightness;
byte dimrate;
};
// Create the scene which is an array of "command" structs
struct command scene[] = {
{LED1,0,255,0},
{LED2,0,255,0},
{LED3,0,255,0},
{LED1,1000,0,0},
{LED2,2000,0,0},
{LED3,3000,0,10},
{LED3,6000,255,-10},
{LED3,9000,0,1},
{LED3,12000,255,-1}
};
// Initialize the LED states to off. Note 0 to MAXPINNUMBER is (MAXPINNUMBER + 1) states.
struct state ledState[MAXPINNUMBER + 1] = {{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}};
void setup(){
}
void loop(){
static unsigned long kickoff = 0;
static unsigned int sceneCtr = 0;
byte ledCtr = 0;
// wait for a button press here to kick off the scene if desired
// Check the scene to see if we have a new command
if (sceneCtr < sizeof(scene) / sizeof(command)) {
if (scene[sceneCtr].time <= millis() - kickoff) {
analogWrite(scene[sceneCtr].pin, scene[sceneCtr].brightness);
if (scene[sceneCtr].dimrate != 0) {
// It dims; we have to add this to our ledState array and keep track of it.
ledState[scene[sceneCtr].pin].time = scene[sceneCtr].time + MILLISPERDIMSTEP;
ledState[scene[sceneCtr].pin].brightness = scene[sceneCtr].brightness;
ledState[scene[sceneCtr].pin].dimrate = scene[sceneCtr].dimrate;
}
sceneCtr += 1;
}
}
// Here we process through all the LEDs in a dimming state
for (ledCtr = 0; ledCtr < MAXPINNUMBER + 1; ledCtr++) {
if (ledState[ledCtr].dimrate != 0) { // only LEDs with a dimming are messed with.
if (ledState[ledCtr].time <= millis() - kickoff) {
if (ledState[ledCtr].brightness + (char) ledState[ledCtr].dimrate >= 0 &&
ledState[ledCtr].brightness + (char) ledState[ledCtr].dimrate <= 255) {
ledState[ledCtr].brightness += (char) ledState[ledCtr].dimrate;
ledState[ledCtr].time += MILLISPERDIMSTEP; // mark the time of next LED adjustment
} else if (ledState[ledCtr].brightness + (char) ledState[ledCtr].dimrate < 0) {
// need to stop at zero and not a negative number.
ledState[ledCtr].brightness = 0;
ledState[ledCtr].dimrate = 0; // zero dimrate so we stop dimming it
} else {
// need to stop at 255.
ledState[ledCtr].brightness = 255;
ledState[ledCtr].dimrate = 0; // zero dimrate.
}
analogWrite(ledCtr, ledState[ledCtr].brightness);
}
}
}
// Have 60 seconds elapsed? We need to restart if so.
if (millis() - kickoff > 60000) {
kickoff = millis();
sceneCtr = 0;
}
}