servo problems

I’m a noob, but I looked around the archives for an answer to this and couldn’t find anything. I’m working on a little servo control problem and was making good progress when everything went a bit wrong. To test the input I added an LED, and when I did my servo went whacky. I backtracked a bunch and modified the demo program “sweep” to try to figure out what’s going wrong.

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


#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 
#define LED 9     // the pin for the LED 
#define BUTTON 11  // input pin of the pushbutton
int val = 0;      // stores the state of the input pin
int old_val = 0;  // stores the previous value of "val" 
int state = 0;    // 0 = LED off while 1 = LED on
int brightness = 130;        // Stores the brightness value
unsigned long startTime = 0; // when did we begin pressing?

void setup() 
{ 
  myservo.attach(8);  // attaches the servo on pin 9 to the servo object 
  pinMode(LED, OUTPUT);   // tell Arduino LED is an output 
  pinMode(BUTTON, INPUT); // and BUTTON1 is an input  
} 
 
 
void loop() 
{ 
  for(pos = 0; pos < 180; 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' 
    //analogWrite(LED, pos);   //*****************************************
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);    // tell servo to go to position in variable 'pos' 
   //analogWrite(LED, pos);   //******************************
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
}

With the LED write statements commented out the servo operates smoothly.

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


#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 
#define LED 9     // the pin for the LED 
#define BUTTON 11  // input pin of the pushbutton
int val = 0;      // stores the state of the input pin
int old_val = 0;  // stores the previous value of "val" 
int state = 0;    // 0 = LED off while 1 = LED on
int brightness = 130;        // Stores the brightness value
unsigned long startTime = 0; // when did we begin pressing?

void setup() 
{ 
  //myservo.attach(8);  // attaches the servo on pin 9 to the servo object   ************************************
  pinMode(LED, OUTPUT);   // tell Arduino LED is an output 
  pinMode(BUTTON, INPUT); // and BUTTON1 is an input  
} 
 
 
void loop() 
{ 
  for(pos = 0; pos < 180; 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' 
    analogWrite(LED, pos);    // ****************************************
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);    // tell servo to go to position in variable 'pos' 
   analogWrite(LED, pos);  //************************************
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
}

With the servo attachment commented out and the LED write uncommented, it cycles the LED up and down as you would expect

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


#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 
#define LED 9     // the pin for the LED 
#define BUTTON 11  // input pin of the pushbutton
int val = 0;      // stores the state of the input pin
int old_val = 0;  // stores the previous value of "val" 
int state = 0;    // 0 = LED off while 1 = LED on
int brightness = 130;        // Stores the brightness value
unsigned long startTime = 0; // when did we begin pressing?

void setup() 
{ 
  myservo.attach(8);  // attaches the servo on pin 9 to the servo object ***************************
  pinMode(LED, OUTPUT);   // tell Arduino LED is an output 
  pinMode(BUTTON, INPUT); // and BUTTON1 is an input  
} 
 
 
void loop() 
{ 
  for(pos = 0; pos < 180; 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' 
    analogWrite(LED, pos);  //**********************
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);    // tell servo to go to position in variable 'pos' 
    analogWrite(LED, pos);  //************************
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
}

with both the LED write uncommented and the servo attached, the LED does not light and the servo moves jerkily. I thought it might be too much current draw, but I connected an external battery (four nicads) to the breadboard bus and no joy.

What gives. And how can I fix this.

On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10,

From the Servo library reference page. Please use code tags when posting code, or disable smileys

myservo.attach(8);  // attaches the servo on pin 9 to the servo object guess which of those statements the compiler believes.

Thanks, that go me on the right path. Lots to learn. this code worked as expected, time for a little more reading and another attempt at the real thing

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


#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 
#define LED 6     // the pin for the LED 
#define BUTTON 11  // input pin of the pushbutton
int val = 0;      // stores the state of the input pin
int old_val = 0;  // stores the previous value of "val" 
int state = 0;    // 0 = LED off while 1 = LED on
int brightness = 130;        // Stores the brightness value
unsigned long startTime = 0; // when did we begin pressing?

void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  pinMode(LED, OUTPUT);   // tell Arduino LED is an output 
  pinMode(BUTTON, INPUT); // and BUTTON1 is an input  
} 
 
 
void loop() 
{ 
  for(pos = 0; pos < 180; 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' 
    analogWrite(LED, pos);
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);    // tell servo to go to position in variable 'pos' 
    analogWrite(LED, pos);
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
}

Any idea why “copy code for forum” in the IDE doesn’t seem to work?

Any idea why "copy code for forum" in the IDE doesn't seem to work?

I don't know, but I never use it, and don't like it.

ponobill: Any idea why "copy code for forum" in the IDE doesn't seem to work?

Because that Arduino IDE feature is not suitable for the Arduino forum. As with many other aspects of the IDE, I wonder what they had in mind when they implemented that 'feature'. But hey, it was free.