Servo Code

I am controlling a servo using Orangutan X2 and AVR. I am able to move it forward with the code below. Now, how can I make it go back to original position to be used in an arm. So, it will move one way to pick up, drop, and must return to new pick up position. I am having troube with the returning part.

#include <avr/io.h>
#include "LCD.h"
#include "SPI.h"
#include <avr/interrupt.h>
#define F_CPU 20000000UL
#include <util/delay.h>

// Include information about the device we're building for
#include "device.h"
// Include for the analog subsystem
#include "servo.h"

int main(void)

	SPIInit();			// allows the mega644 to send commands to the mega168
//	LCDInit();	

	DDRD |= 1 << PD5; // PD1 an output to send signal
	// Two servos
	//uint8_t left, right;
	// Initialize the servo system

	// Define our servos to use PC0 and PC1, respectively
	left  = servo_define(_SFR_IO_ADDR(DDRD), _SFR_IO_ADDR(PORTD), 5);
    //right = servo_define(_SFR_IO_ADDR(DDRC), _SFR_IO_ADDR(PORTC), 1);

	// Turn them both on

      servo_set(left, 1500);
      //servo_set(right, 1500);

The command servo_set doesn’t specify forward or backward rotation, so much as it moves the servo from wherever it is to a particular position. The second argument of the function specifies the length of the servo control pulse in microseconds, which corresponds to the position of the servo. A 1500 microsecond pulse is the “center” position, but try sending it other values, for example:

servo_set(left, 1200);
servo_set(left, 1800);

The default pulse range is defined in servo.h, and limits you to signals between 920us and 2120us. If you find your servos are pressing against their internal mechanical limits you can decrease this range.


Yes I know that the pulse width should control movement but I have tried everything and it keeps going in one dierection. In basic Samp for instance: 850 is one way and 650 is another.

I’m not really clear on what you mean by “keeps going in one direction”. Normal hobby servos have a limited range of motion (180 degrees or so). Are you using a servo that has been modified for continuous rotation?


I have both, continuous and limitted motion servos. It shouldn’t matter should it? All I am trying to so is to move back to the original position.

Continuous rotation modified servos have had their position-sensing potentiometer removed (or just set to mid-range and disconnected from the output shaft of the servo), so you can’t do position control with them. Have you tried your code with a limited-range servo?