Skip to content
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

Library Enhancement - 8-pin control for 2x 4-pin Stepper Motors #46

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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 ==

Expand Down
82 changes: 81 additions & 1 deletion src/Stepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -364,4 +444,4 @@ void Stepper::stepMotor(int thisStep)
int Stepper::version(void)
{
return 5;
}
}
8 changes: 7 additions & 1 deletion src/Stepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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

Loading