accelstepper help needed

#include <AccelStepper.h>

#define FULLSTEP 4
#define HALFSTEP 8
// motor pins
#define motorPin1  4     // Blue   - 28BYJ48 pin 1
#define motorPin2  5     // Pink   - 28BYJ48 pin 2
#define motorPin3  6     // Yellow - 28BYJ48 pin 3
#define motorPin4  7     // Orange - 28BYJ48 pin 4
                        // Red    - 28BYJ48 pin 5 (VCC)
                        
#define motorPin5  8     // Blue   - 28BYJ48 pin 1
#define motorPin6  9     // Pink   - 28BYJ48 pin 2
#define motorPin7  10    // Yellow - 28BYJ48 pin 3
#define motorPin8  11    // Orange - 28BYJ48 pin 4
                        // Red    - 28BYJ48 pin 5 (VCC)


AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
AccelStepper stepper2(HALFSTEP, motorPin5, motorPin7, motorPin6, motorPin8);

void setup()  
{
  stepper1.setMaxSpeed(2000.0);
  stepper1.setAcceleration(3000.0);
  stepper1.setSpeed(2000);
  
  stepper2.setMaxSpeed(1500.0);
  stepper2.setAcceleration(1000.0);
  stepper2.setSpeed(2500);
}


void loop()  
{

stepper1.moveTo(100);
if (stepper1.distanceToGo() == 0) 
stepper1.moveTo(200);
if (stepper1.distanceToGo() == 0) 
stepper1.moveTo(300);
if (stepper1.distanceToGo() == 0) 
stepper1.moveTo(400);
if (stepper1.distanceToGo() == 0) 
stepper1.moveTo(500);
if (stepper1.distanceToGo() == 0) 
 
stepper1.run();   

}

in the code i was expecting the stepper to move to 100, 200, 300, 400, 500 absolute positions, however, it doesn’t work…

I am just simply trying to make the stepper move to absolute positions that I write in the code.

also do i need this line of code? what’s its purpose??
if (stepper1.distanceToGo() == 0)

any help is greatly appreciated! :slight_smile:

I've never used accelStepper, but from the look of your code it will move to 100, then instantaneously move to 200, then 300 etc, without a pause in between individual movements, so it will appear to move directly from it's start position for 500 steps. (And it will keep repeating that indefinitely.) Edit: And I don't think they're absolute positions, I think they're relative to the start position. (But I might be wrong about that.) No, on closer inspection, because it's looping, it will start a movement, then loop until the distance to go is 0, then depending on where in the loop it is at that moment, it will begin another move. End result is similar to what I first described, but even more out of control. (I'm getting tired and slow. :( )

And this:-

if (stepper1.distanceToGo() == 0)

appears to be a check to see if the stepper has finished a move before continuing with the following statement.

this is so much more difficult than I thought it was going to be :( I bought 14 of these little motors, in hopes of controlling them freely, but it's not so easy lol

i basically want to control the motor for example

(in absolute position relative to the start position of the stepper)

50 steps forward

100 steps backward

1000 steps forward

400 steps backward

.........

it needs to move in specific amount as i need it

i also have to drive two of these,, so I hope I can use the code I have posted above

dovidu: in the code i was expecting the stepper to move to 100, 200, 300, 400, 500 absolute positions, however, it doesn't work...

What does happen?

Modify your code to print a debug message between each move - for example Serial.println("Starting 200");

Post a link to the datasheet for your motor.

...R Stepper Motor Basics

OldSteve: (I'm getting tired and slow. :( )

still better than me by lightyears haha

Robin's suggestion is a good one. I'd also suggest that you just play around a bit and get the feel of the various library functions.

All I know for sure is that using 'moveTo()', a positive value goes in one direction and a negative value goes in the other direction.

With a bit of testing/playing you'll get the hang of it.

Robin2: What does happen?

Modify your code to print a debug message between each move - for example Serial.println("Starting 200");

Post a link to the datasheet for your motor.

...R Stepper Motor Basics

it doesn't move at all :(

i don't know how to use the debug thingy... I 've never done that before.

it's for checking what's going on right??

Have you tested with the examples provided with the library? If so, did they work as expected?

OldSteve: Have you tested with the examples provided with the library? If so, did they work as expected?

i'm going to try that right now, although i think it doesn't have what i need it to do

dovidu: i'm going to try that right now, although i think it doesn't have what i need it to do

When trying out a library that you haven't used before, that's the very first thing that you should do. Study the examples, test the examples, try modifying them to ensure that the results are what you expected, then write your own code, using what you've learned.

There are no doubt plenty of examples online for using that library, too. Just take your time, and make sure you fully understand exactly how everything works.

Try this: http://42bots.com/tutorials/28byj-48-stepper-motor-with-uln2003-driver-and-arduino-uno/ which comes with some explanation about this motor and how to be driven by AccelStepper library.

If I am not mistaken, somebody has modified AccelStepper into a version which even should be more suitable for this special little motor.

#include <AccelStepper.h>

AccelStepper stepper1(AccelStepper::FULL4WIRE, 4, 6, 5, 7);
AccelStepper stepper2(AccelStepper::FULL4WIRE, 8, 10, 9, 11);

void setup()
{  
    stepper1.setMaxSpeed(2500.0);
    stepper1.setAcceleration(1500.0);

    stepper2.setMaxSpeed(2500.0);
    stepper2.setAcceleration(1500.0);
}

void loop()
{    
    stepper1.runToNewPosition(300);
    stepper2.runToNewPosition(300);
        
    stepper1.runToNewPosition(500);
    stepper2.runToNewPosition(500);
       
    stepper1.runToNewPosition(100);
    stepper2.runToNewPosition(100);
        
    stepper1.runToNewPosition(150);
    stepper2.runToNewPosition(150);     
}

yeah, so… basically with zero knowledge about driving steppers, thanks to all the folks’ input in my previous posts.

I barely managed to get full control of the steppers movement positions…

however, the stepper1 and stepper2 does not move simultaneously, which means wasted time for my application. so i need some inputs once again!

looking forward to your help :slight_smile:

Are your steppers always going to get identical instructions? If so just do it via hardware.

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks..Tom.. :)

The documentation says this about runToNewPosition(...):

"Moves the motor (with acceleration/deceleration) to the new target position and blocks until it is at position. "

The problem is the word "blocks". To move two motors virtually simultaneously, you will have to use one of the non-blocking functions (such as run()) in a loop.

You may want to look at the library source code for runToNewPosition(...) and do what it does but do it for two motors.

Why have you started a new Thread? Now we have to read 2 Threads to get all the info.

I have suggested to the Moderator to merge them.

...R