Go Down

Topic: Code Blocking, Medium level tutorial. (Read 1 time) previous topic - next topic

larryd

Feb 03, 2018, 11:27 pm Last Edit: Feb 04, 2018, 01:40 am by larryd
Tutorial on code blocking.

Attached is:
LEDsequenceDelay.ino
LEDsequenceMillis.ino
LEDsequenceDelayFlowChart.jpg
LEDsequenceMillisFlowChart.jpg

Two sketches are presented in this tutorial:
- In the first sketch, the user wanted to be able to push a switch on pin 2 and have LED6 toggle on/off. However, switch control of LED6 is a hit and miss proposition as other LED control takes up most of the controller's processing time.
This sketch uses the delay() function which stops normal code execution until a delay period has expired.
Notice switch processing is blocked by every call to the delay() function, 7.2 seconds in total.
We refer to sketches written as such as having blocking code.




- The second sketch uses a BWD (Blink Without Delay) based Timer instead of delay().
Switch control of the LED6 can now occur quickly since the other LED control is written in non-blocking format.
All timing is handled by calling CheckTimer(sharedTimer).
The CheckTimer() function does not wait for any event, it simply checks and operates on things accordingly.



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.
This tutorial introduces the concepts of:
- structure
- BWD (Blink Without Delay timing) technique
- timers
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 have control over your Timers.
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.


See these links for great information on the BWD (Blink Without Delay) technique:
http://forum.arduino.cc/index.php?topic=503368.0

http://www.gammon.com.au/forum/?id=12127

http://www.gammon.com.au/blink


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

wilfredmedlin

the user wanted to be able to
Just curious if that was a forum question, in which case could you link to the thread? It would be good to see the context, and any other posters' approaches, including that of the thread's OP.

Keep Calm and call 0118 999 88199 9119725...... 3

larryd

I don't remember which posting this was from.
Similar questions appear quite often in the programming forum.

Most answers contain:
"use BWD instead of delay()"
''study the BWD example in the IDE examples"
or see
"Demonstration code for several things at the same time"




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

Go Up