root / arduino-1.0 / libraries / Stepper / examples / MotorKnob / MotorKnob.ino @ 58d82c77
History | View | Annotate | Download (863 Bytes)
1 | 58d82c77 | Tom Mullins | /* |
---|---|---|---|
2 | * MotorKnob |
||
3 | * |
||
4 | * A stepper motor follows the turns of a potentiometer |
||
5 | * (or other sensor) on analog input 0. |
||
6 | * |
||
7 | * http://www.arduino.cc/en/Reference/Stepper |
||
8 | * This example code is in the public domain. |
||
9 | */ |
||
10 | |||
11 | #include <Stepper.h> |
||
12 | |||
13 | // change this to the number of steps on your motor |
||
14 | #define STEPS 100 |
||
15 | |||
16 | // create an instance of the stepper class, specifying |
||
17 | // the number of steps of the motor and the pins it's |
||
18 | // attached to |
||
19 | Stepper stepper(STEPS, 8, 9, 10, 11); |
||
20 | |||
21 | // the previous reading from the analog input |
||
22 | int previous = 0; |
||
23 | |||
24 | void setup() |
||
25 | { |
||
26 | // set the speed of the motor to 30 RPMs |
||
27 | stepper.setSpeed(30); |
||
28 | } |
||
29 | |||
30 | void loop() |
||
31 | { |
||
32 | // get the sensor value |
||
33 | int val = analogRead(0); |
||
34 | |||
35 | // move a number of steps equal to the change in the |
||
36 | // sensor reading |
||
37 | stepper.step(val - previous); |
||
38 | |||
39 | // remember the previous value of the sensor |
||
40 | previous = val; |
||
41 | } |