servo question

I am trying to rebuild a hexa pod I have from a different comtroler (Stamp) and have a basic servo code, which works fine for one servo.

Now here comes the problem if I add a second servo and duplicare the basic servo code (with a second servo object).

whin I run the attached code the 1st servo only steps 1 incriment (it does not go through the entire range of motion) and then the 2nd servo goes through an entire range of movement waits 2 sec and then runs again. servo 1 only steps once rhroughout the run of the program.  As far as I can figure it is falling through to the second "for" condition.

How do I fix this, I want the 1st servo to go through its full range then 2nd servo goes through the full range of movement and then back to 1st servo etc...

 

// Sweep
// by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.
// Pin 9 = data servo 1

// Pin 10 = data servo 2
#include <Servo.h>
 
Servo myservo;  // create servo object to control a servo0
Servo myserv1;  // create servo object to control a servo1
int pos = 0;    // variable to store the servo position

init pos1 = 0;
void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo.attach(10);
}
void loop()
{
for(pos = 0; pos < 145; pos += 1)  // goes from 0 degrees to 180 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
  }
   delay(50);
for(pos = 145; 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
  }
     // set the LED on
  delay(50);              // wait
    for(pos = 0; pos < 145; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myserv1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);    // waits 15ms for the servo to reach the position
  }
   delay(50);
for(pos = 145; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  {                               
    myserv1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  delay(50);              // wait
}

 

UncleMike

Hello. Some problems I

Hello. Some problems I see;

1) You never attach myserv1 in your setup. You attach myservo to both pins. This explains the motion. The first servo twitches when something is initialized to pin 9 but stops right away as myservo is reinitialized to pin 10. myservo then runs your code as normal. I think this is your primary problem. Change to;

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  myserv1.attach(10); // attaches servo 2 on pin 10.
}

2) For future coding practices name your servos myserv0 and myserv1 or myservo0 myservo1. Keep your naming convention consistent, the last character switches between a letter and a number - this could give you headaches in large code blocks.

Cheers,
Gonzik



Good call, thats why a

Good call, thats why a second pair of eyes help. Also I ment to tag them 0 and 1 but used o instead of 0

thanks

UncleMike

**Hi **

Hi 

The problem is in  void setup()

it should be

void setup()
{
  myservo.attach(9); 
  myserv1.attach(10);
}

Hope this helps.

Also theres no need for “init pos1 = 0;” in the code as you have only used the variable “pos” and not “pos1” anywhere.

 

Sorry read your comment

Sorry read your comment after replying

I’m suprised it compiles

I’m suprised it compiles with init, should be int

Yes that should be int and

Yes that should be int and not init.My bad