<hello I have a isue with my stepper motor program . I am using a bounce program to drive my stepper motor with . But sometimes it doesnt do the same distance at the beginning how could I define a starting point from where it would go down 35000 steps and then would go up . So that when I take off the power I could just move it back to its initial position . Else could i maybe use 2 limit switches that would define the 2 points where it has to go back the other way
// Bounce.pde
// -- mode: C++ --
//
// Make a single stepper bounce from one limit to another
//
#include <AccelStepper.h>
// Define a stepper and the pins it will use
// AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
AccelStepper stepper(1,7,6);
void setup()
{
// Change these to suit your stepper if you want
stepper.setMaxSpeed(1000);
stepper.setAcceleration(10);
stepper.moveTo(1000);
}
void loop()
{
// If at the end of travel go to the other end
if (stepper.distanceToGo() == 0)
stepper.moveTo(-stepper.currentPosition());
Having a "homing" procedure to really know where things are is a typical process. You have at least one limit switch and in the setup you run towards the limit switch until you hit it. Then you know where you are and can start from there.
Depending on the stepper motor you can drive the motor to a mechanical stop , stalling the motor briefly to set your reference point .
Works with car instrument type steppers for example .
That maybe a problem. If you do so, the reference point is in the middle of your Movement. If you start the program, you do not know in which direction to go to the reference point. It is better to place the reference point at one end of the traverse path and bounce between 0 and the other end.
That's a fairly simple version of homing ( and it sets the reference point to the middle of your movement ). The limit switch must be positioned at the negative end.
// Bounce.ino
// -- mode: C++ --
//
// Make a single stepper bounce from one limit to another
//
#include <AccelStepper.h>
// Define a stepper and the pins it will use
// AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
AccelStepper stepper(1,7,6);
const byte limitPin = A2; // input pin for limit switch, change to your needs
// Switch must connect to GND if stepper reaches the switch.
void setup()
{
// Change these to suit your stepper if you want
pinMode( limitPin, INPUT_PULLUP );
stepper.setMaxSpeed(100); // slow speed for homing
stepper.setAcceleration(100);
// start homing procedure
stepper.moveTo( -2000 ); // should be max possible movement towards the limit switch
while( digitalRead( limitPin ) == HIGH ) stepper.run(); // move until limit switch is reached
stepper.setCurrentPosition( -1000 ); // set current position as negative limit
// start normal move
stepper.setMaxSpeed(1000);
stepper.setAcceleration(10);
stepper.moveTo(1000);
}
void loop()
{
// If at the end of travel go to the other end
if (stepper.distanceToGo() == 0)
stepper.moveTo(-stepper.currentPosition());
stepper.run();
}
How did you connect your linit switch?
The limit switch must be connected between the pin and Gnd, and it mus be normally open. It must close, when the stepper reaches the switch.