74Views1Replies

Author Options:

Moving Three Servos at Once Answered

Hey Guys,

I'm working on a project and I'm trying to figure out what power supply to use.  I have power supply rated for 5V and .7A.  I was going to test this power supply out by making all servos sweep back and forth at once, and see how it performs.  This question is main for the coders out there.  Is the following code correct?  I've had a long work day and I'm about to pass out and I was hoping if someone could critique this real quick.  Thanks in advance!

/* Sweep
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.

modified 8 Nov 2013
by Scott Fitzgerald
http://arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>

Servo myservo01;  // create servo object 
Servo myservo02:
Servo myservo03:

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

void setup()
{
  myservo1.attach(9);  // attaches the servo on pin 9
  myservo2.attach(8);
  myservo3.attach(7);
}

void loop()
{
  for(pos = 0; pos <= 180; pos += 1)     // goes from 0 degrees to 180 degrees
  {                                                                 // in steps of 1 degree
    myservo01.write(pos);                      // tell servo to go to position 'pos'
    delay(15);                                             // waits 15ms for the servo
    myservo02.write(pos);
    delay(15);
    myservo03.write(pos);
    delay(15);


  }
  for(pos = 180; pos>=0; pos-=1)     // goes from 180 degrees to 0 degrees
  {                               
    myservo01.write(pos);            // tell servo to go to position 'pos'
    delay(15);                              // waits 15ms for the servo
    myservo02.write(pos);             
    delay(15);
    myservo03.write(pos);              
    delay(15);
  }
}

Comments

The forums are retiring in 2021 and are now closed for new topics and comments.
0
robo10302
robo10302

5 years ago

Hello friend you made a tiny mistake, you wrote myservo1 instead of myservo01 and didn't put in semicolons before the setup. Here is the corrected code.

#include <Servo.h>

Servo myservo01; // create servo object

Servo myservo02;

Servo myservo03;

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

void setup()

{

myservo01.attach(9); // attaches the servo on pin 9

myservo02.attach(8);

myservo03.attach(7);

}

void loop()

{

for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees

{ // in steps of 1 degree

myservo01.write(pos); // tell servo to go to position 'pos'

delay(15); // waits 15ms for the servo

myservo02.write(pos);

delay(15);

myservo03.write(pos);

delay(15);

}

for(pos = 180; pos>=0; pos-=1) // goes from 180 degrees to 0 degrees

{

myservo01.write(pos); // tell servo to go to position 'pos'

delay(15); // waits 15ms for the servo

myservo02.write(pos);

delay(15);

myservo03.write(pos);

delay(15);

}

}

Hope this helps :)