2015-07-06 15:18:33 +02:00
|
|
|
/*
|
|
|
|
Controlling a servo position using a potentiometer (variable resistor)
|
|
|
|
by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
|
2013-11-08 15:46:27 +01:00
|
|
|
|
|
|
|
modified on 8 Nov 2013
|
|
|
|
by Scott Fitzgerald
|
2015-05-20 17:10:06 +02:00
|
|
|
http://www.arduino.cc/en/Tutorial/Knob
|
2013-11-08 15:46:27 +01:00
|
|
|
*/
|
2013-10-21 09:58:40 +02:00
|
|
|
|
|
|
|
#include <Servo.h>
|
|
|
|
|
|
|
|
Servo myservo; // create servo object to control a servo
|
2008-05-08 00:15:55 +02:00
|
|
|
|
|
|
|
int potpin = 0; // analog pin used to connect the potentiometer
|
2013-10-21 09:58:40 +02:00
|
|
|
int val; // variable to read the value from the analog pin
|
|
|
|
|
2015-07-06 15:18:33 +02:00
|
|
|
void setup() {
|
2013-10-21 09:58:40 +02:00
|
|
|
myservo.attach(9); // attaches the servo on pin 9 to the servo object
|
|
|
|
}
|
|
|
|
|
2015-07-06 15:18:33 +02:00
|
|
|
void loop() {
|
|
|
|
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
|
|
|
|
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
|
|
|
|
myservo.write(val); // sets the servo position according to the scaled value
|
|
|
|
delay(15); // waits for the servo to get there
|
|
|
|
}
|
2013-11-11 12:40:59 +01:00
|
|
|
|