Skip to content

Commit c1442a5

Browse files
committed
Generic constructors without zero parameters
matthijskooijman wrote: > I don't think the generic constructor's arguments are > really easy to use, having to pass zeroes for unused > pins. Why not just add versions of the current > constructors, but with the added phases matrix > arguments? Fixed with this commit.
1 parent 5b947dc commit c1442a5

File tree

3 files changed

+78
-14
lines changed

3 files changed

+78
-14
lines changed

libraries/Stepper/examples/stepper_oneRevolution_28BYJ48_ULN2003/stepper_oneRevolution_28BYJ48_ULN2003.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const unsigned char phasesMatrix[8] PROGMEM = {
4747
};
4848

4949
//initialize the stepper library on pins 8 through 11, phasesMatrix, pin_count, phase_count:
50-
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11, 0, (unsigned char*)phasesMatrix, 4, 8);
50+
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11, 0, 4, (unsigned char*)phasesMatrix, 8);
5151

5252
void setup() {
5353
// 17 is the maximum for counterclockwise revolution of motor 28BYJ-48

libraries/Stepper/src/Stepper.cpp

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
Stepper::Stepper(unsigned number_of_steps, int motor_pin_0, int motor_pin_1)
9292
{
9393
initMotor(number_of_steps, motor_pin_0, motor_pin_1,
94-
0, 0, 0, (unsigned char*)phasesMatrix2, 2, 4);
94+
0, 0, 0, 2, (unsigned char*)phasesMatrix2, 4);
9595
}
9696

9797
/*
@@ -103,7 +103,7 @@ Stepper::Stepper(unsigned number_of_steps, int motor_pin_0, int motor_pin_1,
103103
{
104104
initMotor(number_of_steps, motor_pin_0, motor_pin_1,
105105
motor_pin_2, motor_pin_3,
106-
0, (unsigned char*)phasesMatrix4, 4, 4);
106+
0, 4, (unsigned char*)phasesMatrix4, 4);
107107
}
108108

109109
/*
@@ -116,7 +116,61 @@ Stepper::Stepper(unsigned number_of_steps, int motor_pin_0, int motor_pin_1,
116116
{
117117
initMotor(number_of_steps, motor_pin_0, motor_pin_1,
118118
motor_pin_2, motor_pin_3,
119-
motor_pin_4, (unsigned char*)phasesMatrix5, 5, 10);
119+
motor_pin_4, 5, (unsigned char*)phasesMatrix5, 10);
120+
}
121+
122+
/*
123+
* universal constructor for motors with only one wire
124+
* Sets which wires should control the motor, pointer to the phases matrix, number of pins and phases.
125+
*/
126+
Stepper::Stepper(unsigned number_of_steps, int motor_pin_0,
127+
int pin_count, unsigned char *phasesMatrix, int phase_count)
128+
{
129+
initMotor(number_of_steps, motor_pin_0, 0,
130+
0, 0,
131+
0, pin_count,
132+
phasesMatrix,phase_count);
133+
}
134+
135+
/*
136+
* universal constructor for motors with maximum two wires
137+
* Sets which wires should control the motor, pointer to the phases matrix, number of pins and phases.
138+
*/
139+
Stepper::Stepper(unsigned number_of_steps, int motor_pin_0, int motor_pin_1,
140+
int pin_count, unsigned char *phasesMatrix, int phase_count)
141+
{
142+
initMotor(number_of_steps, motor_pin_0, motor_pin_1,
143+
0, 0,
144+
0, pin_count,
145+
phasesMatrix,phase_count);
146+
}
147+
148+
/*
149+
* universal constructor for motors with maximum three wires
150+
* Sets which wires should control the motor, pointer to the phases matrix, number of pins and phases.
151+
*/
152+
Stepper::Stepper(unsigned number_of_steps, int motor_pin_0, int motor_pin_1,
153+
int motor_pin_2, int pin_count,
154+
unsigned char *phasesMatrix, int phase_count)
155+
{
156+
initMotor(number_of_steps, motor_pin_0, motor_pin_1,
157+
motor_pin_2, 0,
158+
0, pin_count,
159+
phasesMatrix,phase_count);
160+
}
161+
162+
/*
163+
* universal constructor for motors with maximum four wires
164+
* Sets which wires should control the motor, pointer to the phases matrix, number of pins and phases.
165+
*/
166+
Stepper::Stepper(unsigned number_of_steps, int motor_pin_0, int motor_pin_1,
167+
int motor_pin_2, int motor_pin_3, int pin_count,
168+
unsigned char *phasesMatrix, int phase_count)
169+
{
170+
initMotor(number_of_steps, motor_pin_0, motor_pin_1,
171+
motor_pin_2, motor_pin_3,
172+
0, pin_count,
173+
phasesMatrix,phase_count);
120174
}
121175

