FRIENDz

FAILURE IS THE SIGN OF SUCCESS!!

Controlling the Speed of Rotation Servos

You can use similar code for continuous rotation and normal servos, but be aware that continuous rotation servos may not stop rotating when writing exactly 90 degrees. Some servos have a small potentiometer you can trim to adjust for this, or you can add or subtract a few degrees to stop the servo. For example, if the left servo stops rotating at 92 degrees, you can change the lines that write to the servos as follows:
myservoLeft.write(angle+TRIM); // declare int TRIM=2; at beginning of sketch.


Continuous rotation servos are a form of gear reduced motor with forward and backward speed adjustment. Control of continuous rotation servos is similar to normal servos. The servo rotates in one direction as the angle is increased from 90 degrees; it rotates in the other direction when the angle is decreased from 90
degrees. The actual direction forward or backward depends on how you have the servos attached. This example sweeps the servos from 90 to 180 degrees, so if the servos were connected to wheels, the vehicle would move forward at a slowly increasing pace and then slow down to a stop. Because the servo control code is in loop, this will continue for as long as there is power:


#include <Servo.h>
Servo myservoLeft; // create servo object to control a servo
Servo myservoRight; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup()
{
myservoLeft.attach(9); // attaches left servo on pin 9 to servo object
myservoRight.attach(10); // attaches right servo on pin 10 to servo object
}
void loop()
{
for(angle = 90; angle < 180; angle += 1) // goes from 90 to 180 degrees
{ // in steps of 1 degree
// 90 degrees is stopped
myservoLeft.write(angle); // rotate servo at speed given by 'angle'
myservoRight.write(180-angle); // go in the opposite direction
delay(20); // waits 20ms between servo commands
}
for(angle = 180; angle >= 90; angle -= 1) // goes from 180 to 90 degrees
{
myservoLeft.write(angle); // rotate at a speed given by 'angle'
myservoRight.write(180-angle); // other servo goes in opposite direction
}
}



No comments:

Post a Comment