/*
Controlling a servo position using a potentiometer (variable resistor)
by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
modified on 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Knob
*/
#include <Servo.h>
// Define the initial position of the servo motors
#define SERVO1_INIT 180
#define SERVO2_INIT 180
// Define the threshold for detecting potentiometer and acceleration
#define THRESHOLD_P 90
#define THRESHOLD_A 100
// Create objects for the servo motors
Servo myservo1;
Servo myservo2;
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup() {
// Attach the servo motors to their respective pins
myservo1.attach(9);
myservo2.attach(10);
// Set the initial position of the servo motors
myservo1.write(SERVO1_INIT);
myservo2.write(SERVO2_INIT);
// Initialize the serial communication
Serial.begin(9600);
}
void loop() {
// reads the value of the potentiometer (value between 0 and 1023)
val = analogRead(potpin);
// scale it to use it with the servo (value between 0 and 180)
val = map(val, 0, 1023, 0, 180);
// If the magnitude of the potentiometer is above the threshold,
// move the servo motors in same direction
if (val > THRESHOLD_P) {
// sets the servo position according to the scaled value
myservo1.write(val);
myservo2.write(val);
// waits for the servo to get there
delay(15);
// Print the potentiometer value to the serial monitor
Serial.println(val);
}
}