AccelStepper library will run a function only once

I am getting signals from a PS3 controller via USB host shield. I want to move a stepper a determined number of steps when I press a button in the console (See if (PS3.getButtonClick(UP)) " .
Said and done, it worked fine, but for only ONE time. Then I have to re-boot the MEGA and will work again. I am sure the "UP" button is received the second or third time... But the ElbowStepper wont execute again.

Please help

mitch

ArmdroidMega03AccelStepper.ino.ino (6.4 KB)

ArmdroidMega05_MultiSteppers.ino.ino (9.08 KB)

Well, you run to position -1500 when you press the button.
With every successive time, you run to the same position!

What you should do is:

if (PS3.getButtonClick(UP)) {
  Serial.print(F("\r\nUp"));
   ElbowStepper.move(-1500);//Relative, not absolute. "moveTo()" is absolute.
   ElbowStepper.runToPosition();//AccelStepper has this handy function!
}

It worked, you're a genius. Thanks a lot.
I also tried it without the ElbowStepper.setAcceleration(1000); in the setup. It would not move...

Is there a way to just make it go at constant speed for a number of steps?

Tnks

Yes!
Instead of "runToPosition()", call "runSpeedToPosition!"

Thanks again for the quick reply.
What you said, did not work, here is what I used

if (PS3.getButtonClick(UP)) {
Serial.print(F("\r\nUp"));
ElbowStepper.move(-1500);
ElbowStepper.runSpeedToPosition();
// ElbowStepper.runToPosition(); //if you want to use accelleration
}

When I press the UP, I only get one pulse instead of 1500

Thanks

I was also hoping to move 2 steppers at the same time.

I tried:

else if (PS3.getButtonClick(LEFT)){
WristRotRStepper.move(-StepsToTravel);
WristRotLStepper.move(-StepsToTravel);
WristRotRStepper.runToPosition(); //if you want to use accelleration
WristRotLStepper.runToPosition(); //if you want to use accelleration
}
It does the first , stops, than the second. Any ideas?

Thanks

The reason it ones one then the other is because "runToPosition" is a blocking function -- this means it stops program execution until it is done.

If you want both to move similtaneously, you have to options:

---- Option 1 ----

If you do not need acceleration, use the MultiStepper library (if you downloaded the AccelStepper library of their website, you should already have it installed, too!)

    else   if (PS3.getButtonClick(LEFT)){
        long pos[2]={-StepsToTravel,-StepsToTravel};
        pair.move(pos);//Look into the multistepper library for how to set this up!
        pair.runSpeedToPosition();
     }

The multistepper library is really handy. If you need to move motors in a more coordinated way, I suggest you use it! :slight_smile:

---- Option 2 ----

If acceleration is important, you'll have to take a slightly different approach.

bool rotRReached,rotLReached;
do{
  rotRReached=WristRotRStepper.run();
  rotLReached=WristRotLStepper.run();
}while(!(rotRReached&&rotLReached))

This will run the steppers together! Note that this will only work as expected if both motors travel the same distance. If one has shorter to go, It'll get there and stop while the other is still running. They'll both get to where they need to go, but the movemont would be strange :confused:

Thanks a lot , I chose option one. Worked fine in a separate sketch but does not trigger under conditional "left"

Here is what I got, but I attached the entire sketch above.

else if (PS3.getButtonClick(LEFT)){
Serial.print(F("\r\nLeft"));
long positions[2]; // Array of desired stepper positions
positions[0] = -3000;
positions[1] = 3000;
steppers.moveTo(positions);
steppers.runSpeedToPosition(); // Blocks until all are in position
}

More than that , hitting "left" hangs the processor, and I have to reset, and reconnect bluetooth.

Please help.

TNKS

You need to use "move" not "moveTo."
Did you make sure to specify the max speed of the both motors using "setMaxSpeed"? If not, they will not move, hanging to processor.

Give it a shot! :slight_smile:

It would not take it, wont compile,

^
exit status 1
'class MultiStepper' has no member named 'move'

However the "move" is red, ?!?!

here is the whole condition

if (PS3.getButtonClick(LEFT)){
Serial.print(F("\r\nLeft"));
long positions[2]; // Array of desired stepper positions
positions[0] = -3000;
positions[1] = 3000;
steppers.move(positions);
steppers.runSpeedToPosition(); // Blocks until all are in position
}

thanks

PS

Yes, in the setup i got

WristRotRStepper.setMaxSpeed(1000);
WristRotLStepper.setMaxSpeed(1000);

steppers.addStepper(WristRotRStepper);
steppers.addStepper(WristRotLStepper);

Fuzz, I hope you did not give up on me....

I tried the other option, with accelleration, nothing moves at all...

else if (PS3.getButtonClick(LEFT)){
Serial.print(F("\r\nLeft"));
WristRotLStepper.move(-StepsToTravel);
WristRotRStepper.move(-StepsToTravel);
bool rotRReached,rotLReached;
do{
rotRReached=WristRotRStepper.run();
rotLReached=WristRotLStepper.run();
}while(!(rotRReached&&rotLReached));
}

Please help,

Thank you , mitch

Hmmm...
I do not know why this is the case! I'm a bit rusty on AccelStepper and MultiStepper...

It appears MultiStepper only accepts absolute positioning, this should help.

 if (PS3.getButtonClick(LEFT)){
         Serial.print(F("\r\nLeft"));
         long positions[2]; // Array of desired stepper positions
         positions[0] = -3000+WristRotRStepper.currentPosition();
         positions[1] = 3000+WristRotLStepper.currentPosition();;
         steppers.move(positions);
         steppers.runSpeedToPosition(); // Blocks until all are in position
     }

You may also have to define the individual motor's acceleration, even though it has no effect when dealing with multisteppers.

Thanks for trying, but "move" is not accepted under the Multistepper.h. Only Accellstepper has a as a member.