Waiting for light to turn off before activating the next one

  arduino, automaton, c++, nodemcu

I want to make a program with the Automaton library that turns four lights on, one after the other, continuously. The program is supposed to activate when a button is pressed. Each light is lit for 600ms and the next light gets activated after 300ms. The last light is supposed to turn off fully before the first one gets activated again.

The first time the program runs it works fine. All lights turn on when they are supposed to.
However the problem appears when the light goes into the second loop because now the first light gets activated while the last light is still on, which is not supposed to happen.

Is there a way to implement some kind return or stop event which lets the program know to wait until the last light is turned off before going into the next iteration of the loop?

Here is what I have tried so far. The order in which the lights are supposed to turn on is: blue1 -> blue2 -> red -> green

#include <Automaton.h>

Atm_led blue1, blue2, red, green;
Atm_button button;
int on = 600;
int off = 300;

void setup() {
    blue1.begin(D0, true).blink(on);
    blue2.begin(D4, true).lead(300).blink(on);
    red.begin(D5).lead(600).blink(on);
    green.begin(D6).lead(900).blink(on); // I tried to use onFinish() here but that didn't work

    button.begin(D2).onPress(buttonpress);
}

void buttonpress(int idx, int v, int up) {
    if (v) {
        blue1.trigger(blue1.EVT_TOGGLE_BLINK);
        blue2.trigger(blue2.EVT_TOGGLE_BLINK);
        red.trigger(red.EVT_TOGGLE_BLINK);
        green.trigger(green.EVT_TOGGLE_BLINK);
        // I tried to add a delay() here but that didn't work at all
    }
}

void loop() {
    automaton.run();
}

Source: Windows Questions C++

LEAVE A COMMENT