Author Topic: Basic Programming - HELP ! [Axon Microcontroller]  (Read 1375 times)

0 Members and 1 Guest are viewing this topic.

Offline Hasan999Topic starter

  • Full Member
  • ***
  • Posts: 75
  • Helpful? 1
Basic Programming - HELP ! [Axon Microcontroller]
« 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..

Code: [Select]

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.

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,632
  • Helpful? 169
    • Society of Robots
Re: Basic Programming - HELP ! [Axon Microcontroller]
« Reply #1 on: September 29, 2009, 11:40:26 AM »
change your code to this:

Code: [Select]
while(button_pressed())
    {
    servo(PORTE,4,1200);
    delay_ms(20);
    }

Offline Hasan999Topic starter

  • Full Member
  • ***
  • Posts: 75
  • Helpful? 1
Re: Basic Programming - HELP ! [Axon Microcontroller]
« Reply #2 on: September 29, 2009, 12:15:45 PM »
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.

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,632
  • Helpful? 169
    • Society of Robots
Re: Basic Programming - HELP ! [Axon Microcontroller]
« Reply #3 on: September 29, 2009, 12:21:57 PM »
Quote
(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

Or, if you meant just guess the angle:
Code: [Select]
    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);
        }

 


Get Your Ad Here

data_list