Last change
on this file since 199 was 136, checked in by ertl-honda, 9 years ago |
ライブラリとOS及びベーシックなサンプルの追加.
|
File size:
929 bytes
|
Rev | Line | |
---|
[136] | 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://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 | {
|
---|
| 19 | myservo.attach(9); // attaches the servo on pin 9 to the servo object
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | void loop()
|
---|
| 23 | {
|
---|
| 24 | val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
|
---|
| 25 | val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
|
---|
| 26 | myservo.write(val); // sets the servo position according to the scaled value
|
---|
| 27 | delay(15); // waits for the servo to get there
|
---|
| 28 | }
|
---|
| 29 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.