Skip to content

Event-driven multi-tasking

Example exercise

A button and an LED are connected to an ESP32, create a script that does the following:

  • If the button is pressed for less than 1s, the LED changes state.
  • If it is pressed for more than 1s, the LED blinks (500ms ON, 500ms OFF)
  • If the button is pressed again, it turns off and deactivates the blinking (regardless of the state it is in).

Exercise 6: FSM hardware solution

For this solution we need to use an extra cable and GPIO pin for the button.

↑ b:

activate timer_t
state = 1

timer_t:

long_press = true

↓ b:

deactivate timer_t
if (long_press)
    state = 3
    activate timer_blink
    long_press = false
else
    state = 2
    LED ON

timer_blink:

LED = !LED

↓ b:

LED OFF
state = 0
deactivate timer_blink

Download the solution here

Exercise 7: FSM method 1

ç

↑ ↓ b:

if (state == 0 && b)            // before it was green
    activate timer_t
    state = 1
else if (state == 1 && !b)      // before it was magenta
    deactivate timer_t
    if (long_press)
        state = 3
        activate timer_blink
        long_press = false
    else
        state = 2
        LED ON
else                            // before it was light blue
    LED OFF
    state = 0
    deactivate timer_blink

timer_t:

long_press = true

timer_blink:

LED = !LED

Download the solution here

Exercise 7: FSM method 2

The FSM design is the same as in exercise 7, but the software implementation is different: in this case there is a a prior FSM state evaluation.

Download the solution here