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

Support for CNC Shield #26

Open
wants to merge 4 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
74 changes: 74 additions & 0 deletions examples/stepper_cnc_shield/stepper_cnc_shield.ino
Original file line number Diff line number Diff line change
@@ -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);

}
}
}
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name=Stepper
version=1.1.3
version=1.2.0
author=Arduino
maintainer=Arduino <info@arduino.cc>
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
Expand Down
73 changes: 71 additions & 2 deletions src/Stepper.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
Expand All @@ -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
Expand All @@ -251,6 +306,7 @@ void Stepper::stepMotor(int thisStep)
break;
}
}

if (this->pin_count == 4) {
switch (thisStep) {
case 0: // 1010
Expand Down Expand Up @@ -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);
}
}

24 changes: 16 additions & 8 deletions src/Stepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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
Expand Down