3066Views4Replies

Author Options:

Help? with arduino code for controlling 2 servos sweeping at different degrees? Answered

//Sweep

#include "Servo.h"
 
Servo myservo;  // create servo object to control a servo
Servo mysecondservo;              // a maximum of eight servo objects can be created
 
int pos = 0;    // variable to store the servo position

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  mysecondservo.attach(10);
}
 
 
void loop()
{
  for(pos = 0; pos < 140; pos += 1)  // goes from 0 degrees to 140 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 = 0; pos < 140; pos += 1)  // goes from 0 degrees to 140 degrees
  {                                  // in steps of 1 degree
    mysecondservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                   
  }
 
  for(pos = 150; pos>=1; 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 = 150; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  {                               
    mysecondservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);               

  }
}
#include "Servo.h"
Servo mysecondservo;

void loop()

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

4 Replies

user
steveastroukBest Answer (author)2010-06-23

And ? What's wrong with it ? Please don't post code and expect us to decipher its operation. I see two occurences of declaration of servo and servo.h Steve

Select as Best AnswerUndo Best Answer

user

do you know how to use 2 servos to run different sweeps

Select as Best AnswerUndo Best Answer

user

ok nevermind i feel not smart i just figured it out but thanks

Select as Best AnswerUndo Best Answer