Makin’ those servos work!

I’ve kind of taken a break with pyton and pygame, and I’ve started doing things with the Arduino board. So I now own an Arduino Diecimila board. So, of course, I did many of the arduino tutorials on the arduino website.

I did that until I thought I had  basic understanding of Arduino. So I moved on to a slightly bigger project. I was going to build a simple robot using the Arduino.

To start I took the main part of my Parallax Boe 2 off of the body part, so I had something to build with. And, as most robots need SOMETHING to do I hooked up the servos used for the wheels to my Arduino/solderless breadboard using the following schematic:

 

For those who don’t know: Vdd is power, Vss is the ground, and obviously the I/O pin is what gets conneted to the Arduino boards so that the servo actually knows what to do.

After a long afternoon of exploring PWM (Pulse Width Modulation) and how to use it, we tried to actually get the sevos moving. As with most projects I do, it didn’t work at first. So after extensive research online I found this code:

int servoPin = 7;
void setup()
{
pinMode(servoPin,OUTPUT);
}
void loop()
{
int temp;
for (temp = 0; temp <= 200; temp++)
{
digitalWrite(servoPin,HIGH);
delayMicroseconds(1500); // 1.5ms
digitalWrite(servoPin,LOW);
delay(20); // 20ms
}
for (temp = 0; temp <= 200; temp++)
{
digitalWrite(servoPin,HIGH);
delayMicroseconds(1800); // 1.8ms
digitalWrite(servoPin,LOW);
delay(20); // 20ms
}
for (temp = 0; temp <= 200; temp++)
{
digitalWrite(servoPin,HIGH);
delayMicroseconds(1200); // 1.2ms
digitalWrite(servoPin,LOW);
delay(20); // 20ms
}
}

Basically, what this code was doing was using PWM in a “braindead way”. It made The servos soin in one direction until the time got slow enough that it stopped and started spinning in the opposite direction. “Wow. That seems a bit too tedious to be the real way to control the servos,” I thought. Research. Research. Research.

After all of that research and stuff, I found one of the weird things the guy who wrote the previous code did. He was using digitalWrite. It is much easier to us analogWrite, so that you can use a number value instead of just LOW/HIGH. That’s important because the way that the servos work is based on the PWM coming from the Arduino board. So if It’s at either extremes, the servos won’t move very fast, if at all. That’s why, in the following code, the analogWrite sets the wheels to a space in between HIGH and LOW.

#define CLOCK 52
#define COUNTER 200
#define NOTMOVING 128
#define RIGHT 6  //sets pin 6 to the right wheel
#define LEFT 5  //sets pin 5 to the left wheel

void forward() {  //the “function” used to refer to moving the servos forward
  analogWrite(RIGHT, CLOCK);
  analogWrite(LEFT, COUNTER); 
}
void stopped() {  //the “function” used to refer to the servos being stopped
  analogWrite(RIGHT, NOTMOVING);
  analogWrite(LEFT, NOTMOVING);
}
void backwards() { //the “function” used to refer to the servos
  analogWrite(RIGHT, COUNTER);
  analogWrite(LEFT, CLOCK);
}
void setup() {
  pinMode(RIGHT, OUTPUT);  //sets pin 7 as an output
  pinMode(LEFT, OUTPUT);  //sets pin 6 as an output
}
void loop() {
  forward();
}

This code, as it is, will just make the little BOE move forward. However, I added the function of moving backward, and staying still. So all you would have to do to make it move backward or stay still is add it it to the loop. So as soon as I get around to doing other stuff to the robot, like adding sensore, I can still tell it which ways it should move easily. :D

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.