[136] | 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 | // nothing to do inside the setup
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | void loop() {
|
---|
| 36 | // read the sensor value:
|
---|
| 37 | int sensorReading = analogRead(A0);
|
---|
| 38 | // map it to a range from 0 to 100:
|
---|
| 39 | int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
|
---|
| 40 | // set the motor speed:
|
---|
| 41 | if (motorSpeed > 0) {
|
---|
| 42 | myStepper.setSpeed(motorSpeed);
|
---|
| 43 | // step 1/100 of a revolution:
|
---|
| 44 | myStepper.step(stepsPerRevolution / 100);
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 |
|
---|