0
Offline
Newbie
Karma: 0
Posts: 6
Arduino rocks
|
 |
« on: July 25, 2008, 08:25:27 pm » |
Folks I am a newbie to the arduino like mcu world... just getting started with all this...
i am having a lot of trouble just getting one servo to work with arduino... would really appreciate it if anyone can shed some light as to what i am doing wrong here... the servo seems to twitch once in a while ... when the code runs.. but nothing else.. i am using the servo library
my setup is the basic arduino USB board from sparkfun.com JR Servo DS285
Connected to Pin 9 on arduino Power source is from the +5 onboard arduino the arduino is connected to my laptop via USB
#include <Servo.h>
Servo servo1;
void setup() { pinMode(9,OUTPUT); Serial.begin(19200); servo1.attach(9); servo1.setMaximumPulse(2000); servo1.setMinimumPulse(700); Serial.print("Servo1 Ready\n");
}
void loop() { Serial.print("Servo 1 position 70\n"); servo1.write(70); delay(1500); Serial.print("Servo 1 position 100\n"); servo1.write(100); delay(1500); Servo::refresh(); Serial.print("\n"); delay(3000); }
|
|
|
|
|
Logged
|
|
|
|
|
United States (Texas)
Offline
God Member
Karma: 2
Posts: 525
Arduino socks
|
 |
« Reply #1 on: July 25, 2008, 08:29:34 pm » |
try this
#include <Servo.h> Servo servo1; void setup() { pinMode(9,OUTPUT); Serial.begin(19200); servo1.attach(9); servo1.setMaximumPulse(2000); servo1.setMinimumPulse(700); Serial.print("Servo1 Ready\n"); } void loop() { Serial.print("Servo 1 position 70\n"); servo1.write(70); Servo::refresh(); delay(1500); Serial.print("Servo 1 position 100\n"); servo1.write(100); Servo::refresh(); delay(1500); Servo::refresh(); Serial.print("\n"); delay(3000); }
|
|
|
|
« Last Edit: July 25, 2008, 08:30:03 pm by weirdo557 »
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 6
Arduino rocks
|
 |
« Reply #2 on: July 25, 2008, 08:48:36 pm » |
thank you so much, that definitly did get it to start moving... i am trying to make it go from 0 degrees to 180 degrees.. or left to right so i can mount a sensor on it...
i tried updating the code as follows but seems to slow the movement down a lot... do you think it is some sort of a timing issue?
#include <Servo.h> Servo servo1; void setup() { pinMode(9,OUTPUT); Serial.begin(19200); servo1.attach(9); servo1.setMaximumPulse(2000); servo1.setMinimumPulse(700); Serial.print("Servo1 Ready\n"); } void loop() { Serial.print("Servo 1 position 0\n"); servo1.write(0); Servo::refresh(); delay(1500); Serial.print("Servo 1 position 180\n"); servo1.write(180); Servo::refresh(); delay(1500); Servo::refresh(); Serial.print("\n"); delay(3000); }
|
|
|
|
|
Logged
|
|
|
|
|
United States (Texas)
Offline
God Member
Karma: 2
Posts: 525
Arduino socks
|
 |
« Reply #3 on: July 25, 2008, 09:06:34 pm » |
perhaps your servo is just slow, can you post the model?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 6
Arduino rocks
|
 |
« Reply #4 on: July 25, 2008, 09:16:03 pm » |
yes it is JR Servo DS 285... i have seen them used on align trex 450 rc helicopters
|
|
|
|
|
Logged
|
|
|
|
|
United States (Texas)
Offline
God Member
Karma: 2
Posts: 525
Arduino socks
|
 |
