Multiple actions at the same time

Hi,

i just started programming and I would like to have some help with performing several actions at the same time.

I found a code online and modified it a bit:

#include <Servo.h>

Servo myservo1;
Servo myservo2;// create servo object to control a servo
// twelve servo objects can be created on most boards

int pos1 = 0;    // variable to store the servo position
int pos2 = 90;

void setup() {
  myservo1.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo2.attach(10);
}

void loop() {
  
  for (pos1 = 0; pos1 <= 90; pos1 += 1)  { // goes from 0 degrees to 180 degrees  // in steps of 1 degree
    myservo1.write(pos1);              // tell servo to go to position in variable 'pos'              
    delay(5);                        // waits 15ms for the servo to reach the position
  }
  

  for (pos2 = 90; pos2 >= 0; pos2 -= 1) { // goes from 180 degrees to 0 degrees
    myservo2.write(pos2);            // tell servo to go to position in variable 'pos'
    delay(5);                       // waits 15ms for the servo to reach the position
  }


  for (pos1 = 90; pos1 >= 0; pos1 -= 1) { // goes from 180 degrees to 0 degrees
    myservo1.write(pos1);              // tell servo to go to position in variable 'pos'
    delay(5);                       // waits 15ms for the servo to reach the position
  }


   for (pos2 = 0; pos2 <= 90; pos2 += 1) { // goes from 180 degrees to 0 degrees              
    myservo2.write(pos2);            // tell servo to go to position in variable 'pos'
    delay(5);                       // waits 15ms for the servo to reach the position
  }
  

  
}

My problem is that the program is performing every action one by one, but i would like to perform the two first action followed by Nr3 and 4.

Thanks in advance! :slight_smile:

One way to move 2 servos at the same time for your purposes in this program

  for (pos1 = 0; pos1 <= 90; pos1 += 1)  { // goes from 0 degrees to 90 degrees  // in steps of 1 degree
    myservo1.write(pos1);              // tell servo to go to position in variable 'pos1'              
    myservo2.write(90 - pos1);              // tell servo to go to position at the other end of travel              
    delay(5);                        // waits 15ms for the servos to reach position
  }

NOTE that there are better, more generally applicable ways of doing this but keep it simple if this is all you want to do in the program.

I have also corrected the comments in the program.

Uncompiled, untested, incorrect comments removed

void loop() {
 
  for (int pos = 0; pos <= 90; pos++)  {
    myservo1.write(pos);
    myservo2.write(90 - pos);
    delay(5);             
  }

  for (int pos = 90; pos >= 0; pos --) {
    myservo1.write(pos);
    myservo2.write(90 - pos);
    delay(5);           
  }
}

Thank for the help guys! The second answer was what i needed :slight_smile:

simgra:
Thank for the help guys! The second answer was what i needed :slight_smile:

Err, they're the same answer.

AWOL:
Err, they're the same answer.

No, not exactly. On the second one I can decide the delay on both ways.

UKHeliBob:
I have also corrected the comments in the program.

Close, but no cigar.

They really are the same answer, it's just that UKHeliBob left the return as an exercise for the reader.

AWOL:
Close, but no cigar.

What's a few milliseconds here and there :slight_smile: ?

Well, thanks anyways :slight_smile: