I'm reading infrared Sharp distance sensors:
http://www.robofun.ro/senzori/infrarosu/senzor_sharp_%20GP2D120XJ00F
With the reading I'm commanding a servo to direct the robot along a wall, a pretty simple map and write code.
The problem I'm having is that the sensor readings are way off, not behaving liniar... I know other people use these sensors just fine and I know they're wired just fine.
My question is how can I eliminate any oscillations sent to the servo? I'm getting 1 to 3 degrees of oscillations with the robot standing still at a fixed distance from the wall.
Below you have my code:
#include <Servo.h>
Servo myservo;
int val;
int val1;
int IRpin = A7;
// analog pin for reading the IR sensor
void setup() {
myservo.attach(3);
Serial.begin(9600); // start the serial port
}
void loop() {
float volts = analogRead(IRpin);
delay(100);
val1 = map(volts, 230,500, 0 ,100);
val = map(val1, 0, 100, 100, 80);
myservo.write(val);
Serial.println(val);
}
Please note that double-mapping is necessary because, otherwise variations would be MUCH worse.
Thanks to anyone who will take the time to answer this and help me out...
LE: I've already considered hysteresis, but I want something that will not lose time with unnecessary readings and calculations.