Project

General

Profile

Statistics
| Branch: | Revision:

root / arduino-1.0 / libraries / Stepper / examples / stepper_speedControl / stepper_speedControl.ino @ 58d82c77

History | View | Annotate | Download (1.25 KB)

1

    
2
/* 
3
 Stepper Motor Control - speed control
4
 
5
 This program drives a unipolar or bipolar stepper motor. 
6
 The motor is attached to digital pins 8 - 11 of the Arduino.
7
 A potentiometer is connected to analog input 0.
8
 
9
 The motor will rotate in a clockwise direction. The higher the potentiometer value,
10
 the faster the motor speed. Because setSpeed() sets the delay between steps, 
11
 you may notice the motor is less responsive to changes in the sensor value at
12
 low speeds.
13
 
14
 Created 30 Nov. 2009
15
 Modified 28 Oct 2010
16
 by Tom Igoe
17
 
18
 */
19

    
20
#include <Stepper.h>
21

    
22
const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
23
// for your motor
24

    
25

    
26
// initialize the stepper library on pins 8 through 11:
27
Stepper myStepper(stepsPerRevolution, 8,9,10,11);            
28

    
29
int stepCount = 0;         // number of steps the motor has taken
30

    
31
void setup() {
32
  // initialize the serial port:
33
  Serial.begin(9600);
34
}
35

    
36
void loop() {
37
  // read the sensor value:
38
  int sensorReading = analogRead(A0);
39
  // map it to a range from 0 to 100:
40
  int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
41
  // set the motor speed:
42
  if (motorSpeed > 0) {
43
    myStepper.setSpeed(motorSpeed);
44
    // step 1/100 of a revolution:
45
    myStepper.step(stepsPerRevolution/100);
46
  } 
47
}
48

    
49