Go Down

#### jdiezlopez

##### Jan 06, 2009, 01:07 am
Hi, I want to do two things at the same time, for example: turn on two servo motors.

Like calculating [ch960] and flashing a led at the same time.

#### halley

#1
##### Jan 06, 2009, 03:10 am
No such thing as a thread, to the microcontroller.  But you could write your own simple task scheduler, and bounce between those tasks according to their timing requirements.

#### dmesser

#2
##### Jan 06, 2009, 07:18 amLast Edit: Jan 06, 2009, 07:23 am by dmesser Reason: 1
Here's a function I came up with to do what halley is talking about.

Code: [Select]
`boolean cycleCheck(unsigned long *lastMillis, unsigned int cycle) {  unsigned long currentMillis = millis();  if(currentMillis - *lastMillis >= cycle)  {    *lastMillis = currentMillis;    return true;  }  else    return false;}`

Basic usage:

For each event you want to deal with, set up an unsigned int for how often it should trigger in ms (cycle), and an unsigned long to keep track of the last millis() reading since the last event (lastMillis). You call the function passing the address of the lastMillis for that event and the cycle for that event. The function returns a true if the event is due for action, and a false if the syscle time hasn't elapsed yet.

Typical usage example (Simple blinking 2 LEDs at different rates)

Code: [Select]
`#define ledPin1 11#define ledPin2 12#define led1Cycle 100U#define led2Cycle 275Uunsigned long led1LastMillis = 0;unsigned long led2LastMillis = 0;boolean led1State = false;boolean led2State = false;boolean cycleCheck(unsigned long *lastMillis, unsigned int cycle) {  unsigned long currentMillis = millis();  if(currentMillis - *lastMillis >= cycle)  {    *lastMillis = currentMillis;    return true;  }  else    return false;}void setup(){  pinMode(ledPin1, OUTPUT);  pinMode(ledPin2, OUTPUT);}void loop(){  if(cycleCheck(&led1LastMillis, led1Cycle))  {    digitalWrite(ledPin1, led1State);    led1State = !led1State;  }  if(cycleCheck(&led2LastMillis, led2Cycle))  {    digitalWrite(ledPin2, led2State);    led2State = !led2State;  }}`

I've found this to work very well, and it keeps the main loop code very clean. You can even make the cycle times variable by making them variables instead of #defines. Then you can change the cycle times during execution. For example, you want to change the blink rate with a potentiometer.

#### MikMo

#3
##### Jan 06, 2009, 07:23 am
That's a very good generalized solution to a common problem.

#### dmesser

#4
##### Jan 06, 2009, 07:25 amLast Edit: Jan 06, 2009, 07:26 am by dmesser Reason: 1
Thanks. Hope it's useful to some... Someday I'll make the function deal with millis() rollover, too....

#### skumlerud

#5
##### Jan 07, 2009, 12:25 pm
Quote
For each event you want to deal with, set up an unsigned int for how often it should trigger in ms (cycle), and an unsigned long to keep track of the last millis() reading since the last event (lastMillis). You call the function passing the address of the lastMillis for that event and the cycle for that event. The function returns a true if the event is due for action, and a false if the syscle time hasn't elapsed yet.

I use a very similar function myself, the difference is that I'm using a struct to hold the last_millis- and interval-values. I'm also using similar functions to poll switches, changes in analog input etc. As you said, it keeps the main loop clean and the code is much easier to follow.

#### dmesser

#6
##### Jan 07, 2009, 06:03 pm
I like the idea of using a struct for the last millis and cycle parameters. Good idea.

#### dcb

#7
##### Jan 07, 2009, 06:26 pm
Maybe have a look at http://www.bdmicro.com/code/threads/ too.

#### jkone27

#8
##### Feb 17, 2010, 04:43 pm
Hello i have to play a sound on a buzzer (so repeat HIGH and LOW on its pins, at a given frequency (the note)) and while i'm doing this, i have to check if to continue or not.

i check from an incoming message from the usb (a midi message), if the message is NoteOff then it has to stop the BUzzer.

maybe i could use this code to deal with that? :-?

#### mem

#9
##### Feb 17, 2010, 08:31 pm
giacomo, have a look at the Arduino tone function introduced in release 0018: http://arduino.cc/en/Reference/Tone

#10
thaaaaaanks!

#### AlphaBeta

#11
##### Feb 22, 2010, 04:48 pm
Could have a look at this next time you need to time some reoccurring actions:
http://www.arduino.cc/playground/Code/TimedAction

#### efficens

#12
##### Apr 09, 2010, 08:51 pm
Hi.

I wrote some lines using structs and 2 task running, one for send display data and other for read keyboard buttons.

Task for display run every 3000 milliseconds
Keyboard task run every 100 milliseconds

Enjoy ...

Bruno

typedef struct Timer
{
unsigned long start;
unsigned long timeout;
};

char TimerExpired ( struct Timer * timer )
{
if ( millis () > timer->start + timer->timeout )
return true;

return false;
}

void TimerStart ( struct Timer * timer )
{
timer->start = millis ( );
}

//Display task running every 3000 milliseconds
Timer timerDisplay = { 0, 3000 };
//Keyboard task running every 100 milliseconds
Timer timerKeyboard = { 0, 100 };

void setup (void)
{
}

void loop (void)
{
if ( TimerExpired ( & timerDisplay ) )
{
TimerStart ( & timerDisplay );
}

if ( TimerExpired ( & timerKeyboard ) )
{
TimerStart ( & timerKeyboard );
}
}

{
}

{
}

#### federico.dami

#13
##### Apr 11, 2010, 08:05 pm
Try this:
http://robotgroup.com.ar/noticias/20091102/duinos-sistema-operativo-multitarea-para-arduino

#### Tchnclfl

#14
##### Jun 29, 2010, 06:11 am
You can you use the Arduino's in-built PWM timers to change the brightness (as you see it) of an LED.

Check out analogWrite().

Go Up