State Machine and Timers, Medium level tutorial.

This sketch introduces the concepts of:

  • macros
  • structure
  • enumeration
  • BWD (Blink Without Delay timing) technique
  • timers
  • State Machine
    The purpose of this example is to integrate the above concepts into a sketch to see how they can be used to simplify coding and make that coding more efficient.
    An important feature is you can control your Timers.
    Under program control, Timers can be running, restarted and disabled.
    When it comes to the ‘State Machine’, you can see how the sketch’s current state can be sampled to see if things need to be done in that state.
    Effort has been made to make this example non-blocking, i.e. code execution does not pause for events. This makes your code responsive and more easily followed.
    The example was purposefully made to be self contained to avoid hiding code in an external module.
    The attached code is supported with a flow chart to graphically show what is happening.

Attached is:
StateMachineStructureTimers.ino
FlowChart.jpg
StateMachineSimpleStructureFlowChart.pdf

Note to people just starting out with C++.
This example covers some more advanced techniques, these may make things frustrating for new people to follow what is happening.
If new users find things confusing, it might be best to first master all the basic examples that come with the IDE.

Edit
Update sketch

StateMachineSimpleStructureFlowChart.pdf (324 KB)

StateMachineStructureTimers.ino (13.9 KB)

1 Like

CheckTimer()

CheckTimer().pdf (258 KB)

Timers now have the option of timing in millisecond or microseconds.

See attached file:
StateMachineStructureTimersMicros.ino

StateMachineStructureTimersMICROS.ino (14.9 KB)

I just started learning with C++ hope this help me to learn some extra curriculum.

Raynold453:
I just started learning with C++ hope this help me to learn some extra curriculum.

Be aware that the average Arduino has 2048 bytes of RAM for heap and stack combined. The C++ Container classes and dynamic allocation you would do with lots of RAM are bad ideas in small environments unless you like bumping your head!

If there is a used book store you can get to, look for a C manual. You can find many good C sites online, I use those because I can zoom webpages to read the text.

You want to cover C char-array strings and NOT use C++ String objects. You don't have to know or use <string.h> but you do need to see strings as char arrays with null terminated ASCII data fields.
Set up your string buffers and max len values as globals from the start, and keep all the constants in flash (Uno can hold 24KB + good size sketch) with PROGMEM and F() and you can do more than dynamic allocation shotgunning&inflating the heap will let be done.

If you attach SD, the library has a 512 byte DOS buffer with an address in your RAM. You can address that space as variables for your sketch, fill it up with values and write the buffer to SD to save the full state of the sketch which then could be reloaded and started back up. Program = code + data.

GoForSmoke:
... The C++ Container classes ...

What Container classes are you talking about? AFAIK the avr-libc lacks support for new() and delete(). Thus something like the STL is not possible. Were there any news I haven't read yet?

Greets

Gregor

I love to see flowcharts being used in tutorials.
It is the perfect way to illustrate to beginners how and when parallel ‘threads’ have to cooperate.

Of course it’s a great general documentation tool as well. But we all admit to cutting corners - to our own detriment!

gregorss:
What Container classes are you talking about? AFAIK the avr-libc lacks support for new() and delete(). Thus something like the STL is not possible. Were there any news I haven't read yet?

Greets

Gregor

Arduino has a String class and those are Container class.

Anyway, Arduino uses AVR LibC but adds to it, go look at the source to Serial, IIRC it's in Java.

Have you tried to use new on Arduino?
https://forum.arduino.cc/index.php?topic=103010.0

GoForSmoke:
Arduino has a String class and those are Container class.

The Arduino String class != STL.

Greets

Gregor

Make a String. Add a character to it. It copies itself with the added character and deletes the old copy. Heap, meet shotgun.

Call it what you want, it behaves as a Container. It's why we so often have to explain that String use is bad on AVRs.

And just you know, yes I expect there are those who would implement the whole set just for some weird satisfaction. When they do, it will be a bad idea to use them on AVR's.

GoForSmoke:
Call it what you want, it behaves as a Container.

It sure does. But the STL is far more than just one Container. Just think of different queue types (FIFO, LIFO), Lists, Maps ...

Again: Arduino Strings are notthe STL“. I have not yet seen any code that uses new(), delete() or templates like the ones in the STL.

Greets

Gregor

gregorss:
It sure does. But the STL is far more than just one Container. Just think of different queue types (FIFO, LIFO), Lists, Maps ...

Again: Arduino Strings are not „the STL“. I have not yet seen any code that uses new(), delete() or templates like the ones in the STL.

Greets

Gregor

This discussion might be better continued in the Programming forum.

gregorss:
Again: Arduino Strings are notthe STL“. I have not yet seen any code that uses new(), delete() or templates like the ones in the STL.

Greets

Gregor

Nobody claimed that String is an STL-compatible container. It's obvious that it's not. GoForSmoke called it a container, period. The concept of a container class is much more generic than just the specific STL implementation.

And while it does not use new or delete, it does use realloc and free which are basically the same thing.

Thank you for your help and information.