« Reply #5 on: July 25, 2008, 11:58:04 pm » |
hmm, should take about half a second to swing 180 degrees. try commenting out the max and min pulse values just to see what it does. worked with my generic parralax servos.
|
|
|
|
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #6 on: July 26, 2008, 01:59:29 am » |
The software servo library requires refresh() to be called at least once every 50ms (although most servos perform better with a 20ms refresh rate). In the sketch posted above each servo is only refreshed every six seconds. Here is on example of how you can code what you want:
void myRefresh(int delay){ for(int i=0; i < delay/20; i++){ //delay is the total ms delay we want, 20 is the delay per iteration of the loop Servo.refresh(); delay(20); } }
void loop() {
Serial.print("Servo 1 position 0\n"); servo1.write(0); myRefresh(1500);
Serial.print("Servo 1 position 180\n"); servo1.write(180); myRefresh(1500);
Serial.print("3 second delay\n"); myRefresh(3000); // refresh should be called even if the servo is not moving. }
|
|
|
|
« Last Edit: July 26, 2008, 02:01:36 am by mem »
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 6
Arduino rocks
|
 |
« Reply #7 on: July 26, 2008, 05:44:43 pm » |
everyone thank you for your help... the following simple code seems to work just fine on an analog type servo... i am using an EFLRS60 from eflite... the other JR DS285 i beleive is a digital servo and only works if i use digitalwrite... not sure why that's the case though... any ways just thought i would share my findings..
#include <Servo.h>
Servo servo1;
void setup() { Serial.begin(19200); servo1.attach(9); Serial.print("Servo1 Ready\n");
}
void loop() { Serial.print("Servo 1 position 0\n"); servo1.write(0); Servo::refresh(); delay(1000); Serial.print("Servo 1 position 90\n"); servo1.write(90); Servo::refresh(); delay(1000); Serial.print("Servo 1 position 180\n"); servo1.write(178); Servo::refresh(); delay(1000); }
|
|
|
|
|
Logged
|
|
|
|
|
Central Florida, USA
Offline
Jr. Member
Karma: 0
Posts: 59
Arduino rocks
|
 |
« Reply #8 on: July 26, 2008, 10:15:19 pm » |
A working design is not always a good design.
Read and implement per Mem's suggestion. Using delay() in a servo program is playing with fire. You must keep refresh() called frequently. A servo normally has to compare its position with the commanded position. If you go away for a second, it has nothing to compare to and stops moving!
Analog servo? Digital servo? Smoke & mirrors!
|
|
|
|
« Last Edit: July 26, 2008, 10:38:05 pm by kBit »
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #9 on: July 27, 2008, 12:42:59 am » |
... the following simple code seems to work just fine on an analog type servo... any ways just thought i would share my findings..
dsyed1, I appreciate you trying to be helpful but your findings are a bad example of how to use the Software Servo library. As stated in the post above yours, the servo library you are using is designed to expect refresh to be called at least every 50ms and ideally at least every 20ms. Your code calls it once every second. So although you may observe the servos to move, they will not perform within their specifications. That may not matter in your application but for anyone coming to this thread for advice on basic servo interfacing, if you use the Software Servo library then your code should call refresh at least every 50ms. If you want your servo to operate at its rated speed and torque then call refresh every 20ms or so. If its inconvenient to call refresh at the recommended intervals then you may want to look at using one of the servo libraries that use a timer interrupt to do the refresh
|
|
|
|
« Last Edit: July 27, 2008, 12:46:40 am by mem »
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 6
Arduino rocks
|
 |
« Reply #10 on: July 27, 2008, 05:54:50 pm » |
Mem and Kbit you two are absolutely right, I am completely new to this world so just trying to find my way through things.. thanks again for your help!
|
|
|
|
|
Logged
|
|
|
|
|
Shepparton Vic
Offline
Newbie
Karma: 0
Posts: 10
Arduino rocks
|
 |
« Reply #11 on: July 28, 2008, 08:38:30 pm » |
just a thought..I'm new to this to. I have been using the ServoTimer1 lib with no problems. It carries out all timing in hardware so you don't have to worry about refresh. Only downside is you can only use two servos on pins 9 &10.
|
|
|
|
|
Logged
|
|
|
|
|
Central Florida, USA
Offline
Jr. Member
Karma: 0
Posts: 59
Arduino rocks
|
 |
« Reply #12 on: July 30, 2008, 08:51:35 pm » |
just a thought..I'm new to this to. I have been using the ServoTimer1 lib with no problems. It carries out all timing in hardware so you don't have to worry about refresh. Only downside is you can only use two servos on pins 9 &10. Yeah, you pays your money and you takes your choice. Servo1: 2 servos, less worries Servo: many servos, more worries
|
|
|
|
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #13 on: July 30, 2008, 10:30:09 pm » |
just a thought..I'm new to this to. I have been using the ServoTimer1 lib with no problems. It carries out all timing in hardware so you don't have to worry about refresh. Only downside is you can only use two servos on pins 9 &10. Yeah, you pays your money and you takes your choice. Servo1: 2 servos, less worries Servo: many servos, more worries Another option is to use the code posted here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1204020386/74#74 for driving up to eight servos in background using timer2.
|
|
|
|
|
Logged
|
|
|
|
|
|