Simultaneous loops

Hi All,

Arduino noob here so apologies for being a simpleton...

How do I run two different loops at the same time?

I am trying to get two servos to turn 180 with a pause time in between each rotation. However, I want the pause time for each servo to be different.

So I need help to understand how to get the two servo loops to run independently of one another. Here's my code...

#include <Servo.h> // Include servo Library

Servo servo00; //setup servo object
Servo servo01;

void setup() {

servo00.attach(0); //set servo to inputs
servo01.attach(1);

servo00.write(0); //set servo start position
servo01.write(0);

}

void loop() {

int goState = 1;

int pause00 = 1000;
int pause01 = 5000;

if (goState = 1) {

delay(pause00);
servo00.write(180);
delay(1000);
servo00.write(0);

}

if (goState = 1) {

delay(pause01);
servo01.write(180);
delay(1500);
servo01.write(0);

}
}

Thanks for all of your help!

Surely you meant

  if (goState == 1) {

The resources to answer your question, and instructions about posting properly, are available in the information threads at the top of the forum.

aarg:
Surely you meant  if (goState == 1) {

goState does equal 1 all the time anyway, so an extra assignment won't change the outcome :wink: (and the compiler likely got rid of all the tests)

Have a look at how the code is organized in Several Things at a Time

Note how each function runs very briefly and returns to loop() so the next one can be called. None of the functions tries to complete a task in one call. And there may be dozens of calls to a function before it is actually time for it to do anything.

...R

You would probably be better using millis() rather than delay and track the time thus you can get the arduino to perform an action at whatever time you would like and control it individually for each servo. If you are using delays then the arduino is not able to process anything else until that delay is up and so could only work on one servo. Make individual functions and call them in loop when the appropriate conditions are met. Then the arduino can check if the appropriate delay time has passed for one servo again and again each time it runs through the loop while also checking for the other servo in the same way.

Your way is linear and slow:
do something (very little time)
wait a while (loads of time)
do something else (very little time)
wait a while(loads of time)

Alt
check if something needs done (milliseconds)
if something needs done do it (milliseconds)
check if something else needs done (milliseconds)
if something else needs done do it (milliseconds)

The second way can loop thousands of times for every loop of the first way. It is fast enough that it appears that 2 things are done at once. It can activate one servo and then carry on checking to see if the other servo needs activated or if the first servo needs deactivated etc

Thanks for all of your responses. I've started using millis() and that seems to do the job! Cheers.