Skip to content

Commit d706245

Browse files
committed
Added the Stepper motor library (and reference, and to the readme).
1 parent fa585fd commit d706245

File tree

7 files changed

+380
-0
lines changed

7 files changed

+380
-0
lines changed

build/fetch.sh

+2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ curl http://arduino.cc/en/Main/Environment -o environment.html
1414
curl http://www.arduino.cc/en/Reference/HomePage -o index.html
1515
curl http://www.arduino.cc/en/Reference/SoftwareSerial -o SoftwareSerial.html
1616
curl http://www.arduino.cc/en/Reference/EEPROM -o EEPROM.html
17+
curl http://www.arduino.cc/en/Reference/Stepper -o Stepper.html
1718
curl http://www.arduino.cc/en/pub/skins/arduino/arduino.css -o arduino.css
1819
for i in `grep -o "http://www.arduino.cc/en/Guide/[^']*" Guide_index.html | sort -u | grep -v '?' | cut -d '/' -f 6`; do curl http://www.arduino.cc/en/Guide/$i -o Guide_$i.html; done
1920
for i in `grep -o "http://www.arduino.cc/en/Reference/[^']*" index.html | sort -u | grep -v '?' | cut -d '/' -f 6`; do curl http://www.arduino.cc/en/Reference/$i -o $i.html; done
2021
for i in `grep -o "http://www.arduino.cc/en/Serial/[^']*" index.html | sort -u | grep -v '?' | cut -d '/' -f 6`; do curl http://www.arduino.cc/en/Serial/$i -o Serial_$i.html; done
2122
for i in `grep -o "http://www.arduino.cc/en/Reference/SoftwareSerial[^']*" SoftwareSerial.html | sort -u | grep -v '?' | cut -d '/' -f 6`; do curl http://www.arduino.cc/en/Reference/$i -o $i.html; done
2223
for i in `grep -o "http://www.arduino.cc/en/Reference/EEPROM[^']*" EEPROM.html | sort -u | grep -v '?' | cut -d '/' -f 6`; do curl http://www.arduino.cc/en/Reference/$i -o $i.html; done
24+
for i in `grep -o "http://www.arduino.cc/en/Reference/Stepper[^']*" Stepper.html | sort -u | grep -v '?' | cut -d '/' -f 6`; do curl http://www.arduino.cc/en/Reference/$i -o $i.html; done
2325
perl -i -pe "s|http://www.arduino.cc/en/Reference/[^?\"']*\?[^'\"]*|#|g" *.html # replace links to unknown pages with links to '#'
2426
perl -i -pe "s|http://www.arduino.cc/en/Guide/([^']*)|Guide_\1.html|g" *.html # replace links to remote guide with links to local guide
2527
perl -i -pe "s|http://www.arduino.cc/en/Reference/([^']*)|\1.html|g" *.html # replace links to remote reference with links to local reference

build/shared/reference.zip

18.2 KB
Binary file not shown.

readme.txt

+4
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ UPDATES
4848

4949
0008
5050

51+
* Updated examples (in distribution and on the website).
5152
* Added an EEPROM library (see reference for details).
53+
* Added a Stepper motor library (see reference).
5254
* Patched to reduce binary sketch sizes by building the Arduino core as
5355
a library (.a) file - now only the needed parts of the core are linked into
5456
a sketch. Originally written by Nicolas Roland, revised by Don Cross.
@@ -60,6 +62,8 @@ UPDATES
6062
* Lots of reference additions and fixes from Paul Badger.
6163
* Changed default microcontroller to ATmega168 from ATmega8.
6264
* Removed the delay from analogRead().
65+
* Activating TWI/I2C pullup resistors on the ATmega168 (in addition to the
66+
ATmega8).
6367

6468
0007 - 2006.12.25
6569

targets/libraries/Stepper/Stepper.cpp

