Skip to content

Support generic drivers for Stepper library #4257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
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.
  • Loading branch information
skniazev committed Dec 7, 2015
commit c046a3f748751da1e165450ff60b0ae8f394d03d
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const int stepsPerRevolution = 4096; // change this to fit the number of steps
* 7 1 0 0 0
* 8 1 0 0 1
*/
const unsigned char phasesMatrix[8]= {
const unsigned char phasesMatrix[8] PROGMEM = {
0b00010000,
0b00110000,
0b00100000,
Expand Down
5 changes: 3 additions & 2 deletions libraries/Stepper/src/Stepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,12 @@ void Stepper::step(int steps_to_move)
/*
* Moves the motor forward or backwards.
*/
void Stepper::stepMotor(int thisStep)
void Stepper::stepMotor(int thisPhase)
{
unsigned char phase = pgm_read_byte_near(phasesMatrix + thisPhase);
unsigned char running_one = 0b10000000;
for (int i = 0; i < pin_count; i++, running_one >>= 1){
digitalWrite(motor_pin[i], (phasesMatrix[thisStep] & running_one));
digitalWrite(motor_pin[i], (phase & running_one));
}
}

Expand Down
64 changes: 33 additions & 31 deletions libraries/Stepper/src/Stepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,38 @@
#ifndef Stepper_h
#define Stepper_h

#include <avr/pgmspace.h>

//Default phases matrix for 2 control wires is as follows
const unsigned char phasesMatrix2[4] PROGMEM = {
0b01000000,
0b11000000,
0b10000000,
0b00000000
};

//Default phases matrix for 4 control wires is as follows
const unsigned char phasesMatrix4[4] PROGMEM = {
0b10100000,
0b01100000,
0b01010000,
0b10010000
};

//Default phases matrix for 5 control wires is as follows
const unsigned char phasesMatrix5[10] PROGMEM = {
0b01101000,
0b01001000,
0b01011000,
0b01010000,
0b11010000,
0b10010000,
0b10110000,
0b10100000,
0b10101000,
0b00101000
};

// library interface description
class Stepper {
public:
Expand All @@ -109,7 +141,7 @@ class Stepper {
int version(void);

private:
void stepMotor(int this_step);
void stepMotor(int thisPhase);
void initMotor(int number_of_steps, int motor_pin_1, int motor_pin_2,
int motor_pin_3, int motor_pin_4,
int motor_pin_5, unsigned char *phasesMatrix,
Expand All @@ -124,36 +156,6 @@ class Stepper {

// motor pin numbers:
int motor_pin[5]; // Maximum 5 control signals

//Default phases matrix for 2 control wires is as follows
const unsigned char phasesMatrix2[4] = {
0b01000000,
0b11000000,
0b10000000,
0b00000000
};

//Default phases matrix for 4 control wires is as follows
const unsigned char phasesMatrix4[4] = {
0b10100000,
0b01100000,
0b01010000,
0b10010000
};

//Default phases matrix for 5 control wires is as follows
const unsigned char phasesMatrix5[10] = {
0b01101000,
0b01001000,
0b01011000,
0b01010000,
0b11010000,
0b10010000,
0b10110000,
0b10100000,
0b10101000,
0b00101000
};
};

#endif
Expand Down