How to control multiple servos ( not simunetalously)?

Hello,

I have found this code below, its works fine and was found here: https://docs.arduino.cc/learn/electronics/servo-motors/

But this only controls 1 servo, i need to control 4 servos, but i only have enough power for one servo to move at a time (this is intended). I dont know how to write a code that allows for it to first move one servo, then the next and so on..
Ive also been unable to find any guides online- If you know of one, then a link to it would be much appreciated.
So, just to clarify- i need 4 servos to move, but not at the same time, one after the other. they all need to do the same motion; a sweep from 0 to 180 degrees.

Thanks.

#include <Servo.h>

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

int pos = 0;    // variable to store the servo position

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

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

Why did you start this topic in the Bootloader category of the forum ?

It has been moved to a more relevant category

Please get a basic programming tutorial and look at "arrays"

The simplest, but not the best way to do this, is to repeat the code 3 more times

What would that look like?

You have the code that defines the servos them moves it. Why not try copying the code yourself ?

do you understand what this (code from the loop()) does ?


why is this "intended" ? do you plan to power the Servos from the Arduino board 5V pin?

you'll likely have to look at the attach() and detach() functions as well and look at current draws in your circuit (that we don't have...)

This is my attempt. It makes them all move at once, but they dont make the full rotation, they move around 40 degrees id estimate

#include <Servo.h>

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

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);
  myservo.attach(6); 
  myservo.attach(5); 
  myservo.attach(3);  // attaches the servo on pin 9 to the servo object
}

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

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

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

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

Its intended because it creates a more interesting sequence that way. No, im using 4 AA batteries.

You are clearly a beginner at coding. But common sense should tell you there is something wrong with this line. You have 4 servos attached. How does that line of code identify which servo should move?

The answer is that it doesn't, and that's what common sense should be telling you when you are writing code.

This line specifies which servo to move, but not what position to move it to.

So what you code should be doing, I think, is specifying which servo to move before giving it the position to move to.

Your code doesn't have to specify which servo before every individual move. Only before it's first movement.

to be honest, he said so too

That was in reference to the code in post #1.

I was referring to @survup 's attempt at code to move all of them, in post #8.

I've clarified my post. Cheers @J-M-L

1 Like

Ah I had missed that - my bad. Indeed you were spot on with the remark then.

1 Like

Hello survup

What´s the task of the sketch in real life?

Here is a clue

Servo myservo_1;
Servo myservo_2;
Servo myservo_3; 
Servo myservo_4;

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.