Arduino multiable state machines code

This sketch works, but it runs the 2 separate on/off times concurrently.

I would like to know if it can run the 2 separate on/off times consecutively.

Here’s the sketch:

// 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
}
}

You’d need separate info for each interval and test them all after another. Shortened like:

int led1_pin = D2; // pin
int led1_on = 0; // ms switched 'on'
int led1_dur_on = 250; // duration to keep 'on'
int led1_off = 0; // ms switched 'off'
int led1_dur_off = 750; // duration to keep 'off'
bool led1_lit = false; // state: switched 'on'?

int led2_pin = D4;
int led2_on = 0;
int led2_dur_on = 123;
int led2_off = 0;
int led2_dur_off = 987;
bool led2_lit = false;

void setup()
[ . . . ]

pinMode( led1_pin, OUTPUT );
digitalWrite( led1_pin, led1_lit );

pinMode( led2_pin, OUTPUT );
digitalWrite( led2_pin, led1_lit );

[ . . . ]

void loop()
[ . . . ]

int ms_now;

ms_now = millis();

// is led1 switched on? is it time to turn it off?
if ( led1_lit ) {
  if ( ms_now - led1_on > led1_dur_on ) {
    led1_off = ms_now; // remember ms for 'off'
    led1_lit = ! led1_lit; // or: false --> not lit
    digitalWrite( led1_pin, led1_lit );
  }
}

// repeat for 'off' to 'on'

// repeat both for led2

[ . . . ]

If ‘on’ and ‘off’ duration are the same you could simplify that dramatically, but I wouldn’t suggest that. If you would like to set a larger number of LEDs blinking it would make sense to use a loop through an array instead of copying almost similar blocks of code.

Michael