Homing stepper with a swtich

Hey!
I'm worink on a project where I'm gonna build a box that is sliding on a linear rail (very much like a CNC) using a stepper motor. I'm using an meny to determine the actions of the stepper motor, but i wont the stepper to "home" itself before it move to its first stop.

So my question is: Is it posible to make the stepper move until it triggers a switch before starting its actions?

Sorry if the question is bad formulated. It is to prevent the motor to start from a wrong point incase of the power shuts down in the middle of a previous action.

I'm using the library "accelstepper" to control the stepper.

Sure, limit switches are used all the time for this. Look at "micro switches".

Simply move the motor a little bit (In case it is already at the limit) and then slowly move it back while checking the state of the switch.

Rig up a limit switch and at startup, move the stepper until it triggers it. Look at the reprap project - there are limit switches used there for the same thing.

You could use 2x micro switches to speed up reference point homing. Return home at full speed until first micro switch is closed then slow until second one closes.

Oh many answers! :slight_smile:
But how am I going to prgoram it? Is there a tutorial on those RepRap machines?

ekarlsson25:
Oh many answers! :slight_smile:
But how am I going to prgoram it? Is there a tutorial on those RepRap machines?

In setup...
Is limit switch active?
yes: step out x number of steps
is limit switch active?
yes: Error, switch broken. stop.
while limit switch not active
step in 1 step
wend
your home.

I can't work it out.. :~
I'm pretty new to programing.

I can't work it out.. smiley-confuse
I'm pretty new to programing.

What have you tried? Try as I might, I can't find your code anywhere in this thread.

I haven't got the rail atm, but it seems like it should work anyways, like if the button is not active the motor moves backwards. But when the button gets pressed the code from the menu starts.

I've just tried to modify the AccelStepper's example "blocker" with a funktion that reads the buttonstate. But i don't really know how i should proceed, or if I'm doing it right from the start?

But i don't really know how i should proceed, or if I'm doing it right from the start?

Move over a little. Your blocking our view of your code.

A little more. More. Careful you don't fall off of the chair. A little more.

Hey, I said be careful. Now, get up and post your code.

Here is the code. I've gotten it so far that it needs a press of the button to start activate the program. But i cant figure out how to make it move backwards until the button in pressed.

#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper(1, 49, 47);

int switchPin = 33;
int ledPin = 13;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean ledOn = false;

void setup()
{
  pinMode(switchPin, INPUT);
  pinMode(ledPin, OUTPUT);
  stepper.setMaxSpeed(200.0);
    stepper.setAcceleration(100.0);
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(switchPin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;
}

void loop()
{
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)
  {
    ledOn = !ledOn;
    stepper.runToNewPosition(0);
    stepper.runToNewPosition(500);
    stepper.runToNewPosition(100);
    stepper.runToNewPosition(120);
  }
  lastButton = currentButton;
  }