Simultaneous blinking LEDS challenge

I am trying my hand at my very first Arduino program. It is meant to be a firefly simulator but I am having the following problem:

I want to have 4 sets of 2 lights each on their own blink loop. However I want them to blink concurrently to each other instead of each set of 2 having to wait until the previous 2 complete their loop.

Do I need multi-threading for this? Can I do this with Arduino? Any other suggestions?

Code below (I apologize in advance for messy formatting):

int ledPin[] = {8, 9, 10, 11};         // pwm pins only
int value = 0;                             // variable to keep the actual value 
int fadeStart;                             // random number for start of fade sequence
int inSpeed;                              // how fast the light fades in
int outSpeed;                           // how fast the light fades out
int inDelay;                             // how long before next flash starts
int outDelay;                          // how long before fade
//int pin;                               // randomizes which LED fires

void setup() {  
  // nothing to set up
}

void loop()
{  
for (int i=0; i<=3; i++)
  {
   outDelay  = random(5,30);
   inDelay   = random(1000,5500);
   fadeStart = random(25,50);
   inSpeed   = random(25,50);    
   for(value = 0 ; value <= 255; value+=inSpeed) // fade in (from min to max) 
     { 
      analogWrite(ledPin[i], value);           // sets the value (range from 0 to 255) 
      delay(fadeStart);                            // waits for X milli seconds to see the dimming effect 
     } 
   outSpeed   = random(25, 150);
   for(value = 255; value >=0; value-=5)   // fade out (from max to min) 
     { 
      analogWrite(ledPin[i], value); 
      delay(outDelay); 
     }  
delay(100);    // the delay between changes
  }

}

This post I made a while back addresses performing two timing-based actions simultaneously. It was in response to a servo question, but would also apply to blinking two LEDs asynchronously as well:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1213200565/1#1

  • Ben

Thanks Ben! Great suggestion.

I have applied the function to most of the sketch however I'm still having trouble getting this to work. I couldn't get your suggestion of using the timer method (millis()) to work right so I attempted to use MsTimer2. However, The LED does not fade in but instead just lights at full brightness... here's a stripped down version of the code:

#include <MsTimer2.h>

int ledPin[] = {8, 9, 10, 11};            // pwm pins only
int fadeInto = 1;

void setup() {  
  // nothing to set up
}

void fadeIn() {
  analogWrite(ledPin[1], fadeInto);           // sets the value (range from 0 to 255)
}

void loop() {   
  
   while(fadeInto < 255){
      MsTimer2::set(500, fadeIn); // 500ms period
      MsTimer2::start();
      fadeInto++;
   }
}

Apparently I'm not getting the use of this function correct or some other problem with my logic? Any suggestions or even a swatch of pseudocode would be much appreciated.

Thanks.
Craig