### Author Topic: Axon Microcontroller - Programming a Servo (WTH?)  (Read 1955 times)

0 Members and 1 Guest are viewing this topic.

#### Hasan999

• Full Member
• Posts: 75
##### Axon Microcontroller - Programming a Servo (WTH?)
« on: December 19, 2009, 09:21:23 AM »
Ok fine, I'm a beginner, but seriously, WTH ?... why is it not as simple as  Servo(PORTA,1,90)  <--where Servo at A1 turns 90 degrees.

I don't really know what is Duty cycle, PWM etc etc... [I bought Axon Microcontroller just because it said, "extensive source code and tutorials have been developed to make it easy enough for a beginner" - so I thought I would not need to understand the theories]

I know C programming, but this is getting too confusing just to program a servo to turn exactly 90 degrees everytime.

Can someone PLEASE show me a (working) sample program to rotate a Servo at A1, 90 degrees clockwise and then 90 degrees anti-clw with a delay of 1 second in between ?

Thanks a lot !!

• Supreme Robot
• Posts: 11,680
##### Re: Axon Microcontroller - Programming a Servo (WTH?)
« Reply #1 on: December 19, 2009, 10:10:49 AM »
Well, you will always need to understand the theory.

http://www.societyofrobots.com/axon/axon_function_list.shtml#hardware_defines
http://www.societyofrobots.com/axon/axon_function_list.shtml#servo_centering
http://www.societyofrobots.com/actuators_servos.shtml

Every servo is different, so there is no universal command to make a servo go to an exact angle. You send a square wave, and the servo moves to a particular angle. You'll have to experiment to find with square wave pulse width gives the angle you want.

Here is some sample code close to what you want:

Code: [Select]
`//PWM a pulsefor(uint16_t i=0;i<65500;i++){servo(PORTH,2,900);delay_ms(20);}//delay for 1sdelay_ms(1000);//PWM another pulsefor(uint16_t i=0;i<65500;i++){servo(PORTH,2,300);delay_ms(20);}`

#### Hasan999

• Full Member
• Posts: 75
##### Re: Axon Microcontroller - Programming a Servo (FTW)
« Reply #2 on: December 21, 2009, 08:09:52 AM »

I still don't understand the theories lol  ... but Thanks for the sample code...

I used "Trial and Error" method to:

Make Servo Center
Rotate Servo 90º Left
Servo Center
Rotate Servo 90º Right
Servo Center
(each with a delay of 1 second)

It is for HS-422 Servo (using Axon Microcontroller)

Here is the precise program [Pin E4]:
(I'm posting for OTHER lazy newbies like me, who just wants to get started without doing a PhD in Controlling Servos)

for (uint16_t i=0 ; i<15 ; i++)
{
servo(PORTE,4,700);  //CENTER
delay_ms(20);
}

delay_ms(1000);

for (uint16_t i=0 ; i<15 ; i++)
{
servo(PORTE,4,285);  //LEFT
delay_ms(20);
}

delay_ms(1000);

for (uint16_t i=0 ; i<15 ; i++)
{
servo(PORTE,4,700);  //CENTER
delay_ms(20); }
}

delay_ms(1000);

for (uint16_t i=0 ; i<15 ; i++)
{
servo(PORTE,4,1115);  //RIGHT
delay_ms(20);
}

delay_ms(1000);

for (uint16_t i=0 ; i<15 ; i++)
{
servo(PORTE,4,700);  //CENTER
delay_ms(20);
}

//Note: Difference in DurationCycle for 90º is 415
//That means:   left-center-right: 285-700-1115
//Could also be: left-center-right: 265-680-1095