title: Stabilizing Infrared Distance Sensor Output Values
body: <p>I'm reading infrared Sharp distance sensors:</p>
  
  <p><a href="http://www.robofun.ro/senzori/infrarosu/senzor_sharp_%20GP2D120XJ00F" rel="nofollow">http://www.robofun.ro/senzori/infrarosu/senzor_sharp_%20GP2D120XJ00F</a></p>
  
  <p>With the reading I'm commanding a servo to direct the robot along a wall, a pretty simple map and write code.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Below you have my code:</p>
  
  <pre><code>#include &lt;Servo.h&gt;
  
  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);
   }
  </code></pre>
  
  <p>Please note that double-mapping is necessary because, otherwise variations would be MUCH worse.</p>
  
  <p>Thanks to anyone who will take the time to answer this and help me out...</p>
  
  <p>LE: I've already considered hysteresis, but I want something that will not lose time with unnecessary readings and calculations.</p>