|
Last change
on this file since 24 was
3,
checked in by markus, 7 years ago
|
|
Setup project structure
|
|
File size:
923 bytes
|
| Rev | Line | |
|---|
| [3] | 1 | /* |
|---|
| 2 | Controlling a servo position using a potentiometer (variable resistor) |
|---|
| 3 | by Michal Rinott <http://people.interaction-ivrea.it/m.rinott> |
|---|
| 4 | |
|---|
| 5 | modified on 8 Nov 2013 |
|---|
| 6 | by Scott Fitzgerald |
|---|
| 7 | http://www.arduino.cc/en/Tutorial/Knob |
|---|
| 8 | */ |
|---|
| 9 | |
|---|
| 10 | #include <Servo.h> |
|---|
| 11 | |
|---|
| 12 | Servo myservo; // create servo object to control a servo |
|---|
| 13 | |
|---|
| 14 | int potpin = 0; // analog pin used to connect the potentiometer |
|---|
| 15 | int val; // variable to read the value from the analog pin |
|---|
| 16 | |
|---|
| 17 | void setup() { |
|---|
| 18 | myservo.attach(9); // attaches the servo on pin 9 to the servo object |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | void loop() { |
|---|
| 22 | val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) |
|---|
| 23 | val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180) |
|---|
| 24 | myservo.write(val); // sets the servo position according to the scaled value |
|---|
| 25 | delay(15); // waits for the servo to get there |
|---|
| 26 | } |
|---|
| 27 | |
|---|
Note: See
TracBrowser
for help on using the repository browser.