Controlling multiple NEMA 23 motors with AccelStepper...

Greetings.

I am trying to run some test code, but it does not seem to be working. Currently, my NEMA23 motor will go through the first while loop in the setup function, but will not enter the second while loop occuring after… I would like my motor to turn so many steps in one direction, and then so many steps in the other.

All help would be much appreciated.

/*
NEMA23 Stepper Motor Code
*/
 
#include <AccelStepper.h>
 
long receivedAcceleration = 0; //acceleration value from computer
long cushion = 0; //steps retracted from switch after homing
 
int switch1 = 10; // Pin 6 is input from switch to arduino
 
// direction --> digital pin 3, pulses --> digital pin 4
AccelStepper stepper(1, 4, 3);


 
void setup()
{
  
  //setting up some default values for maximum speed and maximum acceleration
  stepper.setMaxSpeed(1000); //SPEED = Steps / second
  stepper.setAcceleration(500); //ACCELERATION = Steps /(second)^2
  stepper.setCurrentPosition(0);
  cushion = 4000;
  
  while(2>1){
  stepper.moveTo(cushion); //set distance
  stepper.run();
  }

  delay(1);
  stepper.setCurrentPosition(0);
  
  while(2>1){
  stepper.moveTo(-1*cushion);
  stepper.run();
  }
  
}


void loop()
{

 
}

This WHILE will never end.

 while(2>1){

Try this

 stepper.moveTo(cushion); //set distance
  while (stepper.distanceToGo() > 0) }
      stepper.run();
  }

...R

Thank you for helping! But I am still running into issues with running my code properly.

I have a limit switch which is the condition for my first while loop. When my limit switch is closed, the first while loop will immediately halt and then should trigger the second while loop. However, this is not what is happening.

Currently, my motor will run and when the limit switch is hit, the motor will stop abruptly without continuing onto the next while loop in my code. Similarly, if my first while loop is ran till completion without the interference from the limit switch, the second while loop will not be initiated. Any thoughts?

/*
NEMA23 Stepper Motor Code
*/
 
#include <AccelStepper.h>

// direction --> digital pin 3, pulses --> digital pin 4
AccelStepper stepper(1, 4, 3);

long cushion = 0; //steps retracted from switch after homing
long homing = 0; //steps to home gantry
int switch1 = 10; // Pin 10 is input from limit switch to arduino



 
void setup()
{
  
  //setting up some default values for maximum speed and maximum acceleration
  stepper.setMaxSpeed(1000); //SPEED = Steps / second
  stepper.setAcceleration(500); //ACCELERATION = Steps /(second)^2
  stepper.setCurrentPosition(0);
  homing = 10000;

  stepper.moveTo(homing); //set distance
  while (digitalRead(switch1)) {
  stepper.run();
  }

  delay(1000);
  
  stepper.setCurrentPosition(0);
  cushion = 1000;
  
  stepper.moveTo(-1*cushion);
  while(stepper.distanceToGo() > 0){
  stepper.run();
  }
  
}


void loop()
{

 
}

Oh whoops, I see I should have adjusted my second while loop condition.

