Pages: [1]   Go Down
Author Topic: servo problems  (Read 345 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Code:
// 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.

Code:
// 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

Code:
// 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.
« Last Edit: February 05, 2013, 03:05:24 pm by ponobill » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26332
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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

Code:
myservo.attach(8);  // attaches the servo on pin 9 to the servo object
guess which of those statements the compiler believes.
« Last Edit: February 05, 2013, 03:03:46 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

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

Code:
// 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?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26332
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: