Controlling servo position from proximity to infared distance sensor.

I am trying to make a sculpture that responds to peoples proximity to it. A square of elastic fabric is mounted on a wall, as the viewer approaches the square I am trying to have a servo push out the fabric out more and more as the person gets closer. I have a decent working setup now with an HC-SR04 ultrasonic sensor. However the sensor is not reliable when reading off clothing, especially loose and baggy clothing. I have a SHARP GP2Y0A21YK0F 10-80cm ir distance sensor that I have set up with the serial monitor and is doing a much better job of measuring but I don't know where to start including the servo into this code. I have researched without any luck controlling movement with the ir sensor. Any advice on how to accomplish this would be greatly appreciated, thank you.

Have you used the example servo programs to make the servo operate the way you want?

Paul

Have a look at the Knob example of the Servo library.

Replace the knob reading with your proximity sensor.

If you are getting sensible numbers from your sensor then just use the map() function to convert those numbers into ones that are appropriate to write() to the servo. That’s it done!

Steve

wvmarle:
Have a look at the Knob example of the Servo library.

Replace the knob reading with your proximity sensor.

Thank you I didn't know I could do this. I figured the ir sensor was so much more complicated than a potentiometer they were totally different. My code is so much simpler than with the ultrasonic and it is way more responsive. Now to include serial monitor readings. Awesome!

The Arduino doesn't care about what sensor is attached or how complex its internal structure is. It only cares about the signal that comes out of the sensor.