Trying to run Stepper Motor to certain position then change speed

#include <AccelStepper.h>
// Define some steppers and the pins the will use
AccelStepper stepper1(AccelStepper::FULL2WIRE, 2, 5);
//AccelStepper stepper2(AccelStepper::FULL2WIRE, 3, 6);

void setup()
{  
  Serial.begin(9600);
    stepper1.setMaxSpeed(3200.0);
    stepper1.setAcceleration(1200);
    stepper1.moveTo(1000);
}
void loop()
{
    stepper1.setSpeed(300);
    stepper1.moveTo(500);  
    stepper1.runSpeedToPosition();
    stepper1.setCurrentPosition(0);
    Serial.println("1");
    stepper1.setSpeed(3200);
    stepper1.moveTo(600);
    stepper1.runSpeedToPosition();
    stepper1.setCurrentPosition(0);
    Serial.println("2");
    stepper1.setSpeed(300);
    stepper1.moveTo(500);
    stepper1.runSpeedToPosition();
    
}

In theory this should just loop right? The speed is set at 300, it runs 500 steps. Then at 3200, runs 600 steps. Then back to 300 to run 500 steps. But for some reason the motor will not run this way. The serial.println's print though so it is executing the code.

Also if anyone has anywhere I can better learn accelstepper it'd be much appreciated as I don't seem to understand its library properly

But for some reason the motor will not run this way.

What does it do? We can not see what you have between the Arduino and the stepper motor(s). We can not see how you are powering the stepper motor(s).

Way more details are needed.

I don't immediately see anything wrong with the code (but I can be very bblind to silly mistakes). As @PaulS says, you need to tell us in detail what the program actually does.

Perhaps when you ask the motor to move at the higher speed from stopped it cannot do so without accelerating? Maybe you should use runToPosition().

Also, I'm not sure if runSpeedToPosition() blocks until it completes the move. runToPosition() does.

I assume you are content for the motor to stop between speed changes.

...R
Stepper Motor Basics
Simple Stepper Code

Powersupply: Eventek KPS3010D 30V 10A max

driver: MySweety TB6600 Rated for 4A max and stepping at 1600 steps per revolution

motor:https://www.amazon.com/gp/product/B00PNEPW4C/ref=ox_sc_act_title_1?smid=AWQBCGWISS7BL&psc=1

There's power draw, a whining noise, but the motor does not turn or do anything. In the pictures, the stepper motor is only the one with the white disk on the shaft. I have a second stepper motor and driver but that's not running right now. I'm trying to get the first one working first

Robin:
I think you are correct that runToPosition blocks which is why I used runSpeedToPosition
The motors do not move at all when I run the program which is why I'm confused

I have run the motors at 120 RPM or 3200step/second before without acceleration so that does not seem to be the problem. And yes, I am fine with a slight delay in between speed changes

Using ANY Stepper library, have you gotten the (those) motor(s) to turn? They look pretty big to be driving with a 4A limited stepper driver.

Yes

#include <AccelStepper.h>
// Define some steppers and the pins the will use
AccelStepper stepper1(AccelStepper::FULL2WIRE, 2, 5);
AccelStepper stepper2(AccelStepper::FULL2WIRE, 3, 6);

void setup()
{  
    stepper1.setMaxSpeed(3200.0);
    stepper1.setAcceleration(6400.0);
    stepper1.moveTo(100000);
    
    stepper2.setMaxSpeed(533);
    stepper2.setAcceleration(6400);
    stepper2.moveTo(1000000);
}
void loop()
{
    // Change direction at the limits
    if (stepper1.distanceToGo() == 0)
        stepper1.moveTo(-stepper1.currentPosition());
    stepper1.run();
    stepper2.run();
}

They run with this code perfectly fine, instantaneous speed, no acceleration required.

I've run them both at 120 RPM perfectly fine or 3200 steps/second

In that current setup the second one runs at 20 RPM

The motors are rated for 4.2A so it seemed fine to run with a 4A driver. And my powersupply is 10A and they're powered in parallel, so it seems fine.

Somark:
Robin:
I think you are correct that runToPosition blocks which is why I used runSpeedToPosition
The motors do not move at all when I run the program which is why I'm confused

What happens with this version of your program

