Hi,
This is my first try at using any kind of motor, I wanted to eventually make a simple robot so I bought two of these (so the robot could drive around): "Servo - Generic Full Rotation (Micro Size)" Servo - Generic Continuous Rotation (Micro Size) - ROB-10189 - SparkFun Electronics
It's listed as running on 4.8 - 6 V, and in the comments the sparkfun guy says you can hook it up directly to an arduino, so I did that (hooked it directly to my Arduino Uno):
power to 5v, ground to gnd, and control line to pin 9 on the arduino... I've found the off position of the servo just fine, and it spins fine if only going in one direction. The problem:
If I tell it to spin for a few secs one way, then stop for half a second, then spin for a few secs the other way, then stop, repeat... It does this for a random amount of time fine, but then inevitably either completely stops, or starts spinning full speed in one direction forever.
I can't seem to figure out what I'm doing wrong... I've tried writing the code a few different ways. Here's what I think looks the most correct, but I'm getting that same problem:
#include <Servo.h>
Servo serv1;
int serv1_off = 101;
int serv_speed = 20;
int serv_max_speed = 180;
int serv_pulse_time = 20;
void setup()
{
serv1.attach(9);
}
void loop()
{
serv_speed = constrain(serv_speed, 0, serv_max_speed - serv1_off);
for (int i = 0; i < (2000 / serv_pulse_time); i++)
{
serv1.write(serv1_off + serv_speed);
delay(serv_pulse_time);
}
for (int i = 0; i < (500 / serv_pulse_time); i++)
{
serv1.write(serv1_off);
delay(serv_pulse_time);
}
for (int i = 0; i < (2000 / serv_pulse_time); i++)
{
serv1.write(serv1_off - serv_speed);
delay(serv_pulse_time);
}
for (int i = 0; i < (500 / serv_pulse_time); i++)
{
serv1.write(serv1_off);
delay(serv_pulse_time);
}
}
The problem happens with both servos, so I doubt the servos are defective. My only guess is my code is wrong, or maybe I really need to run these on a separate 6 V supply... but I'm hoping someone with more experience could help me find where the problem lies.