Go Down

Topic: Sweep Servo Sketch (Read 1 time) previous topic - next topic


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


// 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
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees


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


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


Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131