I have this machine to cut paracord but I had to change some of the electronics and thus the code. Now, it doesn't seem to work. The servo motor will move properly but I can't get the stepper to move. I am using a DM332T stepper motor driver on a nema 17 motor. The schematic of my electronics is below.
#include <AccelStepper.h>
#include <Servo.h>
const byte buttonPin = 5; // the pin to which the pushbutton is attached
//system states
#define runMotors 1
#define endCycle 2
#define coolingPeriod 3
#define stopped 4
byte currentState = stopped;
unsigned long coolingPeriodStartTime;
const unsigned int coolingPeriodDuration = 2000;
const int stepsPerRevolution = 500; // new number of steps per revolution
Servo myservo; // create servo object to control a servo
int pos = 20; // starting position is not top dead center
// initialize the AccelStepper library on the new direction and step pins:
AccelStepper myStepper(AccelStepper::DRIVER, 9, 8);
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
// set the maximum speed at 30 rpm:
myStepper.setMaxSpeed(30);
myservo.write(pos); // start position for servo
myservo.attach(7); // attaches the servo on pin 7 to the servo object
}
void loop() {
switch (currentState) {
case stopped:
checkButton();
break;
case runMotors:
Serial.println("runMotors");
myStepper.runSpeed();
delay(200);
for (pos = 20; pos <= 110; pos += 1) { // goes from 20 degrees to 110 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(100); // waits 100ms for the servo to reach the position
}
for (pos = 110; pos >= 20; pos -= 1) { // goes from 110 degrees to 20 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 5ms for the servo to reach the position
}
currentState = endCycle;
break;
case endCycle:
coolingPeriodStartTime = millis();
digitalWrite(13, HIGH); //indicate start of 4 second period
currentState = coolingPeriod;
Serial.println("coolingPeriod");
break;
case coolingPeriod:
checkButton();
//restart after cooling delay if button not pressed to stop cycle
if (millis() - coolingPeriodStartTime >= coolingPeriodDuration)
{
currentState = runMotors;
digitalWrite(13, LOW);
}
break;
}
}
void checkButton()
{
const byte debouncePeriod = 50;// 50 ms debounce
static byte oldButtonState = HIGH;
byte buttonState = digitalRead(buttonPin); //read push button state
if ((buttonState == LOW) && (oldButtonState == HIGH)) // check is button pressed and is changed using logic for input pullup
{
delay(debouncePeriod); //blocking debounce routine using delay
buttonState = digitalRead(buttonPin); //read button again
if ( buttonState == LOW) //still LOW
{
if (currentState == stopped)
{
//Serial.println("runMotors");
currentState = runMotors;
}
else
{
Serial.println("stop");
currentState = stopped;
digitalWrite(13, LOW);
}
}
}
oldButtonState = buttonState;
}
I had to change some of the electronics and thus the code. Now, it doesn't seem to work. The servo motor will move properly but I can't get the stepper to move. I am using a DM332T stepper motor driver on a nema 17 motor.
I read the article you posted. I have made changes to the code to what I think the library says I should have but it still doesn't work.
Here is the new code:
#include <AccelStepper.h>
#include <Servo.h>
#define runMotors 1
#define endCycle 2
#define coolingPeriod 3
#define stopped 4
#define dirPin = 8
#define stepPin = 9
byte currentState = stopped;
unsigned long coolingPeriodStartTime;
const unsigned int coolingPeriodDuration = 2000;
const byte buttonPin = 5; // the pin to which the pushbutton is attached
Servo myservo; // create servo object to control a servo
AccelStepper myStepper(AccelStepper::DRIVER, 9, 8);// initialize the AccelStepper library on the new direction and step pins:
int pos = 20; // starting position is not top dead center
int numberOfSteps = 515;
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
myStepper.setMaxSpeed(100);
myStepper.setSpeed(30);
myservo.write(pos); // start position for servo
myservo.attach(7); // attaches the servo on pin 7 to the servo object
}
void loop() {
switch (currentState) {
case stopped:
checkButton();
break;
Serial.println("runMotors");
myStepper.move(numberOfSteps);
delay(200);
for (pos = 20; pos <= 110; pos += 1) { // goes from 20 degrees to 110 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(100); // waits 100ms for the servo to reach the position
}
for (pos = 110; pos >= 20; pos -= 1) { // goes from 110 degrees to 20 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 5ms for the servo to reach the position
}
currentState = endCycle;
break;
case endCycle:
coolingPeriodStartTime = millis();
digitalWrite(13, HIGH); //indicate start of 4 second period
currentState = coolingPeriod;
Serial.println("coolingPeriod");
break;
case coolingPeriod:
checkButton();
//restart after cooling delay if button not pressed to stop cycle
if (millis() - coolingPeriodStartTime >= coolingPeriodDuration)
{
currentState = runMotors;
digitalWrite(13, LOW);
}
break;
}
}
void checkButton()
{
const byte debouncePeriod = 50;// 50 ms debounce
static byte oldButtonState = HIGH;
byte buttonState = digitalRead(buttonPin); //read push button state
if ((buttonState == LOW) && (oldButtonState == HIGH)) // check is button pressed and is changed using logic for input pullup
{
delay(debouncePeriod); //blocking debounce routine using delay
buttonState = digitalRead(buttonPin); //read button again
if ( buttonState == LOW) //still LOW
{
if (currentState == stopped)
{
//Serial.println("runMotors");
currentState = runMotors;
}
else
{
Serial.println("stop");
currentState = stopped;
digitalWrite(13, LOW);
}
}
}
oldButtonState = buttonState;
}
I did that and I'm thinking there is something wrong with the way I have the driver hooked up.
The button works, the servo works and repeats, but the stepper motor just doesn't turn.
This program is on a machine that I have created to cut paracord. The stepper turns 515 steps to feed 13 inches of paracord, the servo pushes the paracord down on a hot knife and goes back up.
Then the process starts all over and over and over until I turn it off. My first system worked but it overheated with what I was using. I had to upgrade to a better stepper motor driver. But that changed the code. (I was using the simple standard stepper rotation from the examples library)