Pages: [1]   Go Down
Author Topic: Sweep Servo Sketch  (Read 796 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    myservo1.write(pos);
    myservo2.write(180 - pos);             
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You're welcome.
Logged

Pages: [1]   Go Up
Jump to: