Loading...
Pages: [1]   Go Down
Author Topic: Simple R/C servo code  (Read 1238 times)
0 Members and 1 Guest are viewing this topic.
Daniel
Guest
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,

I was poking around looking for some simple code to use for servo motor demos in class, and found a few examples but nothing stripped-down enough for my liking. See below for what I came up with, which is heavily based on Tom Igoe and Jeff Gray's "temporary servo function". Their original code had floating point calculations for the servo pulse time, which took up a lot of code space, so I reduced this to simple integer math. What's below will step a servo from 0 to 180 degrees.

Thanks to Tom and Jeff...

Daniel
__________________

/*Servo Motor Demo
 * ------------
 * Rotates servo through 180 degrees, using "servoPulse" function
 * adapted from "Temporary Servo Function"  by Tom Igoe and Jeff Gray
 */


int servoPin = 9;            //  R/C  Servo connected to digital pin 13
int myAngle;                 // angle of the servo (roughly in degrees) 0 - 180
int pulseWidth;              // function variable




void setup()
{
  pinMode(9, OUTPUT);      // sets pin D9 as output
}



void servoPulse(int servoPin, int myAngle)     // servo function
{                                              // this is a function for determining our pulsewidth for the servo
  pulseWidth = (myAngle * 6) + 320;            // this determines our delay below (for a standard pot)
  digitalWrite(servoPin, HIGH);                // set servo high
  delayMicroseconds(pulseWidth);               // wait a very small amount (determined by pulsewidth)
  digitalWrite(servoPin, LOW);                 // set servo low
  delay(20);                                   // refresh cycle of typical servos (20 ms)
}




void loop()
{
  for (myAngle=0; myAngle<=180; myAngle++) {  // cycle through every angle (rotate the servo 180 slowly)
    servoPulse(servoPin, myAngle);
  }
}


Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 91
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I experimented with the following code. I want to move the servo around the 90° position like this:
0 - 90 - 1 - 90 - 2 - 90 - ... - 88 - 90 - 89 - 90 - 91 - 90 - 92 - 90 - 93 - ... - 178 - 90 - 179 - 90 - 180 - ...
Unfortunately the servo doesn't move all the way, its movements take place about 20° left and right of the 90° position.

How do I have to change the code that the servo will move the full 180 degrees? I assume it needs more time to move more than 20 or so degrees to the left or right from its middle position, and it needs to be sent the desired position more than once.

Anyone got any ideas how to do this?


[code]
...

void loop() {
    for (myAngle=0; myAngle<=180; myAngle++) {  
    servoPulse(servoPin, myAngle);
    delay(100);
    servoPulse(servoPin, 90);
    delay(100);

  }
  delay(1000);
}
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 91
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It works better now. I simply sent the desired positions 4 times, and now the servo moves from approx. 45°, but not the desired full 90 degrees around its middle position.
Is there a better way? Like leaving out the delays and calling the servoPulse routine 10 times?

Code:
void loop() {
  // cycle through every angle (rotate the servo 180 slowly)
  for (myAngle=0; myAngle<=180; myAngle++) {  
    servoPulse(servoPin, myAngle);
    delay(100);
    servoPulse(servoPin, myAngle);
    delay(100);
    servoPulse(servoPin, myAngle);
    delay(100);
    servoPulse(servoPin, myAngle);
    delay(100);
    servoPulse(servoPin, 90);
    delay(100);
    servoPulse(servoPin, 90);
    delay(100);
    servoPulse(servoPin, 90);
    delay(100);
    servoPulse(servoPin, 90);
    delay(100);
  }
  delay(1000);
}
Logged

Daniel
Guest
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

I don't have the servos handy at the moment to test your code, but you might want to try adjusting the [glow]+ 320[/glow] constant in the equation. When I adapted the code from floating point, some accuracy was lost.. it might be in there, not sure. The statement in your first post "its movements take place about 20° left and right of the 90° position." makes me think that the value 320, which effectively establishes the center point, might be wrong. Plus, I was never good at that math stuff, eh.

Daniel
« Last Edit: November 01, 2006, 03:14:25 am by Daniel » Logged

Pages: [1]   Go Up
Print
 
Jump to: