When do you implement a state machine [SOLVED].

strantor

Joined Oct 3, 2010
6,798
I know very little about C. I mostly program PLCs so I'm forced to use integers to represent state, but even even when I program in Python I use integers rather than enum, because it allows me to do things like:

Python:
if state in range(60, 130):
   doSomething()
if state >= 330
   doSomethingElse()
As opposed to :
Python:
If state in [POST, STARTUP, IDLE, PAUSED, SLEEP, FAULT]:
   doSomething()
I increment the integers by 10 or 100 to leave room between them as I develop, in case I find more states are needed between existing ones. If using an integer, the integer could be arbitrary, but in my case it never is. It's an index that represents a position in the sequence.
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
Agreed. Use enum to make the code easier to follow.

Here is a typical example of using enum in a state machine.

enum State {POST, STARTUP, IDLE, ARMED, TRIGGERED, RUNNING, PAUSED, SLEEP, FAULT};
I was advised to design a state machine on the forum, I started to learn how to draw in this thread https://forum.allaboutcircuits.com/threads/button-state-machine-solved.182198/

I can attempt to design state machine but still I do not understand what is specific use of it
. this question is for me as i'm shooting in the air i don't know the target yet i'm firing
 

DickCappels

Joined Aug 21, 2008
10,186
Going back over the thread, I would say it is (when related to microcontrollers and computers) a programming technique. Use it when it helps achieve your goals.
 

strantor

Joined Oct 3, 2010
6,798
I was advised to design a state machine on the forum, I started to learn how to draw in this thread https://forum.allaboutcircuits.com/threads/button-state-machine-solved.182198/

I can attempt to design state machine but still I do not understand what is specific use of it. this question is for me as i'm shooting in the air i don't know the target yet i'm firing
Can you tell me what about post #18 did not answer this for you? I put a lot of effort into that and if it did not clearly answer your question for a specific reason then I would like to expound on it so that my efforts were not wasted. If your answer is simply tl;dr then I'm done here and I hope you never figure it out.
 

tindel

Joined Sep 16, 2012
936
A simple FSM is used in an oscilloscope to trigger events... it will be something like this:

1. Clear memory buffer
2. arm trigger
3. wait for trigger
4. store data to memory
5. Display new image

Once you complete task #5, you loop back around to 1, and the process repeats itself.
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
Can you tell me what about post #18 did not answer this for you? I put a lot of effort into that and if it did not clearly answer your question for a specific reason then I would like to expound on it so that my efforts were not wasted. If your answer is simply tl;dr then I'm done here and I hope you never figure it out.
@strantor I think I'm thinking too much about this topic that I don't need at all. I will gain more knowledge as I go on writing programs for real world applications. A flowchart, state machine, tables, timing diagram are a tool that helps us to solve our problems. Overall we have to write program for our system, it is not necessary to use flowchart or state machine. But a newbie can have many advantages from using those tool
 

trebla

Joined Jun 29, 2019
547
Overall we have to write program for our system, it is not necessary to use flowchart or state machine.
This is true for very simple systems. If the system has complex algorithm then it is good to draw some flowchart before starting code (sometimes also hardware) development to prevent future software issues and long debugging sessions due the conflicts in code.
 
Top