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
}