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
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?
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.
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.
}
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..
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!
... 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
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!
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.
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
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