Can anyone fix my code?

Hey im doing a small project with stepper motors and arduino uno. Im using the stepper motors to drive two wheels to complete track movements. The code below doesnt seem to be finishing. I did some tests of removing some track movements (i.e 1, 2, 3, 4 and 5 are the different track movements) to see what worked. Here’s a summary:

-Remove 1 & 3 = works
-Remove 3 = stops after 2
-Remove 1 = 2 works, then stutters twice in 3 then doesnt work
-Remove 1, 2 & 3 = works
-Remove 4 & 5 = stops after 2
-Remove all except 3 = works (i.e 3 alone works)

Hope this helps someone. I couldnt figure it out.

``````#include <AccelStepper.h>
// Define a stepper and the pins it will use
AccelStepper stepper1(1, 3, 2); // Left stepper motor with two output pins. 3=step, 2=direction
AccelStepper stepper2(1,5,4);   // Right stepper motor with two output pins. 5=step, 4=direction

//setup and initialize speeds and accelerations to be used
void setup()
{
stepper1.setSpeed(6400);
stepper2.setSpeed(6400);
stepper1.setAcceleration(300);
stepper2.setAcceleration(300);
}

void loop()
{
//TRACK MOVEMENT 1:
stepper1.move(26666);
stepper2.move(26666);
while (stepper1.currentPosition() != 26666) // Full speed up to 26666
{
stepper1.run();
stepper2.run();
}

delay(500);

//TRACK MOVEMENT 2:
//Turn 90 degrees to the right. Turn one wheel only
stepper1.move(6400);
while (stepper1.currentPosition() != 6400)
{
stepper1.run();
}

delay(500);

//TRACK MOVEMENT 3:
//Move striaght
stepper1.move(9600);
stepper2.move(9600);
while (stepper1.currentPosition() != 9600) // Full speed up to 300
{
stepper1.run();
stepper2.run();
}

delay(500);

//TRACK MOVEMENT 4:
//Turn 90 degrees to the right to face the beam
stepper2.move(6400);
while (stepper2.currentPosition() !=6400)
{
stepper2.run();
}

delay(500);

//TRACK MOVEMENT 5:
//Drive
stepper1.move(6000);
stepper2.move(6000);
while (stepper1.currentPosition() != 6000) // Full speed up to 300
{
stepper1.run();
stepper2.run();
}

delay(5000);

}
``````

I'm surprised it works at all. Stepper.move() does a relative move.

Movement 1 goes to 26666, 26666

Movement 2 goes to 33066, 26666 (previous position + 6400, 0)
This movement doesn't stop until stepper1 reaches 6400 but it will only go from 26666 to 33066.

Movement 3 goes to 42666, 36266 (previous position + 9600, 9600)
This movement doesn't stop until stepper1 gets to 9600 but it will only go from 33066 to 42666

Instead of checking for a target position, try:

``````while (stepper1.run() || stepper2.run()) ;
``````

This will wait until both motors have reached the target position.

Like this:

``````void loop()
{
//TRACK MOVEMENT 1:
stepper1.move(26666);
stepper2.move(26666);
while (stepper1.run() || stepper2.run()) ;
delay(500);

//TRACK MOVEMENT 2:
//Turn 90 degrees to the right. Turn one wheel only
stepper1.move(6400);
while (stepper1.run() || stepper2.run()) ;
delay(500);

//TRACK MOVEMENT 3:
//Move striaght
stepper1.move(9600);
stepper2.move(9600);
while (stepper1.run() || stepper2.run()) ;
delay(500);

//TRACK MOVEMENT 4:
//Turn 90 degrees to the right to face the beam
stepper2.move(6400);
while (stepper1.run() || stepper2.run()) ;
delay(500);

//TRACK MOVEMENT 5:
//Drive
stepper1.move(6000);
stepper2.move(6000);
while (stepper1.run() || stepper2.run()) ;
delay(5000);
}
``````

I think that John has probably nailed the problem. I suspect that you should be using moveTo(), not move(), to get to the position(s) you want.

Use variables to hold the position to move to, not literals. Literals are interpreted as ints, unless you define otherwise. The functions you are calling expect longs, not ints.

As was suggested in your other thread, create functions for each set of movements. If function B works when called after function A, but doesn't work if function A is not called first, then there is a clue in what function A is doing that function B is not.

Thanks John!! It works now. But i had to use && rather than | | for the movements that i wanted both steppers to move at the same time because it was only moving one with | |