Running two routines simultaneously?

I've got a problem I just can't figure out. The idea for arduino is to listen to serial port and execute function calls based on a single character it receives...however, at the same time I'd like arduino to send sensor data to the computer every 10 seconds but I'm stuck on how to do that.

First I figured I'd just run two different python scripts (one that runs in every 10 seconds sending character 'a' to get the sensor data and the other that is waiting for a character input (other than 'a') and then sending that to arduino. This ofcourse creates a port in use error when it happens to run at the same time.

Are there any good ways to do this? Ideal would be that arduino waits for serial input for ~9 secs and then executes the sensor read and sends the data and goes back to waiting state. But on the other hand, for that ~1 secs it wouldn't be listening to serial port.

You need the Blink Without Delay concept. There should be an example in the IDE. Also check Nick's page

You may try the NilRtos - with it you can run several tasks in "parallel".. First task will wait on the commands from pc and if any it will execute, the second will send the data to pc when required. That is doable..

This is it in a nutshell. All time related variable arae unsigned long.

// declare variable, pinModes, all that stuff
void loop(){
// 10 second check
currentMillis = millis();
elapsedMillis = currentMillis - previousMillis;
if (elapsedMillis>=10seconds){
previousMillis = previousMillis + 10seconds;
// send sensor data
// serial check
if (Serial.available()>0){
startByte =;
switch (startByte){
case 'a':
// whatever
case 'z':
// whatever
} // end switch
} // end serial check
} // end loop

You need to do both things in the one Python script. The Arduino "blink without delay" concept is equally applicable to Python programs on a PC.