Motor do not spin continuously

Hello,

i search for your help with my little project.

I test many thinks but nothing help.

The process should be:

if buttonMat is pressed (low) the motor x move until buttonend is pressed (low) (buttonMat is still pressed)

after that motorx stop and motor y turn 360°. (cut material) after this the mat fall down and the buttonEnd is unpressed.

The process starts from beginn.

My problem is that motor x doesn’t move continuously. Its stucks randomly.

I use an arduino uno and a cnc shield v3. Stepper 42mm Kuman Motor 17HD48002H-22B

Can you find the error?

#include <AccelStepper.h>

// for the Arduino Uno + CNC shield V3 + A4988 + FL42STH47-1684A

#define MOTOR_X_ENABLE_PIN 8
#define MOTOR_X_STEP_PIN 2
#define MOTOR_X_DIR_PIN 5
//
#define MOTOR_Y_ENABLE_PIN 9//9
#define MOTOR_Y_STEP_PIN 3//3
#define MOTOR_Y_DIR_PIN 6//6

AccelStepper motor_X(1, MOTOR_X_STEP_PIN, MOTOR_X_DIR_PIN);
AccelStepper motor_Y(1, MOTOR_Y_STEP_PIN, MOTOR_Y_DIR_PIN);
//variable
int counter=0;

//def button
const int buttonMat = 12; //y-
const int buttonEnd = 11; //y+


void setup()
{
  motor_X.setEnablePin(MOTOR_X_ENABLE_PIN);
  motor_X.setPinsInverted(false, false, true);
  motor_X.setAcceleration(10000);  
  motor_X.setSpeed(200);
  motor_X.setMaxSpeed(300);
  //motor_X.enableOutputs();
  //motor_X.disableOutputs();

  //

  motor_Y.setEnablePin(MOTOR_Y_ENABLE_PIN);
  motor_Y.setPinsInverted(false, false, true);
  motor_Y.setAcceleration(10000);  
  motor_Y.setSpeed(500);
  motor_Y.setMaxSpeed(500);
  //motor_Y.enableOutputs();
  //motor_Y.disableOutputs();


  ///Def Serial Output
  Serial.begin(230400);

  //Def Button
  pinMode(buttonMat,INPUT_PULLUP);
  pinMode(buttonEnd,INPUT_PULLUP);
  
 
}

void loop()
{
 Serial.print("motor disable");
 Serial.println();
 delay(500);
 motor_Y.disableOutputs();
 motor_X.disableOutputs();
  
  while (digitalRead(buttonMat)==LOW )
  {
    if(digitalRead(buttonEnd)==HIGH)
    {
          Serial.print("material insert and move forward");
          Serial.println();
          
          forward();
    }
    else
    {
          Serial.print("material reach endstop");
          Serial.println();
          Serial.print("Caution start cutting in 2.5s");
          Serial.println();
          delay(2500);
          startCut();
          counter++;
          String strCounter = String(counter);
          Serial.print(strCounter + " " + "Parts cutted");
          Serial.println();
          delay(2500);
         
    }
  
  }
  
}
void startCut()
{

          Serial.print("Danger! Start CUT");
          Serial.println();
          
          motor_X.stop();
          
          motor_Y.enableOutputs();
          
          motor_Y.move(200);
          motor_Y.runToPosition();
         // motor_Y.stop();
          
}
void forward()
{
         motor_X.enableOutputs();
         motor_Y.disableOutputs(); 
         
         motor_X.move(1);
         motor_X.runToPosition();

        

}

ripp0r:
My problem is that motor x doesn't move continuously. Its stucks randomly.

Please describe that in a lot more detail.

What should happen if buttonMat is released?

The way your program is designed you are getting no value from the Accelstepper library and you might just as well control the motor without a library. Have a look at the examples in this Simple Stepper Code

...R
Stepper Motor Basics

The standard way to use AccelStepper is:

Only call setAcceleration and setMaxSpeed in setup().

Call run() in loop(), make sure its called often (dont use delay() anywhere).

use move/moveTo/stop as required in the rest of the code to initiate moves, or cancel them.
currentPostion, targetPosition, distanceToGo are all useful for monitoring the current situation.