Go Down

Topic: multitasking with arduino (Read 658 times) previous topic - next topic

jon

is there an easy way to program arduino to carry out 2 tasks simultaneously?

for example:
i'm developing code to interface with a sensor over a SPI interface.  data readings from the sensor are clocked into arduino at about 500 times per second.
i want to smooth the data using some routine similar to the [LINE] object in max, so that i can output a stream of data from arduino at a higher rate (about 6000 samples/sec) to another device.
just wondering if there's a way to run the data smoothing routine AND the sensor acquisition routine simultaneously, without having to intersperse bits of code...?

Daniel

#1
Feb 19, 2007, 04:33 am Last Edit: Feb 19, 2007, 04:38 am by Daniel Reason: 1
hi

what do the 'samples' get clocked out over-- the serial port?

CosineKitty always has the answer ot this kind of stuff. He's our in-house code guy. :)

D

jon

the 'samples' i want to export are just 12-odd-bit numerical data, representing either musical pitches or other control information (sorry about my lack of technical vocab...).  if serial is fast enough, i'll use that just because it's easy.  otherwise i'll use as many spare digital pins as possible to improvise some kind of parallel interface.

i need to provide a relatively smooth, rapid stream of data, as compared to the data i get from the sensor, which is slow(ish) and sounds jittery when i feed it to a tone generator.  the data could also be sent to some kind of DAC for conversion to control voltages, or perhaps routed over a midi interface, or sent to any number of different gadgets.

so ideally, i'd like to have some kind of program structure that splits arduino's processing time between the tasks of communicating with the sensor, and sending out a smoothed data stream.

another option is perhaps some kind of external processor that takes the data direct from the sensor, or arduino, and does the smoothing automatically... but i've never heard of such a thing, or know what it might be called...

any ideas?

Daniel

hmmm

so you want to interpolate between the numbers, or fill in the blanks so to speak. Your sensor data is not going to be 'slowish' if it is coming in at 500hz, whcih is far apst he threshold of perception... but from what you're saying, the data jumps around?  


Since CK is not here yet I will take shot at the answer and say that you have lots of clock cycles left after getting the sensor data on SPi to be able to blast it out over the serial port between sense data-taking.

Code: [Select]

void setup(){
Serial.begin(115200);
}


//sensor function
void SensorDataTakingFunction(){
//some code here
}

//Average the data
void AverageThatData(){
//some more code here
}


void loop(){
SensorDataTakingFunction();   // get slow sensor data
AverageThatData();                // average the accumulated data
Serial.write(sensorData);
}


D


void loop(){
SensorDataTakingFunction();   // get slow sensor data
AverageThatData();                // average the accumulated data
Serial.write(sensorData);
}


since SensorDataTakingFunction();  is slow
and Serial.write(sensorData); needs to be fast

I think he will be doing something like:

void loop(){
SensorDataTakingFunction();   // get slow sensor data
for (int i = 0; i < 10; i++)
{
AverageThatData();                // average the accumulated data
Serial.write(sensorData);
}
}

This way he will only read the sensor every so often and he will continue to send the average between those times.

Of course the better alternative is to do the averaging on the OTHER end of the device that way you get the data as quickly as possible.





Go Up