#include <AccelStepper.h>
// Define some steppers and the pins the will use
// AccelStepper stepper1(AccelStepper::FULL2WIRE, 2, 5);
//AccelStepper stepper2(AccelStepper::FULL2WIRE, 3, 6);

AccelStepper stepper1(AccelStepper::DRIVER, 2, 5); // for a driver with step and direction

void setup()
{ 
  Serial.begin(9600);
    stepper1.setMaxSpeed(3200.0);
    stepper1.setAcceleration(1200);
    stepper1.moveTo(1000);
}
void loop()
{
    stepper1.setSpeed(300);
    stepper1.moveTo(500); 
    stepper1.runSpeedToPosition();
/*    
    stepper1.setCurrentPosition(0);
    Serial.println("1");
    stepper1.setSpeed(3200);
    stepper1.moveTo(600);
    stepper1.runSpeedToPosition();
    stepper1.setCurrentPosition(0);
    Serial.println("2");
    stepper1.setSpeed(300);
    stepper1.moveTo(500);
    stepper1.runSpeedToPosition();
*/
   
}

...R

Ahh, it worked for the code that was not in comments

It didn't work initially but you declared DRIVER and not FULL2WIRE. I am going to try and slowly add code till it fails

#include <AccelStepper.h>
// Define some steppers and the pins the will use
// AccelStepper stepper1(AccelStepper::FULL2WIRE, 2, 5);
//AccelStepper stepper2(AccelStepper::FULL2WIRE, 3, 6);

AccelStepper stepper1(AccelStepper::FULL2WIRE, 2, 5); // for a driver with step and direction

void setup()
{ 
  Serial.begin(9600);
    stepper1.setMaxSpeed(3200.0);
    stepper1.setAcceleration(1200);
    stepper1.moveTo(1000);
}
void loop()
{
  
    stepper1.setSpeed(300);
    stepper1.moveTo(500); 
    stepper1.runSpeedToPosition();
  
    if (stepper1.distanceToGo() == 0)
    {
    Serial.println("1");
    stepper1.setSpeed(3200);
    stepper1.moveTo(600);
    stepper1.runSpeedToPosition();
    }
  /*  stepper1.setCurrentPosition(0);
    Serial.println("2");
    stepper1.setSpeed(300);
    stepper1.moveTo(500);
    stepper1.runSpeedToPosition();
*/
   
}

I tried this code to force it to further run once the distance to go was zero. I'm having the same issue as last time where in the Serial Monitor, I see continuous 1's but no further movement from the motor other than the initial movement

#include <AccelStepper.h>
// Define some steppers and the pins the will use
// AccelStepper stepper1(AccelStepper::FULL2WIRE, 2, 5);
//AccelStepper stepper2(AccelStepper::FULL2WIRE, 3, 6);

AccelStepper stepper1(AccelStepper::FULL2WIRE, 2, 5); // for a driver with step and direction

void setup()
{ 
  Serial.begin(9600);
    stepper1.setMaxSpeed(3200.0);
    stepper1.setAcceleration(1200);
    stepper1.moveTo(1000);
}
void loop()
{
  
    stepper1.setSpeed(300);
    stepper1.moveTo(500); 
    stepper1.runSpeedToPosition();
    stepper1.setCurrentPosition(0);
   /* if (stepper1.distanceToGo() == 0)
    {
    Serial.println("1");
    stepper1.setSpeed(3200);
    stepper1.moveTo(600);
    stepper1.run();
    }
  /*  stepper1.setCurrentPosition(0);
    Serial.println("2");
    stepper1.setSpeed(300);
    stepper1.moveTo(500);
    stepper1.runSpeedToPosition();
*/
   
}

I then tried this, at that point the motor again refused to move when I added stepper.setCurrentPosition(0); perhaps running all of these commands in void loop() blocks the stepper motor and stops it from taking any further commands. Any suggestions for fixes?

Somark:
It didn't work initially but you declared DRIVER and not FULL2WIRE.

Post a link to the datasheet for your driver. I am amazed that the DRIVER option does not work - it is intended for stepper drivers. Have you correctly identified the step and direction connections?

Given the style of your program I don't understand why you are not using runToPosition().

The reason runSpeedToPosition() works in the shortened program is because it needs to be called many times to get the motor to the position - many more times than there are steps to move.

