Skip to content

Commit 9f55a75

Browse files
committed
unsigned char approach
This version saves further 7 bytes of dynamic memory by changing unnessary int variables for unsigned char in private methods and members. 100% compatibility with the current version of master branch.
1 parent c046a3f commit 9f55a75

File tree

2 files changed

+19
-27
lines changed

2 files changed

+19
-27
lines changed

libraries/Stepper/src/Stepper.cpp

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2)
9494
0, 0, 0, (unsigned char*)phasesMatrix2, 2, 4);
9595
}
9696

97-
9897
/*
9998
* constructor for four-pin version
10099
* Sets which wires should control the motor.
@@ -155,55 +154,48 @@ void Stepper::step(int steps_to_move)
155154
while (steps_left > 0)
156155
{
157156
delayMicroseconds(this->step_delay); // move only if the appropriate delay has passed
158-
// unsigned long now = micros();
159-
// // move only if the appropriate delay has passed:
160-
// if (now - this->last_step_time >= this->step_delay)
161-
// {
162-
// // get the timeStamp of when you stepped:
163-
// this->last_step_time = now;
164157
// increment or decrement the step number,
165158
// depending on direction:
166159
if (steps_to_move >= 0)
167160
{
168161
this->step_number++;
169-
if (this->step_number == this->number_of_steps) {
162+
if (this->step_number >= this->number_of_steps) {
170163
this->step_number = 0;
171164
}
172165
}
173166
else
174167
{
175-
if (this->step_number == 0) {
168+
if (this->step_number <= 0) {
176169
this->step_number = this->number_of_steps;
177170
}
178171
this->step_number--;
179172
}
180-
// decrement the steps left:
181-
steps_left--;
182173
// step the motor to step number 0, 1, ..., {3 or 9}
183174
stepMotor(this->step_number % phase_count);
184-
// }
175+
// decrement the steps left:
176+
steps_left--;
185177
}
186178
}
187179

188180
/*
189181
* Moves the motor forward or backwards.
190182
*/
191-
void Stepper::stepMotor(int thisPhase)
183+
void Stepper::stepMotor(unsigned char thisPhase)
192184
{
193185
unsigned char phase = pgm_read_byte_near(phasesMatrix + thisPhase);
194186
unsigned char running_one = 0b10000000;
195-
for (int i = 0; i < pin_count; i++, running_one >>= 1){
187+
for (unsigned char i = 0; i < pin_count; i++, running_one >>= 1){
196188
digitalWrite(motor_pin[i], (phase & running_one));
197189
}
198190
}
199191

200192
/*
201193
* Initialize the motor
202194
*/
203-
void Stepper::initMotor(int number_of_steps, int motor_pin_1, int motor_pin_2,
204-
int motor_pin_3, int motor_pin_4,
205-
int motor_pin_5, unsigned char *phasesMatrix,
206-
int pin_count, int phase_count)
195+
void Stepper::initMotor(int number_of_steps, unsigned char motor_pin_1, unsigned char motor_pin_2,
196+
unsigned char motor_pin_3, unsigned char motor_pin_4,
197+
unsigned char motor_pin_5, unsigned char *phasesMatrix,
198+
unsigned char pin_count, unsigned char phase_count)
207199
{
208200
this->step_number = 0; // which step the motor is on
209201
this->number_of_steps = number_of_steps; // total number of steps for this motor
@@ -214,7 +206,7 @@ void Stepper::initMotor(int number_of_steps, int motor_pin_1, int motor_pin_2,
214206
this->phase_count = phase_count;
215207
// phasesMatrix is used by the stepMotor() method:
216208
this->phasesMatrix = phasesMatrix;
217-
for (int i = 0; i < this->pin_count; i++){
209+
for (unsigned char i = 0; i < this->pin_count; i++){
218210
switch (i) {
219211
case 0:
220212
this->motor_pin[i] = motor_pin_1; // Arduino pin 1 for the motor control connection

libraries/Stepper/src/Stepper.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,21 +141,21 @@ class Stepper {
141141
int version(void);
142142

143143
private:
144-
void stepMotor(int thisPhase);
145-
void initMotor(int number_of_steps, int motor_pin_1, int motor_pin_2,
146-
int motor_pin_3, int motor_pin_4,
147-
int motor_pin_5, unsigned char *phasesMatrix,
148-
int pin_count, int phase_count);
144+
void stepMotor(unsigned char thisPhase);
145+
void initMotor(int number_of_steps, unsigned char motor_pin_1, unsigned char motor_pin_2,
146+
unsigned char motor_pin_3, unsigned char motor_pin_4,
147+
unsigned char motor_pin_5, unsigned char *phasesMatrix,
148+
unsigned char pin_count, unsigned char phase_count);
149149

150150
unsigned long step_delay; // delay between steps, in ms, based on speed
151151
int number_of_steps; // total number of steps this motor can take
152-
int pin_count; // how many pins are in use.
153-
int phase_count; // how many phases are in use.
152+
unsigned char pin_count; // how many pins are in use.
153+
unsigned char phase_count; // how many phases are in use.
154154
unsigned char *phasesMatrix; // pointer to the phases Matrix
155155
int step_number; // which step the motor is on
156156

157157
// motor pin numbers:
158-
int motor_pin[5]; // Maximum 5 control signals
158+
unsigned char motor_pin[5]; // Maximum 5 control signals
159159
};
160160

161161
#endif

0 commit comments

Comments
 (0)