Skip to content

Commit c046a3f

Browse files
committed
PROGMEM approach
This version saves 28 bytes of dynamic memory in library code by using PROGMEM for driver patterns. File stepper_oneRevolution_28BYJ48_ULN2003.ino shows, how to use PROGMEM also for patterns of custom drivers.
1 parent 3540590 commit c046a3f

File tree

3 files changed

+37
-34
lines changed

3 files changed

+37
-34
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
@@ -35,7 +35,7 @@ const int stepsPerRevolution = 4096; // change this to fit the number of steps
3535
* 7 1 0 0 0
3636
* 8 1 0 0 1
3737
*/
38-
const unsigned char phasesMatrix[8]= {
38+
const unsigned char phasesMatrix[8] PROGMEM = {
3939
0b00010000,
4040
0b00110000,
4141
0b00100000,

libraries/Stepper/src/Stepper.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,12 @@ void Stepper::step(int steps_to_move)
188188
/*
189189
* Moves the motor forward or backwards.
190190
*/
191-
void Stepper::stepMotor(int thisStep)
191+
void Stepper::stepMotor(int thisPhase)
192192
{
193+
unsigned char phase = pgm_read_byte_near(phasesMatrix + thisPhase);
193194
unsigned char running_one = 0b10000000;
194195
for (int i = 0; i < pin_count; i++, running_one >>= 1){
195-
digitalWrite(motor_pin[i], (phasesMatrix[thisStep] & running_one));
196+
digitalWrite(motor_pin[i], (phase & running_one));
196197
}
197198
}
198199

libraries/Stepper/src/Stepper.h

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,38 @@
8585
#ifndef Stepper_h
8686
#define Stepper_h
8787

88+
#include <avr/pgmspace.h>
89+
90+
//Default phases matrix for 2 control wires is as follows
91+
const unsigned char phasesMatrix2[4] PROGMEM = {
92+
0b01000000,
93+
0b11000000,
94+
0b10000000,
95+
0b00000000
96+
};
97+
98+
//Default phases matrix for 4 control wires is as follows
99+
const unsigned char phasesMatrix4[4] PROGMEM = {
100+
0b10100000,
101+
0b01100000,
102+
0b01010000,
103+
0b10010000
104+
};
105+
106+
//Default phases matrix for 5 control wires is as follows
107+
const unsigned char phasesMatrix5[10] PROGMEM = {
108+
0b01101000,
109+
0b01001000,
110+
0b01011000,
111+
0b01010000,
112+
0b11010000,
113+
0b10010000,
114+
0b10110000,
115+
0b10100000,
116+
0b10101000,
117+
0b00101000
118+
};
119+
88120
// library interface description
89121
class Stepper {
90122
public:
@@ -109,7 +141,7 @@ class Stepper {
109141
int version(void);
110142

111143
private:
112-
void stepMotor(int this_step);
144+
void stepMotor(int thisPhase);
113145
void initMotor(int number_of_steps, int motor_pin_1, int motor_pin_2,
114146
int motor_pin_3, int motor_pin_4,
115147
int motor_pin_5, unsigned char *phasesMatrix,
@@ -124,36 +156,6 @@ class Stepper {
124156

125157
// motor pin numbers:
126158
int motor_pin[5]; // Maximum 5 control signals
127-
128-
//Default phases matrix for 2 control wires is as follows
129-
const unsigned char phasesMatrix2[4] = {
130-
0b01000000,
131-
0b11000000,
132-
0b10000000,
133-
0b00000000
134-
};
135-
136-
//Default phases matrix for 4 control wires is as follows
137-
const unsigned char phasesMatrix4[4] = {
138-
0b10100000,
139-
0b01100000,
140-
0b01010000,
141-
0b10010000
142-
};
143-
144-
//Default phases matrix for 5 control wires is as follows
145-
const unsigned char phasesMatrix5[10] = {
146-
0b01101000,
147-
0b01001000,
148-
0b01011000,
149-
0b01010000,
150-
0b11010000,
151-
0b10010000,
152-
0b10110000,
153-
0b10100000,
154-
0b10101000,
155-
0b00101000
156-
};
157159
};
158160

159161
#endif

0 commit comments

Comments
 (0)