Go Down

Topic: Sweep Servo Sketch (Read 847 times) previous topic - next topic

GWS46

How can I change this code to get the servos moving in opposite directions at the same time?

Quote

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


#include <Servo.h>

Servo myservo1;  // create servo object to control a servo
Servo myservo2;  // a maximum of eight servo objects can be created

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

void setup()

  myservo1.attach(9);   // attaches the servo on pin 9 to the servo object
  myservo2.attach(10);  // attaches the servo on pin 10 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
    myservo1.write(pos);
    myservo2.write(pos);             
    delay(15);                       
  } 
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  {                                
    myservo1.write(pos);
    myservo2.write(pos);   
    delay(15);                   
  } 



dxw00d

Code: [Select]
    myservo1.write(pos);
    myservo2.write(180 - pos);             

GWS46

Many thanks, I've spent ages getting it wrong!

dxw00d


Go Up