Accelstepper run 2 stepper at the same time one infinite

Hi, forum.
Have searched the forum and online without success.
I need to control two steppers with accelstepper library.
While once has to go up and down with:
if (stepper1.distanceToGo() == 0) stepper1.moveTo(-stepper1.currentPosition());
stepper1.run();
and of course:
stepper2.run();
in a loop
the other one has to go infinitely in one direction.
Simply?
Not for me...
once the stepper2.move(10000); position has reached the motor stops.
If try to reset the position once arrived,
Since the driver controlling stepper2 has a builtin acceleration function implemented resetting the position will cause the stepper2 to decelerate and then accelerate again once it starts...
Is there a way to send stepper2.move() that does not freeze the whole process?'
bets regards and Happy Halloween..

Have you ever posted before? Please post your entire sketch, using code tags please. Nobody can just guess what you're doing...

void loop()
{
stepper1.moveTo(step_delta);
stepper2.move(10000);
do
{
if (stepper1.distanceToGo() == 0) stepper1.moveTo(-stepper1.currentPosition());
stepper1.run();
stepper2.run();
}
while (1);
}

The ask from @aarg was to post your entire sketch, that is, something we could compile.

That said, I find accelstepper confusing, but I think setSpeed is what you need for the infinite move. You may need runSpeed too, or maybe run will do it - can't tell from the docs.

Yes, we need to see the entire sketch. But the code that you have posted so far, is nonsense. You also did not use code tags as requested. Go look at the stepper library examples to see how it's done.

Hi it is simply taken and adapted from example.

// MultiStepper.pde
// -*- mode: C++ -*-
//
// Shows how to multiple simultaneous steppers
// Runs one stepper forwards and backwards, accelerating and decelerating
// at the limits. Runs other steppers at the same time
//
// Copyright (C) 2009 Mike McCauley
// $Id: MultiStepper.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $

#include <AccelStepper.h>

// Define some steppers and the pins the will use
AccelStepper stepper1(AccelStepper::FULL4WIRE, 0, 1, 2, 3); 
AccelStepper stepper2(AccelStepper::FULL4WIRE, 4, 5, 6, 7);

void setup()
{  
    stepper1.setMaxSpeed(2000.0);
    stepper1.setAcceleration(150.0);
    stepper1.moveTo(15000);
    delay(0);   
    stepper2.setMaxSpeed(2000.0);
    stepper2.moveTo(1500);
 
}

void loop()
{
if (stepper1.distanceToGo() == 0) stepper1.moveTo(-stepper1.currentPosition());
stepper1.run();
stepper2.run();
}

Once position 2 is reached the stepper2 goes off.
I have no way to keep it running continuously.
Regards

Did you try the suggestion in reply #4?
https://github.com/waspinator/AccelStepper/blob/master/examples/ConstantSpeed/ConstantSpeed.pde

For stepper2 call stepper2.runSpeed(); instead.

stepper2.move (2000000000L) ;

is probably good enough - at 4000 steps per second that's 140 hours or so.

Failing that perhaps just use this to keep it moving:

  if (stepper2.distanceToGo() < 1000000L)
    stepper2.move(1000000L) ;

Thanks MarkT.
Have by far tried all the solution proposed but seems this one is the only that works in my case.
One question I have to ask. Reaching maximum speed sometimes the rotor shaft blocks and the stepper keeps vibrating while locked at same frequency instead of turning.
I have tried increasing voltage and current but the problem remains the same.
And trying other things I have seen that the same happens not only at maximum speed... but also at lower ones
What it is caused by?
best regards and thanks

Houseman

That is a sign that the motor has stalled because you tried to make it go faster than it can or the load is too much for the motor. Were you using acceleration? At what step rate does this happen? Are you using microstepping? If so, what ratio?

That can be caused by resonance. What is the motor driving? Through gears, belt, lead screw?
Again what microstepping are you using? Micro stepping can relieve some of the resonance problems.

Another possibility, but more subtle and rare, at max speed, the frame may be vibrating and making some motor connection intermittently open.

