diff --git a/examples/MotorKnob/MotorKnob.ino b/examples/MotorKnob/MotorKnob.ino index 5cf12ec..7a49818 100644 --- a/examples/MotorKnob/MotorKnob.ino +++ b/examples/MotorKnob/MotorKnob.ino @@ -24,6 +24,10 @@ int previous = 0; void setup() { // set the speed of the motor to 30 RPMs stepper.setSpeed(30); + + // uncomment this line if using a motor with an 8-beat pattern, + // like the 28BY-J-48 4-wire motor. + // stepper.setBeatsPerPattern(8); } void loop() { @@ -36,4 +40,4 @@ void loop() { // remember the previous value of the sensor previous = val; -} \ No newline at end of file +} diff --git a/examples/stepper_oneRevolution/stepper_oneRevolution.ino b/examples/stepper_oneRevolution/stepper_oneRevolution.ino index 373eb60..5bcda16 100644 --- a/examples/stepper_oneRevolution/stepper_oneRevolution.ino +++ b/examples/stepper_oneRevolution/stepper_oneRevolution.ino @@ -26,6 +26,11 @@ Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); void setup() { // set the speed at 60 rpm: myStepper.setSpeed(60); + + // uncomment this line if using a motor with an 8-beat pattern, + // like the 28BY-J-48 4-wire motor. + // stepper.setBeatsPerPattern(8); + // initialize the serial port: Serial.begin(9600); } diff --git a/examples/stepper_oneStepAtATime/stepper_oneStepAtATime.ino b/examples/stepper_oneStepAtATime/stepper_oneStepAtATime.ino index e6c141f..46db8cf 100644 --- a/examples/stepper_oneStepAtATime/stepper_oneStepAtATime.ino +++ b/examples/stepper_oneStepAtATime/stepper_oneStepAtATime.ino @@ -31,6 +31,10 @@ int stepCount = 0; // number of steps the motor has taken void setup() { // initialize the serial port: Serial.begin(9600); + + // uncomment this line if using a motor with an 8-beat pattern, + // like the 28BY-J-48 4-wire motor. + // stepper.setBeatsPerPattern(8); } void loop() { diff --git a/examples/stepper_speedControl/stepper_speedControl.ino b/examples/stepper_speedControl/stepper_speedControl.ino index 5eb4f6a..ad8f129 100644 --- a/examples/stepper_speedControl/stepper_speedControl.ino +++ b/examples/stepper_speedControl/stepper_speedControl.ino @@ -29,7 +29,9 @@ Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); int stepCount = 0; // number of steps the motor has taken void setup() { - // nothing to do inside the setup + // uncomment this line if using a motor with an 8-beat pattern, + // like the 28BY-J-48 4-wire motor. + // stepper.setBeatsPerPattern(8); } void loop() { diff --git a/src/Stepper.cpp b/src/Stepper.cpp index 1f76295..effa938 100644 --- a/src/Stepper.cpp +++ b/src/Stepper.cpp @@ -53,7 +53,8 @@ * 9 1 0 1 0 1 * 10 0 0 1 0 1 * - * The sequence of control signals for 4 control wires is as follows: + * There are two possible sequences for 4 control wires. + * Stepper pattern with four beats (the default) is as follows: * * Step C0 C1 C2 C3 * 1 1 0 1 0 @@ -61,6 +62,18 @@ * 3 0 1 0 1 * 4 1 0 0 1 * + * Stepper pattern with eight half steps is as follows: + * + * Step C0 C1 C2 C3 + * 1 1 0 0 0 + * 2 1 1 0 0 + * 3 0 1 0 0 + * 4 0 1 1 0 + * 5 0 0 1 0 + * 6 0 0 1 1 + * 7 0 0 0 1 + * 8 1 0 0 1 + * * The sequence of controls signals for 2 control wires is as follows * (columns C1 and C2 from above): * @@ -88,6 +101,7 @@ Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2) this->direction = 0; // motor direction this->last_step_time = 0; // time stamp in us of the last step taken this->number_of_steps = number_of_steps; // total number of steps for this motor + this->beats_per_pattern = 2; // Arduino pins for the motor control connection: this->motor_pin_1 = motor_pin_1; @@ -118,6 +132,7 @@ Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, this->direction = 0; // motor direction this->last_step_time = 0; // time stamp in us of the last step taken this->number_of_steps = number_of_steps; // total number of steps for this motor + this->beats_per_pattern = 4; // Arduino pins for the motor control connection: this->motor_pin_1 = motor_pin_1; @@ -150,6 +165,7 @@ Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, this->direction = 0; // motor direction this->last_step_time = 0; // time stamp in us of the last step taken this->number_of_steps = number_of_steps; // total number of steps for this motor + this->beats_per_pattern = 10; // Arduino pins for the motor control connection: this->motor_pin_1 = motor_pin_1; @@ -169,6 +185,21 @@ Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, this->pin_count = 5; } +/* + * Select the number of beats in the stepper motor pattern. + * For four-wire motors, this can be either four or eight. + * For two and five wire motors, the number of beats cannot be changed. + */ + +void Stepper::setBeatsPerPattern( int beats ) +{ + if(pin_count==4 && (beats==4) || (beats==8)) { + beats_per_pattern = beats; + } else { + // Illegal combination + } +} + /* * Sets the speed in revs per minute */ @@ -218,10 +249,7 @@ void Stepper::step(int steps_to_move) // decrement the steps left: steps_left--; // step the motor to step number 0, 1, ..., {3 or 10} - if (this->pin_count == 5) - stepMotor(this->step_number % 10); - else - stepMotor(this->step_number % 4); + stepMotor(this->step_number % this->beats_per_pattern); } } } @@ -251,7 +279,7 @@ void Stepper::stepMotor(int thisStep) break; } } - if (this->pin_count == 4) { + if (this->pin_count == 4 && this->beats_per_pattern==4) { switch (thisStep) { case 0: // 1010 digitalWrite(motor_pin_1, HIGH); @@ -280,6 +308,59 @@ void Stepper::stepMotor(int thisStep) } } + if (this->pin_count == 4 && this->beats_per_pattern==8) { + switch (thisStep) { + case 0: + digitalWrite(motor_pin_1, HIGH); + digitalWrite(motor_pin_2, LOW); + digitalWrite(motor_pin_3, LOW); + digitalWrite(motor_pin_4, LOW); + break; + case 1: + digitalWrite(motor_pin_1, HIGH); + digitalWrite(motor_pin_2, HIGH); + digitalWrite(motor_pin_3, LOW); + digitalWrite(motor_pin_4, LOW); + break; + case 2: + digitalWrite(motor_pin_1, LOW); + digitalWrite(motor_pin_2, HIGH); + digitalWrite(motor_pin_3, LOW); + digitalWrite(motor_pin_4, LOW); + break; + case 3: + digitalWrite(motor_pin_1, LOW); + digitalWrite(motor_pin_2, HIGH); + digitalWrite(motor_pin_3, HIGH); + digitalWrite(motor_pin_4, LOW); + break; + case 4: + digitalWrite(motor_pin_1, LOW); + digitalWrite(motor_pin_2, LOW); + digitalWrite(motor_pin_3, HIGH); + digitalWrite(motor_pin_4, LOW); + break; + case 5: + digitalWrite(motor_pin_1, LOW); + digitalWrite(motor_pin_2, LOW); + digitalWrite(motor_pin_3, HIGH); + digitalWrite(motor_pin_4, HIGH);; + break; + case 6: + digitalWrite(motor_pin_1, LOW); + digitalWrite(motor_pin_2, LOW); + digitalWrite(motor_pin_3, LOW); + digitalWrite(motor_pin_4, HIGH); + break; + case 7: + digitalWrite(motor_pin_1, HIGH); + digitalWrite(motor_pin_2, LOW); + digitalWrite(motor_pin_3, LOW); + digitalWrite(motor_pin_4, HIGH); + break; + } + } + if (this->pin_count == 5) { switch (thisStep) { case 0: // 01101 @@ -361,5 +442,5 @@ void Stepper::stepMotor(int thisStep) */ int Stepper::version(void) { - return 5; + return 6; } diff --git a/src/Stepper.h b/src/Stepper.h index 2e68979..8147206 100644 --- a/src/Stepper.h +++ b/src/Stepper.h @@ -53,7 +53,8 @@ * 9 1 0 1 0 1 * 10 0 0 1 0 1 * - * The sequence of control signals for 4 control wires is as follows: + * There are two possible sequences for 4 control wires. + * Stepper pattern with four beats (the default) is as follows: * * Step C0 C1 C2 C3 * 1 1 0 1 0 @@ -61,6 +62,18 @@ * 3 0 1 0 1 * 4 1 0 0 1 * + * Stepper pattern with eight half steps is as follows: + * + * Step C0 C1 C2 C3 + * 1 1 0 0 0 + * 2 1 1 0 0 + * 3 0 1 0 0 + * 4 0 1 1 0 + * 5 0 0 1 0 + * 6 0 0 1 1 + * 7 0 0 0 1 + * 8 1 0 0 1 + * * The sequence of controls signals for 2 control wires is as follows * (columns C1 and C2 from above): * @@ -90,6 +103,9 @@ class Stepper { int motor_pin_3, int motor_pin_4, int motor_pin_5); + // four-wire motors can have either four or eight beats + void setBeatsPerPattern( int beats ); + // speed setter method: void setSpeed(long whatSpeed); @@ -106,6 +122,7 @@ class Stepper { int number_of_steps; // total number of steps this motor can take int pin_count; // how many pins are in use. int step_number; // which step the motor is on + int beats_per_pattern; // number of beats in the motor pattern // motor pin numbers: int motor_pin_1; @@ -118,4 +135,3 @@ class Stepper { }; #endif -