Sticky - Blink Two LEDs, independent, no delay

Msquare:
And so on .... I have spent a few hours on this now, and it is a fascinating challenge, but there is family, a hobby, work and sleep... so I just leave it here for the while being. I'll be back.

It is a nice challenge isn't it? It reminds me of the time developers spend getting 'starting zones' working right for MMO games. Get the basics right and new people will stay with you.

GreenLED  ... QuickInterval ... QuickTimer ... GreenLEDState
RedLED ... SlowInterval ... RedLEDState ... SlowTimer

As a final suggestion, I think the code as it currently stands is hard to amend for, say, a third timer. Also there is a bit of an implication that you need to use green and red LEDs, and that one interval has to be faster than the other.

I suggest renaming (as below) LED1 and LED2. That way, you can add a third LED by copy/pasting, and just searching for LED1 and making it LED3, as appropriate. Having a consistent string to search for will avoid minor errors like changing Green to Blue but not Slow to Slower or whatever.

  if ( millis() - QuickTimer > QuickInterval ) {

I think I prefer the brackets I had. Leaving them out, whilst correct, just makes you pause and wonder about operator precedence. At least I do, when people complain that their timing examples "don't work".

So:

  if ( (millis() - QuickTimer) > QuickInterval ) {
// Variable holding the timer value so far. One for each "Timer"

unsigned long QuickTimer , SlowTimer ;

I think I prefer to train people to initialize variables (and yes, I know it isn't necessary right here).

// Variable holding the timer value so far. One for each "Timer"
unsigned long QuickTimer = 0, 
              SlowTimer = 0 ;

Or perhaps more generally:

void setup() {
  pinMode(LED1,OUTPUT) ;
  pinMode(LED2,OUTPUT) ;
  QuickTimer = SlowTimer = millis ();
}

Then this code could be put elsewhere. In other words, assuming your timers start at zero basically assumes your blinking sketch does nothing else (eg. open files, web pages).

And for example, if the timers were Unix time (which they aren't) then starting them at zero would be much different from starting them at "the current time".

Amended version:

/* "Multiple independent delay" - Two blinking LEDs

Working code example that blinks two LEDs, each with its own independent period.
This extends the BlinkWithoutDelay example of a single LED, showing how you
can implement multiple independent timing paths in the main loop.

Written by several forum members. In public domain.  Oct 2011.
*/

// Which pins are connected to which LED
const byte LED1 = 8 ;
const byte LED2 = 10 ;

// Time periods of blinks in milliseconds (1000 to a second).
// Time variable and constants should be unsigned long, always
const unsigned long LED1interval = 555UL ;
const unsigned long LED2interval = 1234UL ;

// Variable holding the timer value so far. One for each "Timer"
unsigned long LED1timer = 0, 
              LED2timer = 0 ;

// Variable to know what to change the LED to.
int LED1State = LOW ;
int LED2State = LOW ;

void setup() {
  pinMode(LED1,OUTPUT) ;
  pinMode(LED2,OUTPUT) ;
  LED1timer = LED2timer = millis();
}

void loop() {

// Handling the blink of one LED.
// First, check if it is time to change state
  if ( (millis() - LED1timer) > LED1interval ) {
    // It is time to change state. Calculate next state.
    if (LED1State==LOW)
      LED1State = HIGH;
    else
      LED1State = LOW;
    // Write new state
    digitalWrite(LED1, LED1State ) ;
    // Adjust Timer to fire again "LED1interval" later
    LED1timer += LED1interval ;
  }

// The other LED is controlled the same way. Repeat for more LEDs
  if ( (millis() - LED2timer) > LED2interval ) {
    if (LED2State==LOW)
      LED2State = HIGH;
    else
      LED2State = LOW;
    digitalWrite(LED2, LED2State ) ;
    LED2timer += LED2interval ;
  }

/* Other code that needs to execute goes here.
   It will be called many thousand times per second because the above code
   does not wait for the LED blink interval to finish. */

}