Pages: [1] 2   Go Down
Author Topic: continuous servo refuses to rotate any other direction  (Read 1345 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
#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
  }
}

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24462
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if (moveServo == '.') {
  angle += 5;
}
Is easier to read, don't you think?

What does your debug output show?

Lose this:
Code:
 if (millis() - lastPulse >= refreshTime) {
just do the servo write - refresh is taken care of by the library.

Code:
myservo.attach(3);
You've given the pin a nice name - why not use it?

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);
  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);
  }
}
« Last Edit: September 01, 2011, 08:09:02 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Tesla Member
***
Karma: 118
Posts: 8962
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
// 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="";
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Tesla Member
***
Karma: 118
Posts: 8962
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 514
Posts: 31553
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24462
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
strangely such things are rare.
I've never bought one - made plenty though.
Ten, fifteen minutes max.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 514
Posts: 31553
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Tesla Member
***
Karma: 118
Posts: 8962
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Tesla Member
***
Karma: 118
Posts: 8962
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 514
Posts: 31553
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
is that a big problem?

Yes a big problem.

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

Pages: [1] 2   Go Up
Jump to: