continuous servo refuses to rotate any other direction

Hey guys,

I'm using the following code to test run my servo
http://www.lowpricerc.com/product_info.php?products_id=1032
i'm using servo.h so i figured it would be fairly straightforward. Can someone tell me whats wrong with my code?

im powering it with 5v, the servo takes from 4-6v input

#include <Servo.h> 
/** Adjust these values for your servo and setup, if necessary **/
int servoPin     =  3;    // control pin for servo motor 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 

/** The Arduino will calculate these values for you **/
int moveServo;           // raw user input
int refreshTime  =  20;
long lastPulse   = 0;    // recorded time (ms) of the last pulse
int angle=90;

void setup() {
    myservo.attach(3);  // attaches the servo on pin 9 to the servo object 
  //centerServo = maxPulse - ((maxPulse - minPulse)/2);
 // pulseWidth = centerServo;   // Give the servo a stop command
  Serial.begin(115200);
  Serial.println("Arduino Serial Continuous Rotation Servo Control");
  Serial.println("          by Orfeus for GRobot.gr");
  Serial.println("   Press < or > to move, spacebar to center");
  Serial.println();
}

void loop() {
  // wait for serial input
  if (Serial.available() > 0) {
    // read the incoming byte:
    moveServo = Serial.read();

    // ASCII '<' is 44, ASCII '>' is 46 (comma and period, really)
    if (moveServo == 44) { angle = angle+5; }
    if (moveServo == 46) {  angle = angle-5; }
    if (moveServo == 32) {  angle = 90; }

    // stop servo pulse at min and max
    if (angle > 180) { angle = 180; }
    if (angle< 0) { angle = 0; }
    
     // Show me the keys I pressed
     //Serial.print("Key pressed: ");
     //Serial.println(moveServo);

     //print pulseWidth back to the Serial Monitor (comment to undebug)
     Serial.print("Angle: ");
     Serial.print(angle);
     Serial.println("us");
  }

  // pulse the servo every 20 ms (refreshTime) with current pulseWidth
  // this will hold the servo's rotation and speed till we told it to do something else.
  if (millis() - lastPulse >= refreshTime) {

myservo.write(angle);

    lastPulse = millis();           // save the time of the last pulse
  }
}
if (moveServo == '.') { 
  angle += 5; 
}

Is easier to read, don't you think?

What does your debug output show?

Lose this:

  if (millis() - lastPulse >= refreshTime) {

just do the servo write - refresh is taken care of by the library.

myservo.attach(3);

You've given the pin a nice name - why not use it?

#include <Servo.h> 
const int servoPin =  3;    // control pin for servo motor 
Servo myservo;
int angle = 90;

void setup() 
{
  myservo.attach(servoPin);
  myservo.write(angle);
  Serial.begin(115200);
  Serial.println("Arduino Serial Continuous Rotation Servo Control");
  Serial.println("          by Orfeus for GRobot.gr");
  Serial.println("   Press < or > to move, spacebar to center");
}

void loop() 
{
  if (Serial.available() > 0) {
    int moveServo = Serial.read();

    if (moveServo == ',') { 
    	angle += 5; 
    }
    if (moveServo == '.') {  
    	angle -= 5; 
   	}
    if (moveServo == ' ') {  
    	angle = 90; 
    }

    angle = constrain (angle, 0, 180);
    
    // Show me the keys I pressed
    //Serial.print("Key pressed: ");
    //Serial.println(moveServo);

    Serial.print("Angle: ");
    Serial.print(angle);

    myservo.write(angle);
  }
}

Below is some simple test code you can use to check your continous rotation servo.

// zoomkat 10-4-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// Powering a servo from the arduino usually DOES NOT WORK.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(2000); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control 
  Serial.println("servo-test-21"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(1);  
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    } 
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    int n;
    char carray[6]; //converting string to number
    readString.toCharArray(carray, sizeof(carray));
    n = atoi(carray); 
    myservo.writeMicroseconds(n); // for microseconds
    //myservo.write(n); //for degees 0-180
    readString="";
  } 
}

