Go Down

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

jbellavance

#15
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

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

jbellavance

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

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

nielyay

Hello,

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?

nielyay

Hi,
its was great!
Thanks for sharing :)

dfitterman

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.

Taser2012

Very grateful for this Jacgues.

GoForSmoke

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.

terryking228

#22
Mar 08, 2019, 04:34 am Last Edit: Mar 08, 2019, 09:38 pm by terryking228
Hi,
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.. )

https://arduinoinfo.mywikis.net/wiki/Easy-Connect-Software-Outline

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

jeepi

#23
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.

johnnycanuck

This is a very cool tutorial. I've been doing it somewhat sloppily the past few years, and this may help clean up my act. Also motivates me to look for and/or (eventually) come up with an object oriented version.

GoForSmoke

This is a very cool tutorial. I've been doing it somewhat sloppily the past few years, and this may help clean up my act. Also motivates me to look for and/or (eventually) come up with an object oriented version.
Actually, state machines have been used in OOP code since at least 1970 when Charles Moore released Forth.

State machines are a technique, Johnny.

What part of Canada are you from? My family reunion is still held in NB, you might be a cousin.
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.

GoForSmoke

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

*what is [enum] function?
It is not a function, more like a variable, it tells the compiler to associate labels with numbers under a certain name.

enum unoSpi { mosi=11, miso, sck };  // mosi starts as 11, miso is 12, sck is 13

enum rgbColor { red=1, green, blue }; // starts at 1

enum smallNumbers { zero, one, two, three };  // zero is by default 0, one is 1, etc

When I have a function that has an unoSpi arg, the compiler will only accept unoSpi values in that place.

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.

Go Up