Go Down

Topic: State machine for Arduino tutorial (Read 5108 times) previous topic - next topic

jbellavance

Hi,

I just finished a 3 parts State machine tutorial.

You can find it here.
Let me know what you think about it.

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

larryd

I do like the document aesthetics. :)

Looked at things quickly, two small 'constructive' criticisms .
- for beginners, suggest you document the lines of code so a person can more easily follow the code and your program logic.
- personally, I do not like a bunch of code all stuffed on one line. White space is free but we discussed this preference before ;).

The commenting is better in #3.

You are having way too much fun!

Great work!


No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

jbellavance

Hi Larry,

Just added the commented sketches... with a single statement per line  ;)

Enjoy, countryman.

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

Part two now has a traffic light example besides the existing vending machine example.

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

6v6gt

I looked quickly through and it appears very nicely put together.

I guess the main issue for a beginner is (a) recognising whether a specific real world problem is a candidate for the state machine approach and then (b) modelling that problem as a state machine in a systematic way.
Once that level has been achieved, the translation of the state machine model into code should be a relatively straight forward exercise.

I did a simple example for someone here and it was basic enough to describe in an excel table and I left the code more or less 1:1 with the table (although it could be optimised).

jbellavance

Hi 6v6gt,


Thanks for taking the time to look at my tutorial.

The idea of using a table is another great way to describe the machine. It is one of the tools used when designing hardware state machines where all combinations have to be adressed.

I looked at the table and the code that you suggested and it certainly works. As you have seen in my tutorial, I make the transition decisions in the state machine itself. That allows the code for each line in the table to be in just one place.

Have a nice day.

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

beatty_dt

Question:

If void setup(): is run only once, why increment a variable there.

From the elevator code:

void setup() {
for (byte i = 0 ; i < 8 ; i++) {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH); }

Thank you for helping.

David

beatty_dt

#7
Nov 02, 2019, 03:33 pm Last Edit: Nov 03, 2019, 03:09 pm by beatty_dt
My reference is incorrect.

From State machine with Arduino Part 1. pdf
page five

I have since learned setup() and loop() are simple calls to functions nothing special.

David

6v6gt

The example has probably been corrupted because you have not used code tags and the [ i ] subscript is missing.
It is perfectly normal to configure Arduino pins and other hardware items in setup() if these need to be done only once in the program.

beatty_dt

Thank you for your reply.

I have since learned setup() and loop() are simple calls to functions nothing special.

Was under the impression void setup() was only ever called once.

David

6v6gt

Thank you for your reply.

I have since learned setup() and loop() are simple calls to functions nothing special.

Was under the impression void setup() was only ever called once.

David
It is correct that setup() and loop() are function calls.
setup() is called only once at the beginning of execution and is used for initialisation activities like setting the direction of pins OUTPUT, INPUT etc. which are usually done only once. That is not the same as setting the value of an output pin (HIGH or LOW) or reading the value of an input pin, which can be done many times during the programs execution.
loop() is called continuously.

beatty_dt

I note in Mr. Bellavance's state machine elevator code taken from GitHub, that void setup() appears near the end of the program. Program line number 230 on my copy of the Arduino IDE. I learned the program will not compile if the function void setup() is not present. I learned it does not matter whether void setup() appears early or late in the program. The program will compile.

I learned also that the function void main() need not appear in the program at all. Indeed Mr. Bellavance does not use the function name. The program compiles without it.

Mr. Bellavance uses void loop() as the last function call.

David

Go Up