Go Down

Topic: State Machine and Timers, Medium level tutorial. (Read 2333 times) previous topic - next topic

larryd

Jan 27, 2018, 08:44 pm Last Edit: Feb 06, 2018, 07:14 am by larryd
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
No technical PMs.
The last thing you did is where you should start looking.

larryd

#1
Jan 27, 2018, 09:51 pm Last Edit: Jan 27, 2018, 09:51 pm by larryd
CheckTimer()




No technical PMs.
The last thing you did is where you should start looking.

larryd

#2
Jan 28, 2018, 07:44 pm Last Edit: Feb 06, 2018, 01:34 am by larryd
Timers now have the option of timing in millisecond or microseconds.

See attached file:
StateMachineStructureTimersMicros.ino


No technical PMs.
The last thing you did is where you should start looking.

Raynold453

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

GoForSmoke

#4
Feb 22, 2018, 02:44 pm Last Edit: Feb 23, 2018, 12:33 am by GoForSmoke
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.

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.

gregorss

#5
Feb 28, 2018, 02:40 am Last Edit: Feb 28, 2018, 02:41 am by gregorss
... 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
Meine Pizza schmeckt am besten, wenn ich sie selber esse.

lastchancename

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!
Experienced responders have a nose for laziness, (they were beginners once)... expecting the poster to contribute to the learning experience.

GoForSmoke

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

gregorss

Arduino has a String class and those are Container class.
The Arduino String class != STL.

Greets

Gregor
Meine Pizza schmeckt am besten, wenn ich sie selber esse.

GoForSmoke

#9
Feb 28, 2018, 09:58 am Last Edit: Feb 28, 2018, 10:05 am by GoForSmoke
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.
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.

gregorss

#10
Feb 28, 2018, 09:25 pm Last Edit: Feb 28, 2018, 09:34 pm by gregorss
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
Meine Pizza schmeckt am besten, wenn ich sie selber esse.

larryd

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.


No technical PMs.
The last thing you did is where you should start looking.

Jiggy-Ninja

#12
Mar 02, 2018, 04:09 pm Last Edit: Mar 02, 2018, 04:11 pm by Jiggy-Ninja
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.
Hackaday: https://hackaday.io/MarkRD
Advanced C++ Techniques: https://forum.arduino.cc/index.php?topic=493075.0

eskuvofoto

Best Regards,
https://davidfoto.hu/

Go Up