I'm trying to help a buddy with an motorized iron man faceplate using a few programmable micro servos. I'm having issues with my server using the .write function without using a for loop to move the servos gears.
I have seen a lot of tutorial and sample code that for examples that just use .move.
Example:
myservo.write(180);
My servo doesn't move at all.
From sweep example that does works.
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
Robin2:
I am not aware of a Servo.move() function. Only Servo.write() and Servo.writeMicroseconds().
And I don't understand your problem. The only reason for the FOR loop in the snippet that you posted is to control the speed at which the servo moves.
If you are happy for the servo to go at full speed just use Servo.write(your-destination-angle);
...R
Sorry, updated the functions names.
I'm trying to run this simple code and the servo doesn't move.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo.write(180); // move the servo to 180 degrees.
delay(20);
}
Robin2:
How are you powering the servo? It should NOT be powered by the Arduino.
...R
Powered via an AA battery back totaling to 9v, both battery pack and Arduino are grounded to the breadboard.
Strange thing, I took a look at the Servo.h lib and noticed that attach function had an overload that takes in min and max range. when I attach with a min and max number the write method now moves sorta as expected.
myservo.attach(9, 1, 180);
Just visually watching the servos they don't appear to be doing a complete 180 (looks like its about 30 degrees shy).
Could this behavior be attributed to these hitec micro servos are programmable? I don't have a programmer so they are using the default values.
Try this test sketch, start out moving 1 degree at a time, then 2, 3, etc. and see if servo fails at higher moves.
/*
Try this test sketch with the Servo library to see how your
servo responds to different settings, type a position
(0 to 180) or if you type a number greater than 200 it will be
interpreted as microseconds(544 to 2400), in the top of serial
monitor and hit [ENTER], start at 90 (or 1472) and work your
way toward zero (544) 5 degrees (or 50 micros) at a time, then
toward 180 (2400).
*/
#include <Servo.h>
Servo servo;
void setup() {
// initialize serial:
Serial.begin(9600); //set serial monitor baud rate to match
servo.write(90);
servo.attach(9);
prntIt();
}
void loop() {
// if there's any serial available, read it:
while (Serial.available() > 0) {
// look for the next valid integer in the incoming serial stream:
int pos = Serial.parseInt();
pos = constrain(pos, 0, 2400);
servo.write(pos);
prntIt();
}
}
void prntIt()
{
Serial.print(" degrees = ");
Serial.print(servo.read());
Serial.print("\t");
Serial.print("microseconds = ");
Serial.println(servo.readMicroseconds());
}