Is it possible to do multitasking in arduino
Is it possible to do multitasking in arduino
Hi Al,
There are a few projects for Arduino RTOS if you google it. Also you can use timer interrupt to perform tasks like this one
http://chrisbarlow.wordpress.com/2012/09/13/reliable-task-scheduling-with-arduino-avr/
However Arduino's (depending which board) resources is too limited to do multitasks (depending on what kind of task you are doing). Sometime a properly designed pooling method in a loop works better. It all depends on what you want to do.
Cheers
JP
To add to JPMonkey's comment and without knowing what your trying to achieve, in some cases it is better to distribute the logic across a few micro-controllers than to try to multitask it all in one. As soon as you start multitasking you loose some of the tight control of code you had and chose a micro-controller to deliver and therefor the system as a whole becomes less predictable. This does not include the use of interrupts, interrupts are a great way of diverting to a separate very specific task that an external event has triggered, interrupts are often used to run code that otherwise would be implemented through repetitive polling or other inefficient means.
As with everything, there are always different ways to get the job done, let us know what that job is and we can better advise you
Peter
Its software so you can design a piece of code that can essentially multitask by executing a chunk of task for some time and then move to the next task. This is called scheduling and is an OS concept. In microcontroller what me experience is that you are looking to do simple things like receive UART Data, run an LCD, read some sensors, write to a memory card etc etc and you are possibly confused as to how you can make sure everything happens correctly. There are a couple of ways to do this:
1. an RTOS. A piece of code which will manage things for you. Loads of info available and the advantage is that they reduce complexity(somewhat) but have the disadvantage that you need to have some knowledge on the subject and then how to use and if you have enough space. etc. For a lot of complicated tasks this approach is preffered.
2. Interrupts: mircontrollers have interrupts that allow you to execute pieces of code when an even happens e.g. button pushed. pin toggle, usart byte is received, a timer overflows(you set the time interval). This is good enough for a lot of small tasks. Things that can be automated are usually put on interrupts while one or two tasks are put in the main loop. You need to be careful how you configure things because sometimes you end up with bad code that makes your application "stuck" somewhere.
3. Interrupts and state machines: You can write state machine code that is basically saying DIY Scheduler. This is simpler that it seems that there are a good number of books that show you how to write the right code. You can use a time to trigger the SM to jump between tasks but then again it depends on how you like to code.
In Summary I can say that it is possible in a variety of ways but unless you specify your exact intentions, I cannot revert with a definite answer.
Hope this helps.
Cheers,
IP
OK, I noticed you did not actually get an answer your question
YES you can do multitasking on an arduino... sort of
whether you want to depends on what your trying to achieve
How successful it is depends on the complexity and available resources in the chip you choose to use, primarily RAM and Speed etc.
You will never achieve the sophistication of multitasking on Windows, Linus or similar OS, the micro-controllers are just not up to the task for the reasons above and there not meant to be either. If you want that kind of multitasking then look to a PI, net-duino, beagle bone or better.
If you need a simple scheduler (Poor man multitasking
) to ensure certain tasks are performed at specific intervals then this is much more common and there are plenty of examples on how to do this
here is couple to get you going
http://arduino.cc/en/Reference/Scheduler#.UysGfXmPJIk,
and
http://www.codeproject.com/Tips/731031/Scheduler-Library-for-Arduino
Hope this helps
Peter