// Controlling a servo position using a potentiometer (variable resistor) // based on code by Michal Rinott // Modified by BJ Furman for ME 30 and the Spartronics Experimenter // ver. 1.1 29OCT2011 #include #define POT_PIN 1 // Arduino pin that the potentiometer connects to the Experimenter Servo myservo; // Create servo object to control a servo int potVoltage; // Variable to store the voltage value from the potentiometer int scaled_val = 0; // Variable to hold the scaled voltage value void setup() { myservo.attach(10); // Attaches the servo on pin 10 to the servo object } void loop() { potVoltage = analogRead(POT_PIN); // Reads the voltage of the potentiometer (returns a value between 0 and 1023) scaled_val = map(potVoltage, 0, 1023, 0, 179); // Scale the value to use with the servo (value between 0 and 180) myservo.write(scaled_val); // Sets the servo position according to the scaled value delay(15); // Waits for the servo to get there }