Setting the travel range for stepper.

I've been using the Accelstepper library for my DSLR slider project which is great, but would like to use runToNewPosition in a way that takes into account the physical length of the track.

I've already put proximity sensors at the extents that stop the stepper if the carriage reaches either end.
The logical thing to happen would be for the carriage to move to each end once on start-up and store values for min and max which seems to be what most printers do.

Being such a common need, I was thinking there would be functions in the Accelstepper library or something.
Anyone point me to tried and tested example before I go reinvent the wheel?

Ok it wasn't that hard. I was just being a dumbass and initialised 'maxPos' as an int instead of a long.
This works fine.

int maxSpeed = 4500;
long maxPos = 0;

AccelStepper stepper(1, 9, 8);

#define PROXPINA 6  // IR Proximity sensors. LOW when triggered.
#define PROXPINB 7 

void setup()
{  
    stepper.setSpeed(-maxSpeed);
    while (digitalRead(PROXPINB)){ // Find start of rail.
      stepper.runSpeed();
    }
    stepper.setCurrentPosition(0); // Start of rail found, set 0 pos here.
    stepper.setSpeed(maxSpeed);
    while (digitalRead(PROXPINA)){ // find end of rail.
      stepper.runSpeed();
    }
    maxPos = stepper.currentPosition(); // End of rail found, set max pos here.
    stepper.runToNewPosition(maxPos / 2); // Move Carriage to half way.
}

TijuanaKez:
I was just being a dumbass

Just like the rest of us. :slight_smile:

…R

Thanks for the "Heads Up" for that feature....