+220
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
/*
2+
Stepper.cpp - - Stepper library for Wiring/Arduino - Version 0.4
3+
4+
Original library (0.1) by Tom Igoe.
5+
Two-wire modifications (0.2) by Sebastian Gassner
6+
Combination version (0.3) by Tom Igoe and David Mellis
7+
Bug fix for four-wire (0.4) by Tom Igoe, bug fix from Noah Shibley
8+
9+
Drives a unipolar or bipolar stepper motor using 2 wires or 4 wires
10+
11+
When wiring multiple stepper motors to a microcontroller,
12+
you quickly run out of output pins, with each motor requiring 4 connections.
13+
14+
By making use of the fact that at any time two of the four motor
15+
coils are the inverse of the other two, the number of
16+
control connections can be reduced from 4 to 2.
17+
18+
A slightly modified circuit around a Darlington transistor array or an L293 H-bridge
19+
connects to only 2 microcontroler pins, inverts the signals received,
20+
and delivers the 4 (2 plus 2 inverted ones) output signals required
21+
for driving a stepper motor.
22+
23+
The sequence of control signals for 4 control wires is as follows:
24+
25+
Step C0 C1 C2 C3
26+
1 1 0 1 0
27+
2 0 1 1 0
28+
3 0 1 0 1
29+
4 1 0 0 1
30+
31+
The sequence of controls signals for 2 control wires is as follows
32+
(columns C1 and C2 from above):
33+
34+
Step C0 C1
35+
1 0 1
36+
2 1 1
37+
3 1 0
38+
4 0 0
39+
40+
The circuits can be found at
41+
42+
http://www.arduino.cc/en/Tutorial/Stepper
43+
44+
45+
*/
46+
47+
48+
#include "WProgram.h"
49+
#include "Stepper.h"
50+
51+
/*
52+
* two-wire constructor.
53+
* Sets which wires should control the motor.
54+
*/
55+
Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2)
56+
{
57+
this->step_number = 0; // which step the motor is on
58+
this->speed = 0; // the motor speed, in revolutions per minute
59+
this->direction = 0; // motor direction
60+
this->last_step_time = 0; // time stamp in ms of the last step taken
61+
this->number_of_steps = number_of_steps; // total number of steps for this motor
62+
63+
// Arduino pins for the motor control connection:
64+
this->motor_pin_1 = motor_pin_1;
65+
this->motor_pin_2 = motor_pin_2;
66+
67+
// setup the pins on the microcontroller:
68+
pinMode(this->motor_pin_1, OUTPUT);
69+
pinMode(this->motor_pin_2, OUTPUT);
70+
71+
// When there are only 2 pins, set the other two to 0:
72+
this->motor_pin_3 = 0;
73+
this->motor_pin_4 = 0;
74+
75+
// pin_count is used by the stepMotor() method:
76+
this->pin_count = 2;
77+
}
78+
79+
80+
/*
81+
* constructor for four-pin version
82+
* Sets which wires should control the motor.
83+
*/
84+
85+
Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, int motor_pin_3, int motor_pin_4)
86+
{
87+
this->step_number = 0; // which step the motor is on
88+
this->speed = 0; // the motor speed, in revolutions per minute
89+
this->direction = 0; // motor direction
90+
this->last_step_time = 0; // time stamp in ms of the last step taken
91+
this->number_of_steps = number_of_steps; // total number of steps for this motor
92+
93+
// Arduino pins for the motor control connection:
94+
this->motor_pin_1 = motor_pin_1;
95+
this->motor_pin_2 = motor_pin_2;
96+
this->motor_pin_3 = motor_pin_3;
97+
this->motor_pin_4 = motor_pin_4;
98+
99+
// setup the pins on the microcontroller:
100+
pinMode(this->motor_pin_1, OUTPUT);
101+
pinMode(this->motor_pin_2, OUTPUT);
102+
pinMode(this->motor_pin_3, OUTPUT);
103+
pinMode(this->motor_pin_4, OUTPUT);
104+
105+
// pin_count is used by the stepMotor() method:
106+
this->pin_count = 4;
107+
}
108+
109+
/*
110+
Sets the speed in revs per minute
111+
112+
*/
113+
void Stepper::setSpeed(long whatSpeed)
114+
{
115+
this->step_delay = 60L * 1000L / this->number_of_steps / whatSpeed;
116+
}
117+
118+
/*
119+
Moves the motor steps_to_move steps. If the number is negative,
120+
the motor moves in the reverse direction.
121+
*/
122+
void Stepper::step(int steps_to_move)
123+
{
124+
int steps_left = abs(steps_to_move); // how many steps to take
125+
126+
// determine direction based on whether steps_to_mode is + or -:
127+
if (steps_to_move > 0) {this->direction = 1;}
128+
if (steps_to_move < 0) {this->direction = 0;}
129+
130+
131+
// decrement the number of steps, moving one step each time:
132+
while(steps_left > 0) {
133+
// move only if the appropriate delay has passed:
134+
if (millis() - this->last_step_time >= this->step_delay) {
135+
// step the motor to step number 0, 1, 2, or 3:
136+
stepMotor(this->step_number % 4);
137+
// get the timeStamp of when you stepped:
138+
this->last_step_time = millis();
139+
// increment or decrement the step number,
140+
// depending on direction:
141+
if (this->direction == 1) {
142+
this->step_number++;
143+
if (this->step_number == this->number_of_steps) {
144+
this->step_number = 0;
145+
}
146+
}
147+
else {
148+
if (this->step_number == 0) {
149+
this->step_number = this->number_of_steps;
150+
}
151+
this->step_number--;
152+
}
153+
// decrement the steps left:
154+
steps_left--;
155+
}
156+
}
157+
}
158+
159+
/*
160+
* Moves the motor forward or backwards.
161+
*/
162+
void Stepper::stepMotor(int thisStep)
163+
{
164+
if (this->pin_count == 2) {
165+
switch (thisStep) {
166+
case 0: /* 01 */
167+
digitalWrite(motor_pin_1, LOW);
168+
digitalWrite(motor_pin_2, HIGH);
169+
break;
170+
case 1: /* 11 */
171+
digitalWrite(motor_pin_1, HIGH);
172+
digitalWrite(motor_pin_2, HIGH);
173+
break;
174+
case 2: /* 10 */
175+
digitalWrite(motor_pin_1, HIGH);
176+
digitalWrite(motor_pin_2, LOW);
177+
break;
178+
case 3: /* 00 */
179+
digitalWrite(motor_pin_1, LOW);
180+
digitalWrite(motor_pin_2, LOW);
181+
break;
182+
}
183+
}
184+
if (this->pin_count == 4) {
185+
switch (thisStep) {
186+
case 0: // 1010
187+
digitalWrite(motor_pin_1, HIGH);
188+
digitalWrite(motor_pin_2, LOW);
189+
digitalWrite(motor_pin_3, HIGH);
190+
digitalWrite(motor_pin_4, LOW);
191+
break;
192+
case 1: // 0110
193+
digitalWrite(motor_pin_1, LOW);
194+
digitalWrite(motor_pin_2, HIGH);
195+
digitalWrite(motor_pin_3, HIGH);
196+
digitalWrite(motor_pin_4, LOW);
197+
break;
198+
case 2: //0101
199+
digitalWrite(motor_pin_1, LOW);
200+
digitalWrite(motor_pin_2, HIGH);
201+
digitalWrite(motor_pin_3, LOW);
202+
digitalWrite(motor_pin_4, HIGH);
203+
break;
204+
case 3: //1001
205+
digitalWrite(motor_pin_1, HIGH);
206+
digitalWrite(motor_pin_2, LOW);
207+
digitalWrite(motor_pin_3, LOW);
208+
digitalWrite(motor_pin_4, HIGH);
209+
break;
210+
}
211+
}
212+
}
213+
214+
/*
215+
version() returns the version of the library:
216+
*/
217+
int Stepper::version(void)
218+
{
219+
return 4;
220+
}

