Society of Robots - Robot Forum
Software => Software => Topic started by: Hasan999 on September 29, 2009, 10:34:27 AM
-
Hi... just started programming the servo. [Axon Microcontroller - AVR AtMega640]
Here is my code that works !... but not the way I want it to..
for (int loop=0; loop<25; loop++) { // for Trials
while(!button_pressed());
for (int i=0; i<35; i++) {
servo(PORTE,4,i);
delay_ms(18);
}
}
I want the Servo [HSR-1425CR] at E4 to rotate 360º, but using trial and error method to get closer to 360º (changing "i" and/or "delay_ms" everytime). I got pretty close, with 0 < i < 35 and delay of 18ms (as in the code above), but this is what I get:
Output: (each time I press the button)
1) Rotates about 356º
2) Rotates about 358º
3) Rotates about 360º
4) Rotates about 362º
and so on ! ... it keeps increasing !
Even when I use lesser "i" or "delay", it would begin with 350º or something, but still keeps increasing in each loop.
How can I improve the code to make it efficient.
All I want is my servo to rotate exactly 360º everytime I press the button. (I didn't know it is THAT hard to do so)
Please help !! Thanks a lot.
-
change your code to this:
while(button_pressed())
{
servo(PORTE,4,1200);
delay_ms(20);
}
-
no no no noo..
Mr. Admin :p ... according to your code, the servo keeps on rotating while the button is pressed !.
BUT, this is not what I want.
I want it to rotate "exactly" 360º whenever I tap the button
(how do I make my servo rotate at an exact angle everytime?)
Thanks.
-
(how do I make my servo rotate at an exact angle everytime?)
You'll have to add encoders:
http://www.societyofrobots.com/sensors_encoder.shtml (http://www.societyofrobots.com/sensors_encoder.shtml)
Or, if you meant just guess the angle:
while(!button_pressed())//wait
for(i=0;i<30;i++)//change the number until it reaches about 360 degrees
{
servo(PORTE,4,1200);
delay_ms(20);
}