Now for two at once

Now it is time to do some multi-tasking!  First wire up another LED as in the diagram below.

Then we'll create another state machine for a second LED that flashes at completely different rates.  Using two separate state machines allows us to blink the two LEDs completely independent of one another.  Something that would be surprisingly complicated to do using delays alone.

// These variables store the flash pattern
// and the current state of the LED

int ledPin1 =  12;      // the number of the LED pin
int ledState1 = LOW;             // ledState used to set the LED
unsigned long previousMillis1 = 0;        // will store last time LED was updated
long OnTime1 = 250;           // milliseconds of on-time
long OffTime1 = 750;          // milliseconds of off-time

int ledPin2 =  13;      // the number of the LED pin
int ledState2 = LOW;             // ledState used to set the LED
unsigned long previousMillis2 = 0;        // will store last time LED was updated
long OnTime2 = 330;           // milliseconds of on-time
long OffTime2 = 400;          // milliseconds of off-time

void setup() 
{
  // set the digital pin as output:
  pinMode(ledPin1, OUTPUT);      
  pinMode(ledPin2, OUTPUT);      
}

void loop()
{
  // check to see if it's time to change the state of the LED
  unsigned long currentMillis = millis();
 
  if((ledState1 == HIGH) && (currentMillis - previousMillis1 >= OnTime1))
  {
    ledState1 = LOW;  // Turn it off
    previousMillis1 = currentMillis;  // Remember the time
    digitalWrite(ledPin1, ledState1);  // Update the actual LED
  }
  else if ((ledState1 == LOW) && (currentMillis - previousMillis1 >= OffTime1))
  {
    ledState1 = HIGH;  // turn it on
    previousMillis1 = currentMillis;   // Remember the time
    digitalWrite(ledPin1, ledState1);	  // Update the actual LED
  }
  
  if((ledState2 == HIGH) && (currentMillis - previousMillis2 >= OnTime2))
  {
    ledState2 = LOW;  // Turn it off
    previousMillis2 = currentMillis;  // Remember the time
    digitalWrite(ledPin2, ledState2);  // Update the actual LED
  }
  else if ((ledState2 == LOW) && (currentMillis - previousMillis2 >= OffTime2))
  {
    ledState2 = HIGH;  // turn it on
    previousMillis2 = currentMillis;   // Remember the time
    digitalWrite(ledPin2, ledState2);	  // Update the actual LED
  }
}

Thank you sir!  May I have another?

You can add more state machines until you run out of memory or GPIO pins.  Each state machine can have its own flash rate.  As an exercise, edit the code above to add a third state machine.

  • First duplicate all of the state variables and code from one state machine.  
  • Then re-name all the variables to avoid conflicts with the first machine.  

It's not very difficult to do.  But it seems rather wasteful writing the same code over and over again.  There must be a more efficient way to do this!

There are better ways to manage this complexity.  There are programming techniques that are both simpler and more efficient.  In the next page, we'll introduce some of the more advanced features of the Arduino programming language.

Last updated on 2015-05-04 at 04.27.27 PM Published on 2014-11-03 at 01.41.03 PM