NOTE for newcomers. The demo is entirely contained in this Post and Reply #1. There is no need to read further unless you are interested. …added 25Sep2014
There have been a few occasions recently where newcomers seemed to have difficulty applying the “blink without delay” and “state” concepts to a sketch that is intended to manage a number of concurrent actions.
Its very time consuming to respond with suitable code examples - particularly as every case is a little different.
I have prepared the attached example sketch in the hope that it will be a suitable model. I have tested it on an Uno and a Mega. I have called it “SeveralThingsAtTheSameTime.ino”. (It seems to be too long to show the code directly in this post)
My sketch uses the concept in “blink without delay” to cause three LEDs to blink at different intervals, a fourth LED is controlled by a button and a servo sweeps back and forth at two different speeds. The idea is to demonstrate how different processes can be accommodated in the same general framework.
It also uses the “state machine” concept to manage the various activities and enable the different functions to determine what to do.
I have deliberately designed the sketch as a series of short functions. Short pieces of code are much easier to understand and debug and it will be an easy matter for a user to delete functions that they don’t need or to duplicate and modify functions if they need more of the same (for example to flash 5 LEDS at different intervals).
There is a case for saying there is too much in the sketch but I concluded that the larger number of activities is a better demonstration of the capabilities of this approach.
Comments are welcome.
…R
Edit … the attached sketch has been revised to take account of comments below about improving the timekeeping …R
SeveralThingsAtTheSameTimeRev1.ino (9.37 KB)