3630Views4Replies

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
  }
}

Comments

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

Best Answer 10 years ago

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

0
MACKattacksnipe
MACKattacksnipe

Answer 10 years ago

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

0
steveastrouk
steveastrouk

Answer 10 years ago

What does your code do ?

0
MACKattacksnipe
MACKattacksnipe

Answer 10 years ago

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