You could perhaps use stepper.distanceToGo() to check if it has reached its destination before you give it the new speed and number of steps. However the net effect would be the same as using runToPosition()

...R

That was the only guide I could find, the driver didnt come with a manual of any kind.

It appears we're thinking alike, after I posted my reply I started experimenting with runToPosition() it works, but it appears to reverse directions randomly and it doesn't appear as if I can alter the speed. I'm continuing to experiment and find a solution

#include <AccelStepper.h>
// Define some steppers and the pins the will use
// AccelStepper stepper1(AccelStepper::FULL2WIRE, 2, 5);
//AccelStepper stepper2(AccelStepper::FULL2WIRE, 3, 6);

AccelStepper stepper1(AccelStepper::FULL2WIRE, 2, 5); // for a driver with step and direction

void setup()
{ 
  Serial.begin(9600);
    stepper1.setMaxSpeed(3200.0);
    stepper1.setAcceleration(6400);
}
void loop()
{
  
    stepper1.setSpeed(300);
    stepper1.runToNewPosition(500); 
    stepper1.setSpeed(3200);
    stepper1.runToNewPosition(1100);
    stepper1.setSpeed(300);
    stepper1.runToNewPosition(1600);
  
 /*   stepper1.setCurrentPosition(0);
    Serial.println("1");
    stepper1.setSpeed(3200);
    stepper1.moveTo(600);
    stepper1.runSpeedToPosition();
    stepper1.setCurrentPosition(0);
    Serial.println("2");
    stepper1.setSpeed(300);
    stepper1.moveTo(500);
    stepper1.runSpeedToPosition();
*/
   
}

That driver should work with the AccelStepper DRIVER option. Actually, to be more direct, the DRIVER option is the proper choice for that driver.

Make a simple pencil drawing showing how you have everything connected and post a photo of the drawing. See this Simple Image Guide

...R

Never mind, dumb Q. :confused:

Here you go Robin, I don't have the Enable pin connected to anything as it seems to serve no purpose for me. but the numbers in that image are the pins everything is connected to. I looked up the difference between a common cathode and anode connection but it seems arduino has no preference so I just grounded all of the - pins together. Hope this helps you help me

Sorry but, you say 3200. Is that 3200pps or 3200rpm? If rpm, it's never going to happen. The fastest I've ever run a 23 is just over 2,000rpm and that speed was approached very slowly. Plus, you've got almost zero torque over about 600rpm. If you want high speed with a stepper, look to attaching a gear. Also, once you get above about 100rpm, micro-stepping is a waste of time.

That setup should work fine with the DRIVER option. Pin 2 is the step pin and Pin 5 is the direction pin.

I don't consider any of the other options to be appropriate.

...R

DKWatson:
Sorry but, you say 3200. Is that 3200pps or 3200rpm? If rpm, it's never going to happen. The fastest I've ever run a 23 is just over 2,000rpm and that speed was approached very slowly. Plus, you've got almost zero torque over about 600rpm. If you want high speed with a stepper, look to attaching a gear. Also, once you get above about 100rpm, micro-stepping is a waste of time.

No, just 120 RPM but it's 3200 steps/second. Most of the time one runs at 20 RPM and the other runs at 80 RPM so I just need high torque. I plan on not microstepping the higher RPM motor but I need the smoothness on the low RPM one.

Robin2:
That setup should work fine with the DRIVER option. Pin 2 is the step pin and Pin 5 is the direction pin.

I don't consider any of the other options to be appropriate.

alright, I'll give it a try again

I don't want to spam the forum and create a new post so I was hoping you could help me with this one too Robin

#include <AccelStepper.h>

AccelStepper stepper1(AccelStepper::FULL2WIRE, 2, 5);
AccelStepper stepper2(AccelStepper::FULL2WIRE, 3, 6);

int spd = 1000;    // The current speed in steps/second
int sign = 1;      // Either 1, or 0. Can use to change to reverse if sign= -1
String RPM=("RPM");
String Steps=("Steps per Second");
String disp=("Running at:"); //A display string so user knows input will be performed
String disp2=("Steps per Second:"); //Secondary Display String


