diff --git a/README.adoc b/README.adoc index 55b91a8..a098bc2 100644 --- a/README.adoc +++ b/README.adoc @@ -10,7 +10,7 @@ image:https://github.com/{repository-owner}/{repository-name}/actions/workflows/ This library allows you to control unipolar or bipolar stepper motors. To use it you will need a stepper motor, and the appropriate hardware to control it. For more information about this library please visit us at -http://www.arduino.cc/en/Reference/{repository-name} +http://www.arduino.cc/en/Reference/{repository-name} == License == diff --git a/src/Stepper.cpp b/src/Stepper.cpp index 148de03..2a2f3cd 100644 --- a/src/Stepper.cpp +++ b/src/Stepper.cpp @@ -169,6 +169,41 @@ Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, this->pin_count = 5; } +/*for 8 pins at once so that two motors at opposite directions can run parallelly at opposite direction + to create effects like front wheel drive or back wheel drive, since arduino has a single channel*/ +Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, + int motor_pin_3, int motor_pin_4, + int motor_pin_5, int motor_pin_6, + int motor_pin_7, int motor_pin_8) +{ + this->step_number = 0; // which step the motor is on + this->direction = 0; // motor direction + this->last_step_time = 0; // timestamp in us of the last step taken + this->number_of_steps = number_of_steps; // total number of steps for this motor + // Arduino pins for the motor control connection: + this->motor_pin_1 = motor_pin_1; + this->motor_pin_2 = motor_pin_2; + this->motor_pin_3 = motor_pin_3; + this->motor_pin_4 = motor_pin_4; + this->motor_pin_5 = motor_pin_5; + this->motor_pin_6 = motor_pin_6; + this->motor_pin_7 = motor_pin_7; + this->motor_pin_8 = motor_pin_8; + + // setup the pins on the microcontroller: + pinMode(this->motor_pin_1, OUTPUT); + pinMode(this->motor_pin_2, OUTPUT); + pinMode(this->motor_pin_3, OUTPUT); + pinMode(this->motor_pin_4, OUTPUT); + pinMode(this->motor_pin_5, OUTPUT); + pinMode(this->motor_pin_6, OUTPUT); + pinMode(this->motor_pin_7, OUTPUT); + pinMode(this->motor_pin_8, OUTPUT); + + // pin_count is used by the stepMotor() method: + this->pin_count = 8; +} + /* * Sets the speed in revs per minute */ @@ -282,6 +317,51 @@ void Stepper::stepMotor(int thisStep) } } + if (this->pin_count == 8) { + switch (thisStep) { + case 0: // 1010 + digitalWrite(motor_pin_1, HIGH); + digitalWrite(motor_pin_2, LOW); + digitalWrite(motor_pin_3, HIGH); + digitalWrite(motor_pin_4, LOW); + digitalWrite(motor_pin_5, LOW); + digitalWrite(motor_pin_6, HIGH); + digitalWrite(motor_pin_7, HIGH); + digitalWrite(motor_pin_8, LOW); + break; + case 1: // 0110 + digitalWrite(motor_pin_1, LOW); + digitalWrite(motor_pin_2, HIGH); + digitalWrite(motor_pin_3, HIGH); + digitalWrite(motor_pin_4, LOW); + digitalWrite(motor_pin_5, HIGH); + digitalWrite(motor_pin_6, LOW); + digitalWrite(motor_pin_7, HIGH); + digitalWrite(motor_pin_8, LOW); + break; + case 2: //0101 + digitalWrite(motor_pin_1, LOW); + digitalWrite(motor_pin_2, HIGH); + digitalWrite(motor_pin_3, LOW); + digitalWrite(motor_pin_4, HIGH); + digitalWrite(motor_pin_5, HIGH); + digitalWrite(motor_pin_6, LOW); + digitalWrite(motor_pin_7, LOW); + digitalWrite(motor_pin_8, HIGH); + break; + case 3: //1001 + digitalWrite(motor_pin_1, HIGH); + digitalWrite(motor_pin_2, LOW); + digitalWrite(motor_pin_3, LOW); + digitalWrite(motor_pin_4, HIGH); + digitalWrite(motor_pin_5, LOW); + digitalWrite(motor_pin_6, HIGH); + digitalWrite(motor_pin_7, LOW); + digitalWrite(motor_pin_8, HIGH); + break; + } + } + if (this->pin_count == 5) { switch (thisStep) { case 0: // 01101 @@ -364,4 +444,4 @@ void Stepper::stepMotor(int thisStep) int Stepper::version(void) { return 5; -} +} \ No newline at end of file diff --git a/src/Stepper.h b/src/Stepper.h index b5578b4..7873ab1 100644 --- a/src/Stepper.h +++ b/src/Stepper.h @@ -89,6 +89,10 @@ class Stepper { Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, int motor_pin_3, int motor_pin_4, int motor_pin_5); + Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, + int motor_pin_3, int motor_pin_4, + int motor_pin_5, int motor_pin_6, + int motor_pin_7, int motor_pin_8); // speed setter method: void setSpeed(long whatSpeed); @@ -113,9 +117,11 @@ class Stepper { int motor_pin_3; int motor_pin_4; int motor_pin_5; // Only 5 phase motor + int motor_pin_6; + int motor_pin_7; + int motor_pin_8; // Defined till 8 pins because 4-pin x2 unsigned long last_step_time; // timestamp in us of when the last step was taken }; #endif -