Can this be done with a state machine

Hello,

im just begun with the you tube course of Paul McWorther.
The first challenge is to make some code that a led blinks 5 times. another one 10 times and another one 15 times.

Now I also read about a state machine.

Could the challenge be solved with a state machine or can I better use three for loops to make this work.

a "timer" that uses millis() to check for a period of time to have passed and a counter are all that are needed to toggle an LED some # of times.

the timer and LED pin can changed after each sequence, or multiple timers can run simultaneously to toggle multiple LEDs at the same time

really just need a "sequencer" that moves sequentially thru a set of states

post what you think you will work

You could program it using a state machine but unless the sketch is required to do something else whilst the LEDs blink, such as reading an input, I would not bother

Three for loops running one after the other would work, as would a single function called from loop() with parameters defining which LED to blink and the number of times to blink

YES

For extra information and examples look at

you might benefit from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction

is a timer a state machine?

Look forward to doing many things different to how Paul McWhirter does as he runs you through the basics.

But do follow along slavishly if you can tolerate him. If you jump past a few dumb or less than perfect ways to do things, you will have missed an opportunity to let it all soak in.

Sometimes the best way to see a new method of doing things is from the perspective of having done things a few times or more using only simple logic and code.

It's easy to think you mastered something like the if/else statement or while loops or even "blink without delay" only to find yourself looking at code you can't even begin to read, let alone make any sense of, let alone letting alone writing anything that does something similar.

a7

it could be.
The states are LED ON, LED OFF and events is timeOn or timeOff expired and the blink count reached

Yes, with two states:

STATE_0: time has not expired 
STATE_1: time has expired

so is anything where there is some variable keeping track of something a state machine?

in this case there are several states:

  • which LED is being toggled or none
  • the time
  • the expiration time
  • the state of the LED
  • the count of the # of toggles (or On occurrences)

given this explanation, almost every piece of code is a state machine

but is this what is typically understood when someone says "state machine"?

Each LED can be implemented as a Task, and Tasks can be implemented as state machines - see TaskMacros.

usually you define the state of a coherent system.

as OP explained this

there does not seem any relationship between the leds, so you could consider there are 3 independent systems and if you can solve the issue for one system with some parameters, then you just need to instantiate the other two with other parameters

in the example of post 3 of Flashing multiple LEDs at the same time I took that approach, once the class was defined, it was just a matter of instantiating the LEDs

and the loop was pretty trivial, just ticking the state machine.

here it's roughly the same approach but you need to add one parameter which is the number of flashes.

not sure what this means. state machines typically have sub-states and its not uncommon for a single rather than multiple state machines. Or would a coherent state machine not have any sub-states (e.g. on/off state of LED within a machine that tracks which LED is toggling)

a single state machine would handle all states and all simuli, i'm thinking of a matrix. A single state machine would have cases where many stimuli have not effect in many states

I meant it depends on how you define "the system" for which you describe the states.

It could be one system with the three leds and then they could have 8 states (if 0 is OFF and 1 is ON ➜ 000, 001, 010, 011, 100, 101, 110, 111) and there is only one FSM

it could be 3 independent systems, each with its own FSM.

the class example I gave before is for the latter.

1. Do LEDs blink one after another (sequentially)?

2. Do LEDs blink concurrently (simultaneously)?

3. What is the On-time and Off-time of LED1 in a blink? For example: 250 ms On and 250 ms Off.

4. What is the On-time and Off-time of LED2 in a blink? For example: 500 ms On and 500 ms Off.

5. What is the On-time and Off-time of LED3 in a blink? For example: 1000 ms On and 1000 ms Off.

First I want to try to make them run one after each other. Maybe later blink concurently.
I think all led's are the same with time. so onTime 500ms and offTime also 500ms to begin with.

If it is clear to you how you can solve the first challenge with three loops, do it that way. Don't overcomplicate it.

If a state machine simplifies your understanding or your solution to the problem, use that.

Hello roelofw

Consider the straight forward coded example:

constexpr uint8_t LedPin{9};
uint8_t control = 0;
void setup()
{
  pinMode (LedPin, OUTPUT);
}
void loop()
{
  constexpr uint32_t Interval = 333;
  static uint32_t previousTime = 0;
  constexpr uint8_t NumberOfBlink[] {5, 10, 15};
  static uint8_t element = 0;
  if (control == 0 ) control =  NumberOfBlink[element] * 2;
  if (millis() - previousTime >= Interval and control)
  {
    previousTime = millis();
    digitalWrite(LedPin, digitalRead(LedPin) ? LOW : HIGH);
    control--;
    if (control == 0)
    {
      delay(2000);
      element = (element + 1) % sizeof(NumberOfBlink);
      control = NumberOfBlink[element] * 2;
    }
  }
}

hth

Have a nice day and enjoy coding in C++.

Have you succeeded? If yes, post your codes.

Thanks,

I have to study this one because im also a beginner in c++

1 Like

nope

I just wonder if I can do it this way or better learn state machines after doing the whole course.