I have the following test code, which I'm running on an Arduino Yùn. Only PulCyan and DirCyan are connected to Arduino, all the remaining pins are not connected (except ground).
If I run this program I'd expect to have a 500ms stop when the position of the only motor connected (stepperCyan) reaches 0.
However I see that when it reaches position 0, it stops, than it makes 1 step and it stops again, and then it goes at -200 and so on..
How is this possible? I'm quite confused. (code updated thanks to answers below, but still not working properly as described before)
#include <AccelStepper.h>
// Define a stepper and the pins it will use
//Stepper motors
int pulYellow = 30; //N/A on Yùn
int dirYellow = 31; //N/A on Yùn
//int enaYellow = 32; //N/A on Yùn
int pulMagenta = 33; //N/A on Yùn
int dirMagenta = 34; //N/A on Yùn
//int enaMagenta = 35; //N/A on Yùn
int pulCyan = 4; //36 MEGA
int dirCyan = 8; //37 MEGA
//int enaCyan = 38; //N/A on Yùn
AccelStepper stepperMagenta(AccelStepper::DRIVER, pulMagenta, dirMagenta);
AccelStepper stepperYellow(AccelStepper::DRIVER, pulYellow, dirYellow);
AccelStepper stepperCyan(AccelStepper::DRIVER, pulCyan, dirCyan);
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval =500;
void setup()
{
// Change these to suit your stepper if you want
stepperMagenta.setPinsInverted(true, true, true);
stepperYellow.setPinsInverted(false, false, false);
stepperCyan.setPinsInverted(true, true, true);
stepperMagenta.setMinPulseWidth(20);
stepperYellow.setMinPulseWidth(20);
stepperCyan.setMinPulseWidth(20);
stepperMagenta.setMaxSpeed(400);
stepperMagenta.setAcceleration(200);
stepperMagenta.moveTo(200);
stepperYellow.setMaxSpeed(400);
stepperYellow.setAcceleration(200);
stepperYellow.moveTo(200);
stepperCyan.setMaxSpeed(400);
stepperCyan.setAcceleration(200);
stepperCyan.moveTo(200);
}
void loop()
{
if (stepperMagenta.currentPosition() == 200 && stepperMagenta.distanceToGo() == 0)
stepperMagenta.moveTo(0);
if (stepperMagenta.currentPosition() == 0 && stepperMagenta.distanceToGo() == 0){
delay(500);
stepperMagenta.moveTo(-200);
}
if (stepperMagenta.currentPosition() == -200 && stepperMagenta.distanceToGo() == 0)
stepperMagenta.moveTo(200);
//-------
if (stepperYellow.currentPosition() == 200 && stepperYellow.distanceToGo() == 0)
stepperYellow.moveTo(0);
if (stepperYellow.currentPosition() == 0 && stepperYellow.distanceToGo() == 0){
delay(500);
stepperYellow.moveTo(-200);
}
if (stepperYellow.currentPosition() == -200 && stepperYellow.distanceToGo() == 0)
stepperYellow.moveTo(200);
//-------
if (stepperCyan.currentPosition() == 200 && stepperCyan.distanceToGo() == 0)
stepperCyan.moveTo(0);
if (stepperCyan.currentPosition() == 0 && stepperCyan.distanceToGo() == 0){
delay(500);
stepperCyan.moveTo(-200);
}
if (stepperCyan.currentPosition() == -200 && stepperCyan.distanceToGo() == 0)
stepperCyan.moveTo(200);
//-------
stepperMagenta.run();
stepperYellow.run();
stepperCyan.run();
}