Hardware Reference
In-Depth Information
8
9 // the previous reading from the analog input
10 int previous = 0;
11
12 void setup()
13 {
14 // Set a low stepper speed
15 stepper.setSpeed(10);
16
17 // Make a single temperature reading
18 previous = analogRead(0);
19 }
20
21 void loop()
22 {
23 // Get the sensor value
24 int val = analogRead(0);
25
26 // Move the stepper motor depending on the result
27 stepper.step(val - previous);
28
29 // Remember the previous value
30 previous = val;
31
32 delay(5000);
33 }
The Stepper.h i le is required for any projects that use the stepper library,
and this is included on line one of the sketch. On line 4, the amount of steps
required to make a complete revolution is dei ned. Change this according to
the stepper motor you have. On line 7, the Stepper instance is created using the
amount of steps dei ned in STEPS and using digital lines 8 through 11.
setup() dei ned on line 12 does two things. First, it sets up the speed of the
stepper motor to 10 rpm. This is a relatively slow speed, but the motor doesn't
need to turn quickly. Secondly, it takes a reading from the temperature sensor
to use as a reference value. The value is stored in previous , a variable dei ned
on line 10.
On line 21, loop() is declared. In loop() , you'll i rst read the value of the
analog pin into a variable called val and then change the stepper motor's
position by the difference between previous and val . Finally, the contents of
previous are replaced by the contents of val , and the sketch waits for 5 seconds
before looping.
Search WWH ::




Custom Search