122176
/*
@@ -125,13 +179,13 @@ Stepper::Stepper(unsigned number_of_steps, int motor_pin_0, int motor_pin_1,
125179
*/
126180
Stepper::Stepper(unsigned number_of_steps, int motor_pin_0, int motor_pin_1,
127181
int motor_pin_2, int motor_pin_3,
128-
int motor_pin_4, unsigned char *phasesMatrix,
129-
int pin_count, int phase_count)
182+
int motor_pin_4, int pin_count,
183+
unsigned char *phasesMatrix, int phase_count)
130184
{
131185
initMotor(number_of_steps, motor_pin_0, motor_pin_1,
132186
motor_pin_2, motor_pin_3,
133-
motor_pin_4, phasesMatrix,
134-
pin_count, phase_count);
187+
motor_pin_4, pin_count,
188+
phasesMatrix,phase_count);
135189
}
136190

137191
/*
@@ -200,8 +254,8 @@ void Stepper::stepMotor(unsigned char thisPhase)
200254
*/
201255
void Stepper::initMotor(unsigned number_of_steps, unsigned char motor_pin_0, unsigned char motor_pin_1,
202256
unsigned char motor_pin_2, unsigned char motor_pin_3,
203-
unsigned char motor_pin_4, unsigned char *phasesMatrix,
204-
unsigned char pin_count, unsigned char phase_count)
257+
unsigned char motor_pin_4, unsigned char pin_count,
258+
unsigned char *phasesMatrix,unsigned char phase_count)
205259
{
206260
this->step_number = 0; // which step the motor is on
207261
this->number_of_steps = number_of_steps; // total number of steps for this motor

libraries/Stepper/src/Stepper.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,20 @@ class Stepper {
127127
Stepper(unsigned number_of_steps, int motor_pin_0, int motor_pin_1,
128128
int motor_pin_2, int motor_pin_3,
129129
int motor_pin_4);
130+
Stepper(unsigned number_of_steps, int motor_pin_0,
131+
int pin_count, unsigned char *phasesMatrix, int phase_count);
132+
Stepper(unsigned number_of_steps, int motor_pin_0, int motor_pin_1,
133+
int pin_count, unsigned char *phasesMatrix, int phase_count);
134+
Stepper(unsigned number_of_steps, int motor_pin_0, int motor_pin_1,
135+
int motor_pin_2, int pin_count,
136+
unsigned char *phasesMatrix, int phase_count);
137+
Stepper(unsigned number_of_steps, int motor_pin_0, int motor_pin_1,
138+
int motor_pin_2, int motor_pin_3, int pin_count,
139+
unsigned char *phasesMatrix, int phase_count);
130140
Stepper(unsigned number_of_steps, int motor_pin_0, int motor_pin_1,
131141
int motor_pin_2, int motor_pin_3,
132-
int motor_pin_4, unsigned char *phasesMatrix,
133-
int pin_count, int phase_count);
142+
int motor_pin_4, int pin_count,
143+
unsigned char *phasesMatrix, int phase_count);
134144

135145
// speed setter method:
136146
void setSpeed(long whatSpeed);
@@ -144,8 +154,8 @@ class Stepper {
144154
void stepMotor(unsigned char thisPhase);
145155
void initMotor(unsigned number_of_steps, unsigned char motor_pin_0, unsigned char motor_pin_1,
146156
unsigned char motor_pin_2, unsigned char motor_pin_3,
147-
unsigned char motor_pin_4, unsigned char *phasesMatrix,
148-
unsigned char pin_count, unsigned char phase_count);
157+
unsigned char motor_pin_4, unsigned char pin_count,
158+
unsigned char *phasesMatrix,unsigned char phase_count);
149159

150160
unsigned long step_delay; // delay between steps, in ms, based on speed
151161
unsigned number_of_steps; // total number of steps this motor can take

0 commit comments

Comments
 (0)