Real time servo control

Real time servo control

Spyridon Ampanavos
smoothServo.ino
/* Smoothly control a servo
*  
*  Control a servo using a potentiometer
*  This code removes potential noise in the readings
*  and makes the servo respond in smooth way to abrupt changes.
*  This can be useful to prevent fragile moving parts of your project
*  from breaking in some cases
*  
*/


#include <Servo.h>


Servo myservo;
float smoothPos = 0;


void setup() {
    myservo.attach(9);
}


void loop() {
    int pot = analogRead(A0);
    float pos = map(pot, 0, 1023, 0, 180);
    float smoothPos = 0.98*smooothPos + 0.02*pos;
}