targets/libraries/Stepper/Stepper.h

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
Stepper.h - - Stepper library for Wiring/Arduino - Version 0.4
3+
4+
Original library (0.1) by Tom Igoe.
5+
Two-wire modifications (0.2) by Sebastian Gassner
6+
Combination version (0.3) by Tom Igoe and David Mellis
7+
Bug fix for four-wire (0.4) by Tom Igoe, bug fix from Noah Shibley
8+
9+
Drives a unipolar or bipolar stepper motor using 2 wires or 4 wires
10+
11+
When wiring multiple stepper motors to a microcontroller,
12+
you quickly run out of output pins, with each motor requiring 4 connections.
13+
14+
By making use of the fact that at any time two of the four motor
15+
coils are the inverse of the other two, the number of
16+
control connections can be reduced from 4 to 2.
17+
18+
A slightly modified circuit around a Darlington transistor array or an L293 H-bridge
19+
connects to only 2 microcontroler pins, inverts the signals received,
20+
and delivers the 4 (2 plus 2 inverted ones) output signals required
21+
for driving a stepper motor.
22+
23+
The sequence of control signals for 4 control wires is as follows:
24+
25+
Step C0 C1 C2 C3
26+
1 1 0 1 0
27+
2 0 1 1 0
28+
3 0 1 0 1
29+
4 1 0 0 1
30+
31+
The sequence of controls signals for 2 control wires is as follows
32+
(columns C1 and C2 from above):
33+
34+
Step C0 C1
35+
1 0 1
36+
2 1 1
37+
3 1 0
38+
4 0 0
39+
40+
The circuits can be found at
41+
http://www.arduino.cc/en/Tutorial/Stepper
42+
*/
43+
44+
// ensure this library description is only included once
45+
#ifndef Stepper_h
46+
#define Stepper_h
47+
48+
// include types & constants of Wiring core API
49+
#include "WConstants.h"
50+
51+
// library interface description
52+
class Stepper {
53+
public:
54+
// constructors:
55+
Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2);
56+
Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, int motor_pin_3, int motor_pin_4);
57+
58+
// speed setter method:
59+
void setSpeed(long whatSpeed);
60+
61+
// mover method:
62+
void step(int number_of_steps);
63+
64+
int version(void);
65+
66+
private:
67+
void stepMotor(int this_step);
68+
69+
int direction; // Direction of rotation
70+
int speed; // Speed in RPMs
71+
unsigned long step_delay; // delay between steps, in ms, based on speed
72+
int number_of_steps; // total number of steps this motor can take
73+
int pin_count; // whether you're driving the motor with 2 or 4 pins
74+
int step_number; // which step the motor is on
75+
76+
// motor pin numbers:
77+
int motor_pin_1;
78+
int motor_pin_2;
79+
int motor_pin_3;
80+
int motor_pin_4;
81+
82+
long last_step_time; // time stamp in ms of when the last step was taken
83+
};
84+
85+
#endif
86+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
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+
*/
9+
10+
#include <Stepper.h>
11+
12+
// change this to the number of steps on your motor
13+
#define STEPS 100
14+
15+
// create an instance of the stepper class, specifying
16+
// the number of steps of the motor and the pins it's
17+
// attached to
18+
Stepper stepper(STEPS, 8, 9, 10, 11);
19+
20+
// the previous reading from the analog input
21+
int previous = 0;
22+
23+
void setup()
24+
{
25+
// set the speed of the motor to 30 RPMs
26+
stepper.setSpeed(30);
27+
}
28+
29+
void loop()
30+
{
31+
// get the sensor value
32+
int val = analogRead(0);
33+
34+
// move a number of steps equal to the change in the
35+
// sensor reading
36+
stepper.step(val - previous);
37+
38+
// remember the previous value of the sensor
39+
previous = val;
40+
}

0 commit comments

Comments
 (0)