Thursday, January 15, 2009

Servo working with Arduino and Motor Shield


I made the servo I took from the lab work with Arduino and the Motor Shield.
I am attaching a video of it working. The code I used is pretty simple, it just goes from 0 to 180 degrees and comes back.
The servo works right with either the USB or the power adapter with no extra power to the motor shield. I don't know what will happen when we put some load into it. 



Also, I found out that when you attach the motor shield into the Arduino, all the input-output pins are in use, so I don't know how we're going to use both the input-ouptuts AND the motors. Anyone knows anything about this?

Here is the code I used:

#include <>

ServoTimer1 servo1;
int pos = 0;     // variable to store the servo position 
 
void setup() 
  servo1.attach(9);   // attaches the servo on pin 9 to the servo object 


void loop() 
  for(pos = 0;pos <>    // goes from 0 degrees to 180 degrees 
  {                                // in steps of 1 degree 
    servo1.write(pos);             // tell servo to go to position in variable '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 
  {                                
    servo1.write(pos);             // tell servo to go to position in variable 'pos' 
    delay(15);                     // waits 15ms for the servo to reach the position 
  } 

You can find the library ServoTimer1.h here.
Extract the rar file and copy the whole ServoTimer1 folder into the Arduino directory \hardware\libraries

Update:
In the first line of the code "#include <>"
You have to type ServoTimer1.h between the <>.
This thing won't let me post it right because it assumes it's code for the blog.


No comments:

Post a Comment