by mwtse » Sun Jun 17, 2012 8:52 pm
I've switched to ServoTimer2 library but the problem persist. CRO shown that the pulse width change (become much wider) nearly every second (I've put a 1 second delay). However, the change in pulse width do not occur every time. The following is the bare minimum code that can reproduce the problem, any hint?
#include <ServoTimer2.h> // changed from Servo.h
#include <SoftwareSerial.h>
ServoTimer2 servo; // create servo object to control a servo
SoftwareSerial sensorSerial(2, 255, true);
//pin 3 of Arduino connect to Rx of sensor, a HIGH enable the ranger sensor
const unsigned char sensor=3;
void setup()
{
servo.attach(9); // attaches the servo to a pin
servo.write(100); // sets the servo position
pinMode(sensor, OUTPUT);
digitalWrite(sensor, LOW); // switch off the range sensor until needed
sensorSerial.begin(9600); // set the data rate for the SoftwareSerial port
}
void loop() {
digitalWrite(sensor,HIGH);
delay(1); // 1 ms is enough to get senor to make 1 measuerment
digitalWrite(sensor,LOW);
delay(49); // sensor need time to make measurement
while (sensorSerial.available()) {
char c = sensorSerial.read();
}
delay(1000);
}