threading/multiple threads

I made two separate libraries, and I need the code in void loop, below, to run simultaneously. I've heard that in general this is possible with threading, but I have no idea how to do it. I also saw in a post that this may not be possible on the arduino. Is this the case? If so, what other options do I have? Would it be best to try to incorporate the libraries into a single library? The basic idea is that I want to create a multisensor feedback loop, in which each object is looking for a different type of input, and emitting a different output based on that input. One of the libraries reads in analog input from one pin, maps it to a meaningful output and writes out onto another pin. The other library controls a servo motor which scans a room looking for meaningful input and, when it finds it, goes to the location with the greatest input.

The two libraries work fine independently, but do not work together. This is where I try to call them together:

#include <MoreInMoreOut.h> //controls output of object (light/sound, etc) based on input 
#include <carrot_h2.h> //controls motor scanning the room
#include <Servo.h>
#include <Array.h>


MoreInMoreOut buzz1(2, 9); //2 is input (distance), 9 is output (piezo)
MotorScan firstMotor(10, 2); //10 is output (motor), 2 is input (distance)

void setup()
{
   Serial.begin(9600);           
 Serial.println("test");
}


void loop()
{
  firstMotor.scanAndGetSerialValues(); 
  buzz1.getSerialValues();
  delay(10); 
//how can I get these to run parallel to one another instead of consecutively? Should I try to combine it into a single library?
}

Thanks for your thoughts!

First, yes you can do multithreading - or at least co-routines on the Arduino, but usually it isn't a good solution and it creates more work than it resolves. In your case, your libraries probably suck and take too much time. First get rid of all delay statements inside and outside the libraries and then arrange things so that if one thing doesn't need to be done, another thing is tried. One could also suggest you take a look at the Blink without Delay tutorial. (I really need a button to automatically insert that link)

For time critical functions, you also have the option of using interrupts, which is still better than multiple threads, but considering how you asked the question I doubt this will improve matters much for you. Those also need some more understanding of how the processor works and what cannot be done inside an interrupt service routine and what's just a bad idea.

To sum it up, I advise you to clean up your code so things works properly with one single loop. If you think that's impossible, let us now why and give some more details about what you try to achieve.

Korman

http://www.arduino.cc/playground/Code/TimedAction#Example

Have a look at the 'Three Examples At Once' it runs three Arduino sketches simultaneously.

thank you, both for your posts. Since the two tasks I wanted to call simultaneously are very basic, I've worked around the problem using conditional statements, and everything seems to be working so far.