Arduino – Testing continuous servo

Posted: July 31, 2012 in Arduino

So I bought a couple hxt9g servos, and have modded them for continuous rotation. The reason behind this was I’m still learning a lot when it comes to Arduino. There are Sheilds and H-bridges when using motors, so hacking a servo is an easy way around this from what I’ve been reading.

below code is not mine, but enables you to use the < > keys to control direction and speed.

/*
* Serial Port Continuous Rotation Servo – Movement and Speed Control
* ————–
*
* Alteration of the control interface to use < and > keys
* to slew the servo horn left and right.  Works best with
* the Linux/Mac terminal “screen” program.
*
* Adapted for Continuous Rotation Servo (Parallax)
* by Orfeus (for http://www.grobot.gr)
* v.200910211932
*
* For Serial Port Terminal on Windows I used PuTTY SSH Client
* ( freeware at http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html )
*
*
*
*
* Created 10 December 2007
* copyleft 2007 Brian D. Wendt
* http://principialabs.com/
*
* Adapted from code by Tom Igoe
* http://itp.nyu.edu/physcomp/Labs/Servo
*
*/

/** Adjust these values for your servo and setup, if necessary **/
int servoPin     =  9;    // control pin for servo motor
int minPulse     =  1170;  // maximum servo speed clockwise
int maxPulse     =  1770; // maximum servo speed anticlockwise
int turnRate     =  75;  // servo turn rate increment (larger value, faster rate)
int refreshTime  =  20;   // time (ms) between pulses (50Hz)

/** The Arduino will calculate these values for you **/
int centerServo;         // center servo position
int pulseWidth;          // servo pulse width
int moveServo;           // raw user input
long lastPulse   = 0;    // recorded time (ms) of the last pulse

,
void setup() {
pinMode(servoPin, OUTPUT);  // Set servo pin as an output pin
centerServo = maxPulse – ((maxPulse – minPulse)/2);
pulseWidth = centerServo;   // Give the servo a stop command
Serial.begin(9600);
Serial.println(“Arduino Serial Continuous Rotation Servo Control”);
Serial.println(”          by Orfeus for GRobot.gr”);
Serial.println(”   Press < or > to move, spacebar to center”);
Serial.println();
}

void loop() {
// wait for serial input
if (Serial.available() > 0) {
// read the incoming byte:
moveServo = Serial.read();

// ASCII ‘<‘ is 44, ASCII ‘>’ is 46 (comma and period, really)
if (moveServo == 44) { pulseWidth = pulseWidth + turnRate; }
if (moveServo == 46) { pulseWidth = pulseWidth – turnRate; }
if (moveServo == 32) { pulseWidth = centerServo; }

// stop servo pulse at min and max
if (pulseWidth > maxPulse) { pulseWidth = maxPulse; }
if (pulseWidth < minPulse) { pulseWidth = minPulse; }

// Show me the keys I pressed
//Serial.print(“Key pressed: “);
//Serial.println(moveServo);

//print pulseWidth back to the Serial Monitor (comment to undebug)
Serial.print(“Pulse Width: “);
Serial.print(pulseWidth);
Serial.println(“us”);
}

// pulse the servo every 20 ms (refreshTime) with current pulseWidth
// this will hold the servo’s rotation and speed till we told it to do something else.
if (millis() – lastPulse >= refreshTime) {
digitalWrite(servoPin, HIGH);   // start the pulse
delayMicroseconds(pulseWidth);  // pulse width
digitalWrite(servoPin, LOW);    // stop the pulse
lastPulse = millis();           // save the time of the last pulse
}
}

/* Orfeus 200910212001 */

Leave a comment