Hey all,
I'm trying to make a hand follow device for some research project.
One of the things the devices should be able to do is to stop going to an end position and move to a new position from where it was.
code snap..
// stepperX was moving to 1, because of a trigger stop moving and go to 12000
stepperX.stop();
stepperX.moveTo(12000);
and call
stepperX.run();
what happens is that the motor stops but then still moves towards the 1 for a few seconds and then switches direction and goes to 12000.
This is my code:
Homeing in setup works its the part in loop that failes
#include <AccelStepper.h>
// Define the Pins used
#define home_switchX 3 // x end switch
#define home_switchY 14 // y end switch
// Define some steppers and the pins the will use
AccelStepper stepperX(1, 54, 55);
AccelStepper stepperY(1, 60, 61);
long initial_homing=-1; // Used to Home Stepper at startup
void setup()
{
pinMode(38 , OUTPUT);
pinMode(56 , OUTPUT);
digitalWrite(38 , LOW);
digitalWrite(56 , LOW);
pinMode(home_switchX, INPUT_PULLUP);
pinMode(home_switchY, INPUT_PULLUP);
delay(5); // Wait for EasyDriver wake up
Serial.begin(115200);
stepperX.setMaxSpeed(3000.0);
stepperX.setAcceleration(1000.0);
stepperY.setMaxSpeed(3000.0);
stepperY.setAcceleration(100.0);
// homeing x
while (digitalRead(home_switchX))
{ // Make the Stepper move CCW until the switch is activated
stepperX.moveTo(initial_homing); // Set the position to move to
initial_homing--; // Decrease by 1 for next move if needed
stepperX.run(); // Start moving the stepper
delay(5);
}
stepperX.setCurrentPosition(0); // Set the current position as zero for now
initial_homing=1;
while (!digitalRead(home_switchX))
{ // Make the Stepper move CW until the switch is deactivated
stepperX.moveTo(initial_homing);
stepperX.run();
initial_homing++;
delay(5);
}
stepperX.setCurrentPosition(0);
// homeing y
while (digitalRead(home_switchY))
{ // Make the Stepper move CCW until the switch is activated
stepperY.moveTo(initial_homing); // Set the position to move to
initial_homing--; // Decrease by 1 for next move if needed
stepperY.run(); // Start moving the stepper
delay(5);
}
stepperY.setCurrentPosition(0); // Set the current position as zero for now
initial_homing=1;
while (!digitalRead(home_switchY))
{ // Make the Stepper move CW until the switch is deactivated
stepperY.moveTo(initial_homing);
stepperY.run();
initial_homing++;
delay(5);
}
stepperY.setCurrentPosition(0);
stepperX.setMaxSpeed(300000.0);
stepperX.setAcceleration(10000.0);
stepperY.setMaxSpeed(300000.0);
stepperY.setAcceleration(10000.0);
}
void loop()
{
long TravelX;
long TravelY;
if(!digitalRead(home_switchY)) //end switch abused as input to move between position 1 and 12000
{
stepperX.stop();
stepperX.moveTo(12000);
}
if(!digitalRead(home_switchX)) //end switch abused as input to move between position 1 and 12000
{
stepperX.stop();
stepperX.moveTo(1);
}
bool delayQ=true;
if ((stepperX.distanceToGo() != 0)) {
stepperX.run();
delayQ=false;
}
if ((stepperY.distanceToGo() != 0)) {
stepperY.run();
delayQ=false;
}
}