Offline
Newbie
Karma: 0
Posts: 23
|
 |
« on: September 01, 2011, 01:42:58 am » |
Hey guys, I'm using the following code to test run my servo http://www.lowpricerc.com/product_info.php?products_id=1032i'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 } }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19001
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: September 01, 2011, 07:04:14 am » |
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); } }
|
|
|
|
« 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.
|
|
|
|
0
Offline
Tesla Member
Karma: 50
Posts: 6530
Arduino rocks
|
 |
« Reply #2 on: September 01, 2011, 11:19:21 am » |
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=""; } }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 23
|
 |
« Reply #3 on: September 01, 2011, 11:38:15 am » |
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
Tesla Member
Karma: 50
Posts: 6530
Arduino rocks
|
 |
« Reply #4 on: September 01, 2011, 01:31:58 pm » |
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
|
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 269
Posts: 25392
Solder is electric glue
|
 |
« Reply #5 on: September 01, 2011, 01:50:08 pm » |
Some continuously rotating servos are one direction only. We need to know what you have.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 23
|
 |
« Reply #6 on: September 01, 2011, 02:02:04 pm » |
the servo i have is http://www.lowpricerc.com/product_info.php?products_id=1032it 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
Brattain Member
Karma: 137
Posts: 19001
I don't think you connected the grounds, Dave.
|
 |
« Reply #7 on: September 01, 2011, 02:03:44 pm » |
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.
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 269
Posts: 25392
Solder is electric glue
|
 |
« Reply #8 on: September 01, 2011, 02:09:06 pm » |
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
Newbie
Karma: 0
Posts: 23
|
 |
« Reply #11 on: September 01, 2011, 07:56:50 pm » |
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
Tesla Member
Karma: 50
Posts: 6530
Arduino rocks
|
 |
« Reply #12 on: September 01, 2011, 11:20:14 pm » |
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
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 23
|
 |
« Reply #13 on: September 02, 2011, 11:27:52 am » |
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
Brattain Member
Karma: 269
Posts: 25392
Solder is electric glue
|
 |
« Reply #14 on: September 02, 2011, 11:30:58 am » |
|
|
|
|
|
Logged
|
|
|
|
|
|