State machine for Arduino tutorial

Hi,

I just finished a 3 parts State machine tutorial.

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

Jacques

I do like the document aesthetics. :slight_smile:

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!

Hi Larry,

Just added the commented sketches... with a single statement per line :wink:

Enjoy, countryman.

Jacques

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

Jacques

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).

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

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

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

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.

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

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

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.

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