Tutorial on code blocking.
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:
- BWD (Blink Without Delay timing) technique
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:
LEDsequenceDelay.ino (2.53 KB)
LEDsequenceMillis.ino (6.9 KB)