Thanks groundFungus.
I think it is probably more a stall problem doe to an excessive load applied on the shaft. Since I need maximum velocity of the shaft I cannot utilize microsteps since I would loose speed. I think it is a torque problem. So increasing the input voltage should solve the problem..

Please help since I am really going mad...
Taken the Multistepper.pde code as reference and focusing on stepper1 movement (back and forth) the
"if (stepper1.distanceToGo() == 0) stepper1.moveTo(-stepper1.currentPosition()); "
statement is only valid if the movement is around zero.
Immagine in the setup of your sketch you send a stepper1 command to move forth +2000 steps. And from there (2000steps) not from 0 point you want to travel far a total of 30000 steps (+15000 -15000).
How would the if statement look like?
I made only this possible to reset stepper counts sending a new zero reference just before the loop statement with stepper1.setCurrentPosition(0L); command.
Now I ask is there another way to make this happen without resetting position with some if conditions?
Please enlighten me..
Best regards and thanks for the help

The moveTo() function is absolute. So if you want 30000 steps, 15000 then -15000 you would moveTo(15000) then moveTo(0).
Or as in your scenario, moveTo(2000), moveTo(17000), moveTo(2000).

The move() function is relative. 30000 steps would be move(15000) then move(-15000), which would return to 0.

Here is a demo program that uses an array of positions and an array of speeds for stepper movement. The sequence will repeat, endlessly. Maybe this will be helpful.

#include <AccelStepper.h>

const unsigned int NUM_STEPS = 6;

unsigned int xArray[NUM_STEPS] = {0, 900, 650, 400, 650, 100};
unsigned int xSpeeds[NUM_STEPS] = {200, 2000, 200, 100, 400, 50};

const byte enablePin = 8;  // needed for the CNC shield that I use for testing

AccelStepper x_stepper(AccelStepper::DRIVER, 2, 5);

void setup()
{
   Serial.begin(115200);
   pinMode(enablePin, OUTPUT);  // CNC shield
   digitalWrite(enablePin, LOW);  // CNC shield
   x_stepper.setAcceleration(2000);
   x_stepper.setMaxSpeed(200);
   x_stepper.setSpeed(200);
   x_stepper.setCurrentPosition(0);
}

void loop()
{
   static unsigned int index = 0;
   if (x_stepper.run() == 0)  // the previous move has finished
   {
      Serial.println(index);
      x_stepper.moveTo(xArray[index]);
      x_stepper.setMaxSpeed(xSpeeds[index]);
      index++;
      if (index >= NUM_STEPS)
      {
         index = 0;
      }
   }
}

And this combines the code from before that moves one stepper through a sequence with one that runs another stepper at a constant speed, forever.

#include <AccelStepper.h>

const unsigned int NUM_STEPS = 6;

unsigned int xArray[NUM_STEPS] = {0, 900, 650, 400, 650, 100};
unsigned int xSpeeds[NUM_STEPS] = {200, 2000, 200, 100, 400, 50};

const byte enablePin = 8;

AccelStepper x_stepper(AccelStepper::DRIVER, 2, 5);
AccelStepper y_stepper(AccelStepper::DRIVER, 3, 6);

void setup()
{
   Serial.begin(115200);
   pinMode(enablePin, OUTPUT);
   digitalWrite(enablePin, LOW);
   x_stepper.setAcceleration(2000);
   x_stepper.setMaxSpeed(200);
   x_stepper.setSpeed(200);
   x_stepper.setCurrentPosition(0);

   y_stepper.setMaxSpeed(2000);
   y_stepper.setSpeed(300);
}

void loop()
{
   static unsigned int index = 0;
   if (x_stepper.run() == 0)
   {
      Serial.println(index);
      x_stepper.moveTo(xArray[index]);
      x_stepper.setMaxSpeed(xSpeeds[index]);
      index++;
      if (index >= NUM_STEPS)
      {
         index = 0;
      }
   }
   y_stepper.runSpeed(); // run at 300 steps/second, forever
}