How do you run more than one subroutine at the same time in arduino?

Is there a way to run more than one subroutine at a time with arduino. Or is there a way to run to commands at once.  Such as moving two servos at the same time, or running a piezo melody, and a led matrix or pattern at the same time. 

Thanks in advance!!!


gmoon7 years ago
The easiest way to is to use the IRQs. An IRQ is a "hardware interrupt."

AVRs (and other uCs) have this capability built-in. An Arduino is, of course, an AVR with some added programming (firmware.)

The most common type of interrupt is a timer IRQ. When the internal timer counts down from a certain value, it triggers the execution of an external routine--an ISR, or Interrupt Service Routine. The timer then resets itself, and code execution returns to the exact point it left...starting the process all over again.

Here's a nice tutorial for using Arduino interrupts.

Of course, you could also "break up" your own code into subroutines and "schedule" it manually.
Jayefuu7 years ago
I googled "Arduino Multitasking", there were loads of forum help threads on the Arduino.cc website.

This is the basic tutorial on running events after X microseconds instead of using delays all the time. If you can understand that tutorial, you should be able to answer your question.

Hope this helps!
JaredsProjects (author)  Jayefuu7 years ago
I'm not quite sure how the tutorial works for making to different subroutines at the same time.  Can you explain how it can make two subroutines at once.  Is it like you tell both to start at a certain time, which is a few microseconds away, and then they both start?

Thank You!!!
You can't  "run more than one subroutine at a time" in a single CPU. All you can do is rapidly switch between them.

Triggering things off a timer is the simplest solution to program -- a poor man's scheduler. You set up a main routine which knows that each of the subroutines runs once every N microseconds, and it kicks them off each time that delay elapses. You can get fancier with this by assigning tasks priorities, and/or by giving them the opportunity to "yield" the processor so other tasks can run and then resume where they left off (so slow tasks don't keep other tasks from running for too long); that eventually evolves into a full collaborative multitasking systems like the one Windows was eventually based on.

For your application, though... If you're controlling the piezo transducer directly from the program, you probably want the timing to be Pretty Darned Precise. In that case, I'd suggest that you write the light updates into the music loop -- have them happen only between notes, or process them during the delays between piezo on/off transitions. The former is relatively easy; the latter would require understanding exactly how long each light update takes and figuring that into the loop frequency settings.

I'd go with updating lights between notes, if that works for your intended application.. A timing glitch during a silence will probably go unnoticed by the human listener; giving the impression of simultaneous processing. A glitch while a note is actually being produced is likely to be much more audible, and avoiding glitches is going to require much more careful coding.

Or move the note generation off the main processor, so the Arduino only has to keep track of when notes start, stop, and what their frequency is... which can be handled much more easily by the timer-driven scheduler.