I'm trying to use a special servo that is capable of 270 degree rotation. And my project requires 0-180 degree rotation. How can I initialize the servo to be -90 degree from the start so it is able to rotate 180 degrees at once?
Please tell me if I need to further clarify my question.
Before you attach the servo, you can set the position. Then, when you attach the servo, it will go to that position.
With a servo that can rotate from 0 to 270, you can't possibly get it to -90. So, you really need to clarify, possibly with pictures, what you want the servo to do.
Servos don't recognise negative positions. If you're using Servo.h then always write to the servo with writeMicroseconds(). You'll need to experiment to work out what values will send your particular servo to the positions you want, no-one else can do that for you.
To set your initial position just write to the servo immediately before the attach() in setup() (or immediately after, there's very little difference).
A typical servo, i.e. one that rotates about 180 degrees, uses a pulse width of 1000 to 2000 uS, repeated every 20mS or thereabouts.
Your 270 degree servo may be set-up to use those same pulse widths, or it may be set-up to use extended timing pulses, 600 to 2400 uS.
Unless you have the spec. sheet for your servos, you will only know by experimentation what to give it for the rotation you need.
Start by giving it 1500 uS pulse width (centre), then keep incrementally reducing it until it starts to complain (you will hear it trying to go beyond the physical stops). Store the uS value that gives maximum rotation in that direction without complaints from the servo.
Do the same from 1500 upwards, again until it complains, and store the uS value for maximum rotation in that direction, again without complaints.
Now you can use the "map" function in your sketch to map degrees angle, to uS value to send.
Map will also invert the signal if you want it to, just swap the low/high values of either pair of the 4 parameters you specify to the map function call.
PaulS:
Before you attach the servo, you can set the position. Then, when you attach the servo, it will go to that position.
With a servo that can rotate from 0 to 270, you can't possibly get it to -90. So, you really need to clarify, possibly with pictures, what you want the servo to do.
Hi Paul,
Thank you for your suggestion. For example, if my servo is at 40 degree before doing anything to it and I set it to 90 degree before attachment command. Will it move 50 degree to 90? Or it will not move and set this position to 90 degree instead.
The communication with a servo is
a) one way, it's only the Arduino sending commands without any feedback
b) it is position based. The Arduino sends an absolute position, not movement.
c) only has a fictive link with degrees. 0 to 180 in the Servo library just maps to 544us to 2400us pulses. How the servo interprets these is up to the servo.
So if you set a position and attach the servo the Arduino will simply tell the servo to move to that absolute position how many degrees that may be.
PaulS:
If you tell the servo to go to position 90, when it is at position 40, it will move 50 degrees. It will NOT set the current position as 0.
Actually it won't move 50 degrees because the servo apparently has a movement range of 0 to 270 degrees according to the OP, but it will move to the position equivalent to 90 (135) and it won't set the current position as zero.
UKHeliBob:
Actually it won't move 50 degrees because the servo apparently has a movement range of 0 to 270 degrees according to the OP, but it will move to the position equivalent to 90 (135) and it won't set the current position as zero.
I assumed (yes, I know what that means) that OP would discover the range of values that cause the servo to move to the limits, and set them, so that servo.write(90) would actually make the servo move 1/3 of its travel.
UKHeliBob:
Actually it won't move 50 degrees because the servo apparently has a movement range of 0 to 270 degrees according to the OP, but it will move to the position equivalent to 90 (135) and it won't set the current position as zero.
Which is why it's much better to use writeMicroseconds() because then you don't fool yourself that the angle in write(angle) actually means an angle.
slipstick:
Which is why it's much better to use writeMicroseconds() because then you don't fool yourself that the angle in write(angle) actually means an angle.
Steve
Or write your own function that does the angle conversion so that you can call it with an angle between 0 and 270 and have the servo move there.
UKHeliBob:
Or write your own function that does the angle conversion so that you can call it with an angle between 0 and 270 and have the servo move there.
You don't need to write such a function, it already exists, and it is called "map".
Let us say you have determined (as I posted above) that 975 uS makes the servo go maximum anticlockwise, and 2105 uS makes it go maximum clockwise....
If the total servo travel is 274 degrees, and you want to set position 90 degrees....
You don't need to write such a function, it already exists, and it is called "map".
Sigh. Yes, I know about map() and it is the right way to convert the values, but what I was proposing was a function with a sensible name that would allow you to wrote a statement such as
positionServo(servoNumber, 135);
and have the nominated servo move to 135 degrees. You could also write it such that the number passed to the function represented the percentage of the full servo travel that you wanted it to move.
positionServo(servoNumber, 50);
A bit of creativity would also allow you to use meaningful names for the servos