Well now that finals are done with I can get back to blogging and working on my pet projects.
I blogged earlier about my first robotic project: the air freshener. After some ebay action (win some, lose some) I finally got a servo for about 5 dollars. It’s a fairly simple robot so the servo was the only thing I needed.
I did a little wiring and since I am such a newbie looked had to get a little help know which wires were what. Society Of Robots is a good resource for newbies like me
So after wiring to the Arduino some programming needed to be dealt with. I found some servo code here. Great code with a bunch of comments. Perfect for me. Whalala copy, paste, and uploaded to the Arduino. To talk to the Arduino I used the screen to connect via USB.
At first it didn’t work. So I had to tweak the original code to disregard the left and right arrow keys and just used “u” and “d” to increment/decrement the pulse width sent to the server. Or to change to angle of the servo.
int servoPin = 6; // control pin for servo motor
int minPulse = 540; // minimum servo position
int maxPulse = 2370; // maximum servo position
int turnRate = 100; // 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
int intTest;
void setup() {
pinMode(servoPin, OUTPUT); // Set servo pin as an output pin
centerServo = maxPulse - ((maxPulse - minPulse)/2);
pulseWidth = centerServo; // Give the servo a starting point (or it floats)
Serial.begin(9600);
Serial.println(” Arduino Serial Servo Control”);
Serial.println(”Press to move, spacebar to center”);
Serial.println();
}
void loop()
{
// wait for serial input
if (Serial.available() > 0)
{
// read the incoming byte:
moveServo = Serial.read();
if(moveServo == ‘u’)
{
pulseWidth = pulseWidth + 10;
Serial.println(”Increasing Width”);
Serial.println(pulseWidth);
}
if(moveServo == ‘d’)
{
pulseWidth = pulseWidth - 10;
Serial.println(”Decreasing Width”);
Serial.println(pulseWidth);
}
if(pulseWidth < minPulse)
{
pulseWidth = minPulse;
}
if(pulseWidth > maxPulse)
{
pulseWidth = maxPulse;
}
}
// pulse the servo every 20 ms (refreshTime) with current pulseWidth
// this will hold the servo’s position if unchanged, or move it if changed
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
}
}
This also was a good way to find out the min and max pulse of the servo I got. Oh also side note if anyone happens to buy a AE A1903 servo the above settings are perfect.









Entries (RSS)