Go Down

Topic: New State machine tutorial (Read 15052 times) previous topic - next topic


Jun 11, 2018, 10:41 pm Last Edit: Jun 11, 2018, 10:41 pm by jbellavance
Hi @echigard,

Then you have a motor with 3 states : running CW, stopped and running CCW
You would probably want to rember the last direction your motor was running before it was stopped. (last running state)

The sensor changes the state of the motor's state as:
- if the state is (runningCW or running CCW) then {last running state = motor's state; motor's state = stopped}
- else: the motor's state is the last running state.

button1 state falling -> motor state : running CW
button2 state falling -> motor state : running CCW

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.


Hi @cyrut2,

Read this first.

PullUp is just the exact opposite of PullDown. hence:
Code: [Select]
if (switchMode == PULLUP) pinIs = !pinIs;
If the mode is pullup, pinIs = not pinIs

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.



Thanks for the tutorial sir, its very helpfull.

Btw, i want to ask you on Part 1 files > TwoStateMachines

Code: [Select]
enum LedStates{ON, OFF};

*what is [enum] function?


its was great!
Thanks for sharing :)


I have enjoyed reading the example PDF's. Very helpful!
I think that there is an error in Part 1, p. 9. The case IS_FALLING should start with a call to toggleMachine() as shown in the c++ files. Without this it is not different from the previous example.


Very grateful for this Jacgues.


It is best that you make all of your time variables for a use the same size unsigned integers. The lower 8 or 16 bits of millis() and micros() returns work fine as timers with limited max intervals.

I have a button class that debounces using 8 bit time variables. They take less room and use fewer cycles.

16 bit unsigned millis() is good to over a minute, Arduino variable type is word.

The compiler might not turn our mixed-math source into what we think it will in all circumstances.

Have you seen Nick Gammon's state machine example in his tutorial on reading text without blocking? It's at the 2nd address in my sig space.

1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.


Mar 08, 2019, 04:34 am Last Edit: Mar 08, 2019, 09:38 pm by terryking228
Thanks for the push on State Machines.  I once told the vendor of a $200,000  semiconductor tool that we wouldn't buy it until they provided us with a State Machine diagram of the factory communications interface.  They grumbled for 3 months, sent the diagram, and said "Oh, there's a major upgrade of the software we just installed".

Here's a State Machine example for a Traffic Light that includes vehicle detection on the side street and a Walk request button.  This is in a preprogrammed example package for young kids starting to connect hardware to Arduino.  

Awww. it didn't fit in 9000 bytes. Ummm...

OK, put it on my wiki here:  (NOT good formatting. I will figure it out.. )


Scroll down to last example..
Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info


Apr 14, 2019, 11:06 am Last Edit: Apr 14, 2019, 11:06 am by jeepi
Bit of a leap of faith to think that someone new to FSMs will know what an enum is.

Why is that?

An enum is a variable type which is present in many programming languages and is not more related to FSMs than any other variable types.

Go Up