FSM for Scheduling

I need help to know if I have a finite state machine diagram for multitasking. How to convert it into scheduling FSM?

There are two tasks. One blinks a led every one second. The other task is to turn on a LED only when the user presses a button.

I want one led to blink every 1000 ms. The other task is to respond to a user pressing a button by turning on a led and period for this task is 100 ms.

I have already finite state machine diagram, Please look at attached picture Now I am trying to make FSM for scheduling, There may be different states in scheduling like ready, running, suspended blocked. How to convert it into scheduling FSM?

FSM.jpg

I want one led to blink every 1000 ms.

That is straightforward BlinkWithoutDelay which is, in essence, a rudimentary stand alone FSM

How many states are there in the other FSM ?
My favourite method of implementing a FSM is to use switch/case. One case for each state so that only the code for the current state is executed. Timing states should use millis() timing to avoid blocking the sketch

UKHeliBob:
How many states are there in the other FSM ?

That's my question How many states should be in other FSM. I have seen mainly four states read, running, suspend and blocked

If I am not wrong I think there should be three in other FSM ready running and suspend

FSM.jpg
The second FSM in your does not match your description

The other task is to respond to a user pressing a button by turning on a led and period for this task is 100 ms.

Now you say

I think there should be three in other FSM ready running and suspend

Please describe exactly what you want to do. How many FSMs do you think there should be ?

Rehan11:
That's my question How many states should be in other FSM. I have seen mainly four states read, running, suspend and blocked

If I am not wrong I think there should be three in other FSM ready running and suspend

You’re confusing the states of a running process (in a real time operating system) with the states of a FSM.
These are completely different types of “state”.

A FSM has as many states as necessary as dictated by the problem you are trying to solve.
In the case of a simple blinking led there are two states, ON and OFF.

More complex problems will have many more states.

Arduino isn’t a RTOS, so you can ditch any pre-conceived idea you already have of ready, running, suspended and blocked. They are not relevant here.

Edit: Looking back at your post history and your previous questions on RTOS subjects it would appear you are still very confused on this point.

UKHeliBob:
FSM.jpg
The second FSM in your does not match your description
Now you say
Please describe exactly what you want to do. How many FSMs do you think there should be ?

Image shows the behaviour of two tasks

I want to create FSM like given in page OS Process States - javatpoint for FSM posted in my Image

Both of these images are already FSMs. You question makes no sense.

Your FSM image describes the states a couple of LEDs could be in, and the events which cause transitions between those states.
The java FSM describes the states a RTOS process can be in, and the events which cause transitions between those states.

The Java one isn’t going to help you understand or implement anything on Arduino. Arduino does not run a RTOS.

pcbbc:
You’re confusing the states of a running process (in a real time operating system) with the states of a FSM.
These are completely different types of “state”.

A FSM has as many states as necessary as dictated by the problem you are trying to solve.
In the case of a simple blinking led there are two states, ON and OFF.

More complex problems will have many more states.

Arduino isn’t a RTOS, so you can ditch any pre-conceived idea you already have of ready, running, suspended and blocked. They are not relevant here.

Edit: Looking back at your post history and your previous questions on RTOS subjects it would appear you are still very confused on this point.

Right now I am not thinking about RTOS. It's a difficult subject beyond my skills and knowledge

You say

The other task is to respond to a user pressing a button by turning on a led and period for this task is 100 ms.

Do you mean that the program should only check the state of the button every 100ms ? If so, then why, because you could do it every time through loop(), ie thousands of times per second

As previously requested, please describe exactly what you want the program to do.

Rehan11:
Right now I am not thinking about RTOS. It's a difficult subject beyond my skills and knowledge

Great, then ditch your java process FSM diagram. It’s not relevant here anyway.

Now, just looking at your FSM for the LEDs it has 2 state variables, each with two states. Coincidentally both state variables say if a particular led is ON or OFF.

Now what’s your question?

As noted, blink without delay takes care of your first LED. Then you need an independent piece of code to take care of the other. The question is what the 100mS is for.

Do you check the button every 100mS and set the LED to match? Do you check continuously and having noticed a press, ignore the button for 100mS? Some other thing?

The whole point about state machines is that they are dead simple to run in parallel:

void setup()
{
  init_state_machine1() ;
  init_state_machine2() ;
}

void loop()
{
  handle_state_machine1 () ;
  handle_state_machine2 () ;
}

I came across link How to Multitask in Arduino - bright developers

How many state tasks (ready, running, suspended, blocked )there in the link

Please help us and yourself by describing what the sketch should do as you have been asked several times Stop using buzz words like multitasking and FSM and linking to sites that do not help solve your problem

Rehan11:
I came across link How to Multitask in Arduino - bright developers

How many state tasks (ready, running, suspended, blocked )there in the link

Two state machines with two states each:
LED 1 ON + LED 1 OFF
LED 2 ON + LED 2 OFF
So there are four states in total.

The states in a state diagram are the circles. The events which cause transitions between those states are the arrows.

Edit: The Introduction of “scheduling” into that tutorial/description is not helpful in my opinion. It implies that every state machine requires a regular “tick” in order to transition state. Quite often in FSMs the events that transition states are NOT regular ticks. They are things like buttons being pressed, or data arriving from a serial port. For those sorts of event you can/should continually check for the event as often as possible (i.e. every time through loop).

An RTOS tick normally supports process prioritization and pre-emption. In the code linked to above, true pre-emption is not possible because the ticks are not associated with an interrupt that can save the full process context including CPU registers. That makes the tick mostly an academic exercise, since the lack of a true context switch forces all the user code to be written in a non-preemptive way. Such code can run perfectly well without a task scheduler, the only advantage to it that I can see, is to centralize and codify the FSM and also process prioritization (in the example given, the "user LED" process is given 1/10 the CPU time as the "LED" process).

It might help someone understand RTOS principles, but it's not really a very practical, workaday solution.

I want to make a finite state machine diagram for below example in the context of scheduling

Example: Blink LED at every 500 ms but when a button is pressed stop blinking

Maybe this example is not suitable I'm just assuming this to understand states in scheduling. How to create FSM scheduling that represents a given example

I want to make a finite state machine diagram for below example in the context of scheduling

Sorry, but I don't see where scheduling comes into this

Rehan11:
I want to make a finite state machine diagram for below example in the context of scheduling

How to create FSM scheduling that represents a given example

It feels like you've read an article about this and are trying to jam a random example into an unnecessarily restrictive framework.

If you need an Arduino to run one or many FSMs to solve a particular problem, you can. There have been many examples posted here over the years. What you won't find I suspect, is any that tried to do it as a general purpose scheduler - there's no point because as previously noted, the Arduino isn't equipped to run any preemptive scheduling, it's all cooperative multitasking.

There is a task switching framework available I think, but as all such things that don't have proper hardware features to back it up, it relies on each "thread" yielding, which means it's rather fragile.

Specially for finite state machine application, i’ve build some software. Look at my website. There are several examples to download. The above “problem” with 2 different threads can be made easy ( www.jbsiemonsma.nl). If you want some extra information please leave a massage.