#include <Servo.h>
int lastVal;
Servo myServo;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(A0, INPUT);
myServo.attach(A1);
}
long convert(long x, long in_min, long in_max, long out_min, long out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void loop() {
// put your main code here, to run repeatedly:
//To Print once everytime a new value comes in
if (convert(analogRead(A0), 0, 1023, 0, 180) != lastVal) {
//Prints value to serial
Serial.print("Pot Reading: ");
Serial.println(analogRead(A0));
Serial.print("Servo Degress: ");
Serial.println(convert(analogRead(A0), 0, 1023, 0, 180));
//stores read value to the lastVal variable
lastVal = convert(analogRead(A0), 0, 1023, 0, 180);
}
//Writes the value to the servo
myServo.write(lastVal);
}