thanks for the help guys, I'll be sure to run those when I get back. But if it still turns one way then its probably the servo's problem? Any idea what's in my code that could've made it keep turning one way? Could my extra refresh timing be causing it?

thanks

You have provided no specific info on the servo you are using, how it is powered, etc.. I assume it is a continous rotation servo, and if so, did you buy it as such, or is it a DIY one?

Some continuously rotating servos are one direction only. We need to know what you have.

the servo i have is
http://www.lowpricerc.com/product_info.php?products_id=1032

it doesn't have any doc on it, which is quite a pain...

it was one of the few continuous servos I could find too, strangely such things are rare.

strangely such things are rare.

I've never bought one - made plenty though.
Ten, fifteen minutes max.

strangely such things are rare.

Not strange at all, servos are all about positioning, make it a continuously rotating one and you throw most of the functionality away. It is the wrong thing to use, that is there are much better ways of getting the same effect.

http://www.advancenow.com.au/servlet/the-359/360-degree-Servo-Continuous/Detail

looks like my servo should work just like that, so hopefully its just something on my end

Another blurb on that servo, for only $19.90! One probably could easily make this servo from one of the $1.99 servos.

http://www.advancenow.com.au/servlet/the-359/360-degree-Servo-Continuous/Detail

i actually bought mine for $2, so it was a bargain for less work. I just tested both code and Mike's couldnt change the servo direction. Zoomkat's didnt make it move at all.

Is this busted? I have 2 of them and neither of them works.

Is this busted? I have 2 of them and neither of them works.

Often people miss wire the servos, have inadequate power supplys, don't connect the arduino and servo power supply grounds, etc.. Below us a basic servo wiring setup.

I have a usb breakoutboard from sparkfun that powers and serially connects my arduino to the computer. Then I have an external power src of 5v running in my servo. My servo has black, red and white wires so I naturally connect black to ground, red to power and white to arduino. though red is in between black and white, unlike in your diagram.

One thing though is that my servo does not share ground with the arduino, is that a big problem?

is that a big problem?

Yes a big problem.

http://www.thebox.myzen.co.uk/Tutorial/Power_Supplies.html

One thing though is that my servo does not share ground with the arduino, is that a big problem?

Yes.

One thing though is that my servo does not share ground with the arduino, is that a big problem?

Only a problem if you want it to work, it simply won't without a common connection between the arduino ground and external voltage source negative terminal. That is unless you use an opto-isolator between the arduino output pin and the servo control input.

Lefty

wow that would explain it, :blush: i'll try that get back to you guys. hopefully my miswiring didn't mess up the servo

One thing though is that my servo does not share ground with the arduino, is that a big problem?

Yes, the servo won't work properly without the common ground. You may also cause damage to the servo without the proper ground. I've found that without the ground, the small servos will get very hot. As to the wiring order on the servo connector, the +v connector (usually red/orange) is normally the center connector. This prevents possible servo damage if the connector is incorrectly plugged into servo receiver.

connecting the ground worked, but after testing a little bit while, the strangest thing happened, everything just stopped working again. I can hear a low hum from the servo, but that's it.

here's my code:

#include <Servo.h> 
const int servoPin =  3;    // control pin for servo motor 
Servo myservo;
int angle = 90;

void setup() 
{
  myservo.attach(servoPin);
  myservo.write(angle);
  Serial.begin(115200);

}

void loop() 
{
  if (Serial.available() > 0) {
    int moveServo = Serial.read();
   //1-0,2-30,3-60,4-90,5-120,6-150,7-180
    if (moveServo == '1') { 
    	angle =0; 
    }
    if (moveServo == '2') {  
    	angle =30;
   	}
    if (moveServo == '3') {  
    	angle =60;
    }
     if (moveServo == '4') {  
    	angle =94;
    }
     if (moveServo == '5') {  
    	angle =120;
    }
     if (moveServo == '6') {  
    	angle =150;
    }
     if (moveServo == '7') {  
    	angle =180;
    }

    


    Serial.print("Angle: ");
    Serial.print(angle);

    myservo.write(angle);
  }
}

I checked the power src for the servo, and its still 5v no problems, and the connections all seem fine...