diff --git a/examples/stepper_cnc_shield/stepper_cnc_shield.ino b/examples/stepper_cnc_shield/stepper_cnc_shield.ino new file mode 100644 index 0000000..998add6 --- /dev/null +++ b/examples/stepper_cnc_shield/stepper_cnc_shield.ino @@ -0,0 +1,74 @@ +// From the pinout diagram + +// X: Step = 2, Direction = 5 +// Y: Step = 3, Direction = 6 +// Z: Step = 4, Direction = 7 + +byte enablePin = 8; +byte directionPin = 5; +byte stepPin = 2; +int numberOfSteps = 400; +int pulseWidthMicros = 8; // microseconds +int millisBetweenSteps = 8; // milliseconds + +long previousMillis = 0; +bool forward = true; +int stepCount = 0; +bool enabled = true; + +/********************** + void setup() - Initialisations + ***********************/ +void setup() { + + // Setup + + Serial.begin( 9600 ); + Serial.println("Starting StepperTest"); + + delay(2000); + + pinMode(directionPin, OUTPUT); + pinMode(stepPin, OUTPUT); + pinMode(enablePin, OUTPUT); + + // Initialise the step count + + int stepCount = 0; + +} + +void loop() { + + // Avoid using delay as this seems to break the servo + + unsigned long currentMillis = millis(); + + if (enabled == true) { + if (currentMillis - previousMillis > millisBetweenSteps) { + previousMillis = currentMillis; + + if (forward == true) { + stepCount = stepCount + 1; + if (stepCount > numberOfSteps) { + forward = false; + } + digitalWrite(directionPin, HIGH); + } + else + { + stepCount = stepCount - 1; + if (stepCount < 1) { + enabled = false; + digitalWrite(enablePin, HIGH); + } + digitalWrite(directionPin, LOW); + } + + digitalWrite(stepPin, HIGH); + //delayMicroseconds(pulseWidthMicros); // this line is probably unnecessary + digitalWrite(stepPin, LOW); + + } + } +} diff --git a/library.properties b/library.properties index 875067a..7e8ef17 100644 --- a/library.properties +++ b/library.properties @@ -1,8 +1,8 @@ name=Stepper -version=1.1.3 +version=1.2.0 author=Arduino maintainer=Arduino -sentence=Allows Arduino boards to control a variety of stepper motors. +sentence=Allows Arduino boards to control a variety of stepper motors and stepper shields. paragraph=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. category=Device Control url=http://www.arduino.cc/en/Reference/Stepper diff --git a/src/Stepper.cpp b/src/Stepper.cpp index 6361ce4..077805b 100644 --- a/src/Stepper.cpp +++ b/src/Stepper.cpp @@ -1,5 +1,5 @@ /* - * Stepper.cpp - Stepper library for Wiring/Arduino - Version 1.1.0 + * Stepper.cpp - Stepper library for Wiring/Arduino - Version 1.2.0 * * Original library (0.1) by Tom Igoe. * Two-wire modifications (0.2) by Sebastian Gassner @@ -8,6 +8,7 @@ * High-speed stepping mod by Eugene Kozlenko * Timer rollover fix by Eugene Kozlenko * Five phase five wire (1.1.0) by Ryan Orendorff + * CNC shields (1.2.0) by Jeremy Green * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -73,11 +74,48 @@ * The circuits can be found at * * https://docs.arduino.cc/learn/electronics/stepper-motors#circuit + * + * Add support to use CNC shields where only 2 pins are used axis and direction + * */ #include "Arduino.h" #include "Stepper.h" +/* + * three-wire constructor + * Sets which wires should control the motor. + */ +Stepper::Stepper(int number_of_steps, int axis_pin, int direction_pin, int enable_pin) +{ + this->step_number = 0; // which step the motor is on + 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 + + // Arduino pins for the motor control connection: + this->motor_pin_1 = axis_pin; + this->motor_pin_2 = direction_pin; + this->motor_pin_3 = enable_pin; + + // Indicate that a shield is being used + this->shield = shield; + + // setup the pins on the microcontroller: + pinMode(this->motor_pin_1, OUTPUT); + pinMode(this->motor_pin_2, OUTPUT); + pinMode(this->motor_pin_3, OUTPUT); + + // When there are only 3 pins, set the others to 0: + + this->motor_pin_4 = 0; + this->motor_pin_5 = 0; + + // pin_count is used by the stepMotor() method: + this->pin_count = 1; + +} + /* * two-wire constructor. * Sets which wires should control the motor. @@ -220,6 +258,8 @@ void Stepper::step(int steps_to_move) // step the motor to step number 0, 1, ..., {3 or 10} if (this->pin_count == 5) stepMotor(this->step_number % 10); + else if (this->pin_count == 1) + stepMotor(direction); else stepMotor(this->step_number % 4); } @@ -231,6 +271,21 @@ void Stepper::step(int steps_to_move) */ void Stepper::stepMotor(int thisStep) { + if (this->pin_count == 1) { + switch (thisStep) { + case 0: // 01 backwards + digitalWrite(motor_pin_1, HIGH); + digitalWrite(motor_pin_2, HIGH); + digitalWrite(motor_pin_1, LOW); + break; + case 1: // 00 forwards + digitalWrite(motor_pin_1, HIGH); + digitalWrite(motor_pin_2, LOW); + digitalWrite(motor_pin_1, LOW); + break; + } + } + if (this->pin_count == 2) { switch (thisStep) { case 0: // 01 @@ -251,6 +306,7 @@ void Stepper::stepMotor(int thisStep) break; } } + if (this->pin_count == 4) { switch (thisStep) { case 0: // 1010 @@ -361,5 +417,18 @@ void Stepper::stepMotor(int thisStep) */ int Stepper::version(void) { - return 5; + return 6; } + +/* + enable(bool) enables or disables all stepper + */ + void Stepper::enable(bool state){ + if (state == true){ + digitalWrite(motor_pin_3, LOW); + } + else { + digitalWrite(motor_pin_3, HIGH); + } + } + diff --git a/src/Stepper.h b/src/Stepper.h index f1241ef..a565357 100644 --- a/src/Stepper.h +++ b/src/Stepper.h @@ -8,6 +8,7 @@ * High-speed stepping mod by Eugene Kozlenko * Timer rollover fix by Eugene Kozlenko * Five phase five wire (1.1.0) by Ryan Orendorff + * CNC shields (1.2.0) by Jeremy Green * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -73,6 +74,9 @@ * The circuits can be found at * * https://docs.arduino.cc/learn/electronics/stepper-motors#circuit + * + * Add support to use CNC shields where only 2 pins are used axis and direction + * */ // ensure this library description is only included once @@ -83,6 +87,7 @@ class Stepper { public: // constructors: + Stepper(int number_of_steps, int axis_pin, int direction_pin, int enable_pin); Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2); Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, int motor_pin_3, int motor_pin_4); @@ -95,26 +100,29 @@ class Stepper { // mover method: void step(int number_of_steps); - + + void enable(bool state); + int version(void); private: void stepMotor(int this_step); - int direction; // Direction of rotation - unsigned long step_delay; // delay between steps, in ms, based on speed - 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 direction; // Direction of rotation + unsigned long step_delay; // delay between steps, in ms, based on speed + 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 // motor pin numbers: int motor_pin_1; int motor_pin_2; int motor_pin_3; int motor_pin_4; - int motor_pin_5; // Only 5 phase motor - + int motor_pin_5; // Only 5 phase motor + bool shield; // To support shields unsigned long last_step_time; // timestamp in us of when the last step was taken + }; #endif