void setup()
{  
  Serial.begin(9600);
  
  stepper1.setMaxSpeed(6400); //Setting max speed of 240 RPM if Steps are 400 On Off On
  stepper1.setAcceleration(6400); //Max acceleration
  stepper1.setSpeed(3200);    //Initial Starting Speed, not important 3200=120 RPM only for stepper1();
                              //Because of Step Rate of stepper1(); driver

  stepper2.setMaxSpeed(6400); //Setting max speed of 240 RPM if Steps are 400 On Off On
  stepper2.setAcceleration(6400); //Max acceleration
  stepper2.setSpeed(2133);    //Initial Starting Speed, not important, 2133=20 RPM only for stepper2();
                              //because of Step Rate of stepper1(); driver
  
  Serial.println("Initializing");
  Serial.println("Awaiting input. Entire desired RPM or 0 to stop");
}

void loop()
{ 
  int input; //Int to take converted input_String 
  String input_String; //String for receving Serial.read(); and converting to int value_RPM
  String num; //A display string so user knows input will be performed at num RPM
  
  if(Serial.available()){ //Making sure serial port is available before taking any action
    
    Serial.println("Received input of:"); //Letting user know input was received
    input_String = Serial.readString(); //Reading user input
    input = input_String.toInt(); //converting the input into a useable int
    Serial.println(input_String.toInt());  //Printing what int we got
    
    if (input >= 1000){ //This if only for stepper 2, makes sure that the input is over 1000 so we know
                        //User wants to modify stepper 2. Add 1000 to whatever RPM value you desire to control stepper 2
      Serial.println("Modifying speed of Stepper 2");
      input = input-1000; //Once confirmed that stepper 2 is being controlled, reduce to normal values and function normally
      if (input == 1) {  // Rotates forward, useless for now but if you want to add reverse functionality it is useful
      disp=("Rotating Forward");
      sign = 1;      
    }    
    else if (input == 0){  // stop. Once stopped, it will not restart      
      disp=("Rotation Stopping \n");
      sign = 0;
      num= "0";
      Serial.println("You have stopped the motor. To resume motion, reupload the program");   
    }   
    else{     
      spd=input/0.009375; //Ratio for OFF ON OFF S1 S2 S3 steps. Don't question it, this is different for
                        //stepper1(); as the steps/rev are different
      num=(spd);        //Convert spd back to string to display and manipulate easier   
    }
    stepper2.setSpeed(sign * spd); //Keep this for future reverse functionality
    Serial.println(disp + input + " " + RPM); //Final confirmation of action Arduino is taking
    Serial.println(disp2 + num + " " + Steps); //Displayed in steps per second   
    }  
   else{ //This else for only stepper 1  
    Serial.println("Modifying speed of Stepper 1");
    if (input == 1) {  // Rotates forward, useless for now but if you want to add reverse functionality it is useful
      disp=("Rotating Forward: ");
      sign = 1;    
    }   
      else if (input == 0){  // stop. Once stopped, it will not restart     
        disp=("Rotation Stopping \n");
        sign = 0;
        num= "0";
        Serial.println("You have stopped the motor. To resume motion, press the restart button");      
      }    
    else{    
      spd=input/0.0375; //Ratio for ON OFF ON S1 S2 S3 steps. Don't question it, this is different for
                        //The second motor as the steps/rev are different
      num=(spd);        //Convert spd back to string to display and manipulate easier    
    }    
    stepper1.setSpeed(sign * spd); //Keep this for future reverse functionality
    Serial.println(disp + input + " " + RPM); //Final confirmation of action Arduino is taking
    Serial.println(disp2 + num + " " + Steps); //Displayed in steps per second    
    }    
  }
  stepper1.runSpeed(); //Running arduino at desired speed
  stepper2.runSpeed();
}

For whatever reason, whenever I tell the motors to go to 0 RPM, or essentially stop, they refuse to ever raise their RPM ever again. Is it because the motors are trying to step but since their step speed is 0 they cant step? If so is there any workaround to this?

Also, again off topic but it seems that randomly the power to my motors will cut. As in they'll be chugging along at 200 RPM and after 2 minutes or so they lose power, their current draw drops to 0 and the only way to fix this is restart the power supply. Any suggestions there?

Update: I got it working to my wishes using runToNewPosition() and setCurrentPosition(0) thank you Robin, although I'd greatly appreciate any help you can offer me on my subsequent problems.