Had to change my program

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;
}

Lanyard machine (1).pdf (6.4 KB)

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.

You have not correctly converted your previous code using Stepper.h to the present version using AccelStepper.h

Have you reviewed and run the library examples?

You are not using the correct AccelStepper library functions and the runMotors case will not run the stepper.

See this documentation for a review of the available functions.

https://hackaday.io/project/183279-accelstepper-the-missing-manual/details

Thank you. I will have to go back and look at that.

Your schematic is missing a 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;
}

Hi,

What do you mean?
What part doesn't work?
What is it supposed to do?

Have you got a simple code to JUST test the stepper?
If not, then write one and make sure your stepper circuit is working.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia: :santa:

First, run the simple library example for constant speed with your constructor and wiring and prove that you are wired and powered correctly.

Then, you can make these changes to your code.

  1. Give some reasonable value for acceleration to move from the very slow default value.
myStepper.setMaxSpeed(100);
myStepper.setSpeed(30);
myStepper.setAcceleration(15);
  1. Call the blocking function to run the stepper after you set the target position
Serial.println("runMotors");
myStepper.move(numberOfSteps);
myStepper.runToPosition();
delay(200);

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.

I did test just the stepper and it did not turn. Nothing! I think I have something hooked up wrong on the driver.

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)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.