stepper.moveTo(-1*cushion);
while(stepper.distanceToGo() >= -1*cushion){
stepper.run();

It seems when I let me first while loop continue without interference from the limit switch, my motor will not proceed onto the second while loop after it has finished with the first. The only way for me to go onto the next while loop is by using the limit switch. Any ideas on why this is?

/*
NEMA23 Stepper Motor Code
*/
 
#include <AccelStepper.h>

// direction --> digital pin 3, pulses --> digital pin 4
AccelStepper stepper(1, 4, 3);

long cushion = 0; //steps retracted from switch after homing
long homing = 0; //steps to home gantry
int switch1 = 10; // Pin 10 is input from limit switch to arduino



 
void setup()
{
  
  //setting up some default values for maximum speed and maximum acceleration
  stepper.setMaxSpeed(1000); //SPEED = Steps / second
  stepper.setAcceleration(500); //ACCELERATION = Steps /(second)^2
  stepper.setCurrentPosition(0);
  homing = 2000;

  stepper.moveTo(homing); //set distance
  while (digitalRead(switch1)) {
  stepper.run();
  }

  delay(1000);
  
  stepper.setCurrentPosition(0);
  cushion = 1000;
  
  stepper.moveTo(-1*cushion);
  while(stepper.distanceToGo() >= -1*cushion){
  stepper.run();
  }
  
}


void loop()
{

 
}

I figured it out. My first while loop was completing, but staying in the loop. To fix that I just implemented a for loop with a break. Anyone have a better idea?

/*
NEMA23 Stepper Motor Code
*/
 
#include <AccelStepper.h>

// direction --> digital pin 3, pulses --> digital pin 4
AccelStepper stepper(1, 4, 3);

long cushion = 0; //steps retracted from switch after homing
long homing = 0; //steps to home gantry
int switch1 = 10; // Pin 10 is input from limit switch to arduino



 
void setup()
{
  
  //setting up some default values for maximum speed and maximum acceleration
  stepper.setMaxSpeed(1000); //SPEED = Steps / second
  stepper.setAcceleration(500); //ACCELERATION = Steps /(second)^2
  stepper.setCurrentPosition(0);
  homing = 2000;

  stepper.moveTo(homing); //set distance
  while (digitalRead(switch1)) {
  stepper.run();
  if(stepper.distanceToGo() == 0){
    break;
  }
  }

  delay(1000);
  
  stepper.setCurrentPosition(0);
  cushion = 1000;
  
  stepper.moveTo(-1*cushion);
  while(stepper.distanceToGo() >= -1*cushion){
  stepper.run();
  }
  
}

void loop()
{

 
}

Don't use while loops with AccelStepper.

Place run() in the loop() function so it runs often and repeatedly. Call moveTo() whenever you want to initiate a movement. The rest happens automatically.

MarkT:
Don't use while loops with AccelStepper.

In this case I think the OP is using them to get homing movement in the setup() function.

...R

Greetings.

I am trying to run two NEMA23 motors using two separate DM542S drivers running off the same power supply. The power supply is rated at 24 volts and 8.8 amps. The drivers are currently set at 2.37 A RMS with a peak value of 3.318 A. If both NEMA23’s are rated for 2.8 amps, and the peak current is equal to RMS*1.4… Should I reduce the current to the next setting? The next setting would be 1.91 A, which has a peak value (2.674 A) below the current rating for the NEMA23 (2.8 A).

Also, I have used both current outputs settings and achieved the same problem while running my motors. When I run both motors, one motor is much faster than the other although their coded to run at the same speed. Furthermore, if I run the slower motor at a much higher speed like 5000 steps per second whereas the other is 1000 steps per second, BOTH motors bug out and change their speed sporadically until my code finishes. I would like to run both motors at the same speed, but I am not sure what is to blame here… Any insight into my problem as well as to an explanation for the weird behavior when I increase the speed of my slower motor would be much appreciated.

/*
NEMA23 Stepper Motor Code
*/
 
#include <AccelStepper.h>

// direction --> digital pin 3, pulses --> digital pin 4
AccelStepper stepper1(1, 4, 3);
AccelStepper stepper2(2, 7, 6);

long cushion = 0; //steps retracted from switch after homing
long homing = 0; //steps to home gantry
int switch1 = 10; // Pin 10 is input from limit switch #1 to arduino
int switch2 = 11; // Pin 11 is input from limit switch #2 to arduino

void setup()
{
  
  //setting up some starting values for maximum speed and maximum acceleration during homing procedure
  stepper1.setMaxSpeed(1000); //SPEED = Steps / second
  stepper1.setAcceleration(1000); //ACCELERATION = Steps /(second)^2
  stepper1.setCurrentPosition(0);
  
  stepper2.setMaxSpeed(1000); //SPEED = Steps / second
  stepper2.setAcceleration(1000); //ACCELERATION = Steps /(second)^2
  stepper2.setCurrentPosition(0);
  // 
  
  homing = 5000; // Number of steps taken to find homing position -- This number should be large so as to always hit limit switches

  stepper1.moveTo(homing); //set distance
  stepper2.moveTo(homing);
  
  while (digitalRead(switch1) && digitalRead(switch2)) {
  stepper1.run();
  stepper2.run();
  
  if(stepper1.distanceToGo() == 0){
    break;
  }
  }

  delay(1000);
  cushion = 500;
  
  stepper1.setCurrentPosition(0);
  stepper2.setCurrentPosition(0);
  
  stepper1.moveTo(-1*cushion);
  stepper2.moveTo(-1*cushion);
  
  while(stepper1.distanceToGo() != 0 && stepper2.distanceToGo() != 0){
  stepper1.run();
  stepper2.run();
  }
  
}

void loop()
{

 
}

You already have another Thread about this - I recognised the code. I am suggesting to the Moderator to merge this with the other Thread. Double posting just wastes time

...R

Topics on the same subject merged

Duplicate posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Okay, but where can I find the duplicate post?

PneumaticNomad:
Okay, but where can I find the duplicate post?

It's now within this Thread.

...R

Well, my original post is about a single motor and doesn't include anything about the power supply and motor specifications. My newer post that is deemed a duplicate was about multiple motors running and not operating in sync with one another no matter my changes to the code. Yes, the code is similar, but built upon by solving the issues I was having with one motor in my first post, and then adding code for multiple stepper motors. In other words, I was trying to write about two different problems I'm having. How can I submit a post about my newer problems with syncing two motor without getting it sourced back to my older post with one motor?

Is the problem of controlling a single motor solved ?

Yes, it was solved. I posted my solutions to my problems above in case others ever need an answer. My current post that was merged with my older one was for two motors and concerns failing to sync the motors with the AccelStepper Library commands.

Yes, it was solved.

Good. Now use this topic to move on to your current problem with multiple motors building on the solution to the original problem