Go Down

Topic: Simultaneous loops (Read 16307 times) previous topic - next topic


Hi everybody.

I have 1 question. Is it possible to run multiple 'for' loops simultaneously?
If it is possible, can anyone please show me how?


No. Not literally. A processor (well Arduino processor) can only do one thing at a time. But it can to things so fast that they look like they are happening at the same time.

How about you tell us what you want to do.

Rob Gray aka the GRAYnomad www.robgray.com


Currently, I'm using a 'for' loop to run 2 servos at the same time. Like this one :

for(ArmPos=ArmPos;ArmPos<150;ArmPos += 1)  //servo move from 0 to 150 degrees
      if( (sensorvalue1 >426) && (sensorvalue2 >426) )
        break; //exit loop if meet the conditions
      else if( sensorvalue1 < 426)
        servo1.write(ArmPos);  // tell servo to go to position in variable 'ArmPos'
      else if( sensorvalue1 < 426)
        servo2.write(ArmPos);  // tell servo to go to position in variable 'ArmPos'

Above sketch runs the servos at the same degree of rotation(DOR) but What I want to do is to run the servos saperately.
what I'm thinking is that when I can run each servo in diff. for loops and possiblely run it simultaneously,
than  I can controll it whether to run the servo at the same DOR or saperately.


If you want to move each servo independently, you don't want/need any loops. The purpose of the loop is to iterate over each servo doing the same thing for each one.
The art of getting good answers lies in asking good questions.


You can take a look at the TimedAction library.
It has an example where it runs ThreeExamplesAtOnce, it does not actually run the tasks in parallel, it just simulates parallelism by alternating calling and waiting as per the defined interval.



Thanks AlphaBeta,

The timedActions were exactly what I was looking for.


Go Up