Simultaneous Tasks


I'm trying to build one alarm but I came with a little problem. I want it to keep playing a certain sound but at the same time wait for one SMS to be received. The thing is that searching if there is one SMS takes too long and the alarm stays a little strange, having a short stop. Also I have some buttons that have to wait for all this checkings, which makes me need to press it for a while before it acts. Is there any way to make independent actions to solve this problem?

Best regards, Rhen

See blink without delay - get rid of all the delays in you program.


Also, you can use timers and interrupts. Interrupts will stop the current running task when triggered and switch to another task, and when that is finished back to the original task. Interrupts can be triggered by a state ( change) of an input pin. Timers work in a similar way, but are triggered by a timer that hass expired. Making two things happen at exactly the same time in not possible, but if you switch fast enough, it will appear as if they happen simultaniously.


No you can't use interrupts in that way!. An ISR needs to be kept a short as possible. And while interrupts work during delay() you can't see the result of the interrupt until the delay is complete. In addition to that, when in an ISR other interrupts are turned OFF, so the timer (mills etc) and Serial don't work as normal. Using Serial inside an ISR or in a function called from the ISR may crash your program.


You can - not by putting everything inside an ISR - but by using a state variable and multiple short loops this works like a charm. It is all in the design of the program. In complex situations this is not an ideal solution, as that basically comes down to writing a simple kernel, but if the number of tasks is low and the tasks are simple it works.