controlling servos with writeMicroseconds

Hi,

im trying to control a servo with the servo library, and writeMicroseconds, but having no luck.

Ive found Scott Harris’ example that i found with goolge, on using interrupts, to read values off pin 2 (my receiver)

but im getting alot of noise on the servo1. and as its passed into the writeMicroseconds.

with the code as it is i get the servo moving erratically… but i think its more of a bad HIGH,LOW (im not using a pull down resistor, and im not sure if i need one.)

i hope someone can help me here.

p.s if i change from using pin 9 to pin 6, the servo becomes a lot more stable, but still doesn’t respond in the way im expecting it to.

// Reading servos with interrupts

// For an Arduino Mega

// Scott Harris January 2010

//

// This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License.

// To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/

#include <Servo.h>

volatile long servo1; // servo value

volatile long count1; // temporary variable for servo1

#define int0 (PIND & 0b00000010) //For Duemilenove. Untested!

Servo myServo;

void handleInterrupt()

{

if(int0)

count1=micros(); // we got a positive edge

else

servo1=micros()-count1; // Negative edge: get pulsewidth

}

void setup()

{

Serial.begin(9600);

pinMode(2,INPUT);

attachInterrupt(0,handleInterrupt,CHANGE); // Catch up and down

myServo.attach(9);

}

void loop()

{

delay(50);

Serial.println(servo1,DEC); // Pulsewidth in microseconds

myServo.writeMicroseconds(servo1); // Mirror servo on another pin

}

Are you using a mega or duemilenove?

Can you successfully control a servo if you don’t use the interrupt code?

Do the readings from the interrupt code look ok? If they are noisy, by how much?

Have you tried scoping the rx output and comparing the pulsenwidth measurements from the interrupt code to the scope measurements?

How about a detailed description of your setup?

I don’t recall using a pull-up or pull-down in my setup, but that might depend on the rx you’re using.

Break it down into pieces to isolate the problem.

Scott

ScottH:
Are you using a mega or duemilenove?

Can you successfully control a servo if you don’t use the interrupt code?

Do the readings from the interrupt code look ok? If they are noisy, by how much?

Have you tried scoping the rx output and comparing the pulsenwidth measurements from the interrupt code to the scope measurements?

How about a detailed description of your setup?

I don’t recall using a pull-up or pull-down in my setup, but that might depend on the rx you’re using.

Break it down into pieces to isolate the problem.

Scott

Hi Scott,

im using a mini pro.

for some strange reason when i first started out i could only upload a sketch if i had fio selected, and not the mini pro.

this screwed with the times of everything.

now that i have the mini pro selected, and uploading things with out errors, things are getting better.

i can now get the servos to work with servo.write. and can do the full 180.

but what puzzles me the most now is the code that handles the interrupts.

if i set the servos with writeMicroseconds, i get the right angles, etc. but if i wait a while the servo will reset it self.

do i have to setup all the pins im not using to GRD, so that i dont get any strange interrupts occurring??

i dont have any fancy hardware to test with so im just breaking the problem down piece by piece.

Dave

Dave,

I’m not at all sure the resetting your seeing is evidence of a problem or of interrupt activity. IIRC, servo modules will “remember” their setpoints for only a few seconds. To hold a position, you need to keep sending the setpoint. My inference is that the servo library you are using does that for you, but you’ll have to code the “reminders” yourself to drive a servo with writeMicroseconds.

Anyone better clued than I care to chime in?

Eric

I don’t think you need to periodically update the writeMicroseconds call to the servo library. It sends out the PWM at the appropriate rate.

Not sure what you mean by the servo resetting itself.

How are things hooked up? Why not remove the servo and print out the data coming from the interrupt routine over a period of time. Is electrical noise from the servo messing with things? Are the servo and arduino on the same supply? Do you have decoupling caps? Is the data fro. The interrupt routine the same with and without the servo attached?

Scott

Is it a 3.3V pro mini or a 5V model? How are things hooked up?

Scott