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.
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.
ekarlsson25:
Oh many answers!
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 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?
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;
}