Request to take a look at an existing code (Nema 17 forward and reverse control)

Good Evening. A year or so ago I paid someone on fiverr ( I cant access the account now) to code this for me as I wanted to control a nema with forward , reverse and at an adjustable speed. I never got around to finishing the project past getting the code I'm looking at continuing the project now and Ive set up the driver (tb6600), motor (nema 17) and switch and can run the code with some success. My only problem is that I cant figure out how to change the speed that the motor turns. Ive adjusted delayMicroseconds(500); which I thought was what I was instructed to do, but the motor starts to stall at delayMicroseconds(300); Ive had advice that i'd need to use accelstepper but I have zero coding knowledge above basic LED tutorials . Could someone quote me to take a look at the code below and come up with a solution. Thanks for a quick background, this will control a carriage mounted in a 10m ceiling track that will take a target to any point on the track by holding the forward or reverse button on the rocker switch

/*Description:
  This project is about the control a nema 17 stepper motor, simple forward and reverse with a rocker switch, at and adjustable speed.

   Components:
   - The driver is a tb6600
   - an Arduino nano for the controller
   - and a Nema 17 stepper.
   - rocker switch
   Circuit:
     Arduino        Component
        11  ------->  button forward
        12  ------->  button reverse
        3  ------->  step pin driver tb6600
        2  ------->  dir pin driver tb6600
        4  ------->  en pin driver tb6600

*/

//Pins definitions
#define forward 11         // button forward pin
#define reverse 12         //button reverse pin
#define stepPin 3         // STEP+ pin
#define dirPin 2         // DIR+ pin
#define enPin 4          // ENABLE+ pin

//Declare variables
boolean stateForward;   //0--> motor off /1--> motor forward
boolean stateReverse;   //0--> motor off /1--> motor reverse
int Speed = 500;       //speed of motor

void setup() {
  //pins configuration
  pinMode(forward, INPUT_PULLUP);
  pinMode(reverse, INPUT_PULLUP);

  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(enPin, OUTPUT);

  digitalWrite(enPin, HIGH);  //disable the driver

  Serial.begin(9600);
}

void loop() {
  if (digitalRead(forward) == LOW) {  //if the button is pressed
    digitalWrite(enPin, LOW);  //enable the driver
    Serial.print("Forward");

    //Enables the motor direction to move
    digitalWrite(dirPin, HIGH);
    //Makes 200 Pulses for making one full cycle rotation
    for (int x = 0; x < 500; x++) {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(500);
    }
    digitalWrite(enPin, HIGH);  //disable the driver
  }



  if (digitalRead(reverse) == LOW) {  //if the button is pressed
    Serial.print("Reverse");
    digitalWrite(enPin, LOW);  //enable the driver
    //Changes the rotations direction
    digitalWrite(dirPin, LOW);
    // Makes 200 pulses for making one full cycle rotation
    for (int x = 0; x < 500; x++) {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(500);
    }
    digitalWrite(enPin, HIGH);  //disable the driver
  }


}

In order to run a motor, the power supply must be able to overcome the inertia of the motor. Your motor is receiving the pulses too fast for it to get up to speed. There are two basic ways of solving this:

1) Use a higher voltage power supply: allows a higher starting torque to overcome inertia by producing more current.

2) Accelerate the motor to speed: allows a higher starting torque because at low step rates, the current is higher (oversimplifying).

1 requires changing your power supply. #2 can be done without changing hardware, but depending on the power supply you have now, it may not be effective enough.

Q: what kind of power supply are you using and do you have a link to the motor?

Hi. Thanks for your reply.

The motor i’m using is this

and power supply wise, Im using a 12v 30A 3d printer power supply or a 6s 21v li-ion battery.

The TB6600 driver is set to 1.5a (1.7a peak)

Would adding in accelstepper in overcome the problem? Thanks again

How heavy is the load? i.e., can you easily move it by hand? I can send you a short demo program that can demonstrate how accelstepper is used, but I don't recall if I wrote it for the TB6612 or a different driver. Need to check.

That would be great. The load is very light. (A wheeled trolly supporting a piece of paper)

Thanks

OK. You certainly should have enough oomph: that's barely more than the motor itself. Intall the AccellStepper library and try loading the "bounce.pde" demo in the AccelStepper examples folder. If your TB6612 is already wired correctly, and it sounds like it is, the default configuration of the demo should be fine.

Dynamic problems like this are hard to debug remotely (I've had motion problems resolved by counterintuitively adding a little extra load to the motor) but that should give you a good start.

Thats awesome. Thanks

Accelstepper seems to be the answer

That seems to have sorted the stalling issue . I've managed to cobble together a code that works , moving the target from home to a set point and back again when a forward and reverse buttons are pressed.

I just need to figure out how to have it move when the button is pressed and held and stop when released. Any ideas on that?

Your help is massively appreciated.

// Include the AccelStepper library:
#include 

long stepsPerRev = 200;    //200 steps per stepper motor revolution
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#define forward 11         // button forward pin
#define reverse 12         //button reverse pin
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1
//#define home_switch 9 // Pin 9 connected to Home Switch (MicroSwitch)
#define enPin 4          // ENABLE+ pin
//Declare variables
boolean stateForward;   //0--> motor off /1--> motor forward
boolean stateReverse;   //0--> motor off /1--> motor reverse
int direction;    // Variable to set Rotation (CW-CCW) of the motor
//int steps;        // Used to set HOME position after Homing is completed
// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

void setup() {
  //pins configuration
  pinMode(forward, INPUT_PULLUP);
  pinMode(reverse, INPUT_PULLUP);
  digitalWrite(enPin, HIGH);  //disable the driver



  // Set the maximum speed and acceleration:
  stepper.setMaxSpeed(5000);
  stepper.setAcceleration(1000);
}

void loop() {
  if (digitalRead(forward) == LOW) { //if the button is pressed
    digitalWrite(enPin, LOW);  //enable the driver
    //Enables the motor direction to move
    digitalWrite(dirPin, HIGH);
    // Set the target position:
    stepper.moveTo(25000);
    // Run to target position with set speed and acceleration/deceleration:
    stepper.runToPosition();
    //digitalWrite(enPin, HIGH);  //disable the driver

  }





  if (digitalRead(reverse) == LOW) { //if the button is pressed
    digitalWrite(enPin, LOW);  //enable the driver
    //Changes the rotations direction
    digitalWrite(dirPin, HIGH);
    // Move back to zero:
    stepper.moveTo(100);
    // Run to target position with set speed and acceleration/deceleration:
    stepper.runToPosition();
    //digitalWrite(enPin, HIGH);  //disable the driver
  }

}