Multi Channel LED fading with.. millis?

Hi all,

I am trying to do the following: use one PWM output to 'flicker' red LEDs at random

WHILST

use another PWM output to fade in/out some blue LEDs.

I would like this to happen simultaneously - but am struggling to make the two things happen at the same time.

my code so far:

/* plus one palmela install code test

june 2010

*/


//  LED PWM output pins
#define RedPin 11
// #define GrnPin 10
#define BluPin 9
// #define WhtPin 3


int Red;  // The state of the Red LED
//int Grn;  // The state of the Green LED
int Blu;  // The state of the Blue LED
//int Wht;  // The state of the White LED

// For blue LED fading

int BluFadVal = 15;

// For Windspeed vortex reads
/*
#define uint unsigned int
#define ulong unsigned long

#define PIN_ANEMOMETER 2 // Digital pin 2 input

#define MSECS_CALC_WIND_SPEED 500 // How often wind speed is calculated

volatile int numRevsAnemometer = 0; // incremented in the interrupt
ulong nextCalcSpeed; // When next calculate wind speed
ulong time; // Millis() at each start of loop().

*/

//interval defs

long interval = 50;

long BluInterval = 200;

long last_time = 0;


void setup(){
 
  // Windspeed vortex input defs
  
//  pinMode(PIN_ANEMOMETER, INPUT);
//  digitalWrite(PIN_ANEMOMETER, HIGH);

//attachInterrupt(0, countAnemometer, FALLING);
// ********* attachInterrupt(0, countAnemometer, RISING);


// ************ nextCalcSpeed = millis() + MSECS_CALC_WIND_SPEED;


// set LED control pins as outputs
 
  pinMode(RedPin, OUTPUT);
  // pinMode(GrnPin, OUTPUT);
  pinMode(BluPin, OUTPUT);
 //  pinMode(WhtPin, OUTPUT);
  
  
  


  
}



//  set the LED brightness values when called below
void WriteLEDArray() {
  analogWrite(RedPin, Red);
 // analogWrite(GrnPin, Grn);
  analogWrite(BluPin, Blu);
 // analogWrite(WhtPin, Wht);
}



void loop(){
  
   // red LED flicker
  if ( millis() >=(last_time+interval) ) {
  
 Red = (random(120)+135);
 //Blu = BluFadVal;
 
 //if i comment this out both codes run but red flickers few times, then blue fades in/out, then red flickers again. red flicker waiting for blue cycle to complete 
// blue fades in/out but after each time turns off and on. would like it to stay lit - why i put limit of 15 on it
 // last_time = millis();
  }
  
  // Blue LED fade
  if ( millis() >=(last_time+BluInterval) ) {
  
   for (BluFadVal = 15; BluFadVal <= 255; BluFadVal +=1)
 // BluFadVal +=1;
  {
  analogWrite(BluPin, BluFadVal);
   delay(20);
  }
 
 
 
   for (BluFadVal = 255; BluFadVal >=15; BluFadVal -=1)
 // BluFadVal -=1;
  {
  analogWrite(BluPin, BluFadVal);
  delay(20);
  }
  
 // Red = (random(120)+135);
  
  last_time = millis();
  }
  
  
  
  
  //experiment
//  else {
 //   Red = (random(120)+135);
 // }  
  // function to set LED values
  
  WriteLEDArray();
  
}

At the moment, the red LEDs flicker, THEN the blue LEDs fade in/out, THEN the red LEDs flicker, etc.

Any pointers in the right direction would be lovely. I'm trying to modify the 'Blue LED Fade' part of the code so that it does one step, then loops back to the 'red LED flicker' section.

And for the commented out windspeed vortex code - i'd like to eventually get readings from it to change the brightness of white LEDs too, but trying to crack this red/blue simultaneous problem first.

It's all for an installation - i want red 'fire-y' flickering, blue 'wave-y' fading and white light changing according to wind speed. At the same time from one arduino if possible as I have a shield with buckpucks to power the high power LEDs. I thought using the millis() with different values for each channel would work.

/rant

have scoured forums and head scratched for a while so thought i'd post it up. Ta for your time - like I said any pointers or links would really help. I'm learning.

s

The whole reason for using millis() to determine when it is time to do something is so that you get rid of calls to delay.

Your code to fade the blue LED still uses delay().

Get rid of the for loop with delay for fading the blue LED.

On each pass through loop, determine if it is time to change the status of the red LED. If so, determine the new value and set it.

Then, determine if it is time to change the status of the blue LED. If it is, determine the new value, and set it.

How you determine the new value for the red LED and for the blue LED will be different, but that is all that is different.

If you want the red LED and blue LED intervals to be different, you need a last_red_time and last_blue_time, not just one last_time.