Hi all, can you help?
I'll detail my setup in a sec, but in summary, I'm having great issues trying to get a stepper motor to run into a limit switch, stop and reverse a known number of steps to a new zero position. The code structure i've written works in another program perfectly, but for some reason, it's stopping in the current one.
I have:
Integrated Nema17 (from Zapp Automation) with their UIM240L02T controller.
Sparkfun 16x2 LCD shield
Arduino Uno
12VDC supply
The wiring is all correct as confirmed by much simpler code!
In detail, I want:
My stepper is part of a linear bed that needs to be able to zero itself before starting a program of movements. To do this, I use an initialisation loop (void Init) to send the stepper moving toward the switch with a number of steps far greater than those available in the range it has to move. To do this I use the following code (I'll post the loop code at the end and if needed the whole program in a later post)
while (digitalRead(datumPin) == LOW){
stepper.run();
}
When the switch goes high, the position is set to zero and the stepper is reversed by a known number of steps.
Serial.println(stepper.currentPosition());
stepper.setCurrentPosition(0);
Serial.println("current position is now zero");
Serial.println(stepper.currentPosition());
delay(500);
stepper.moveTo(-100);
stepper.setMaxSpeed(1000);
while (stepper.distanceToGo() != 0)
{
stepper.run();
Serial.println(stepper.distanceToGo());
}
The new position is reset to zero and this becomes the home point for all future movements.
What happens:
The stepper runs until the switch goes high, but then stops dead. I've placed a number of "println"'s in the code and it seems as though the position is set to zero, but the "distanceToGo" doesn't decrease to the -100 target.
This is particularly annoying as the original code (minus all the debugging and attempts to get it to work) is working perfectly in another rig.
Any help would be greatly appreciated as I'm close to tearing my hair out!
Here's the loop...
void Init(){ //run into datum switch, set zero, return to start position
if (digitalRead(datumPin) == LOW){
Serial.println("DATUM PIN OFF");
digitalWrite(sleep, HIGH);
delay(100);
stepper.move(10000);
stepper.setMaxSpeed(1000);
while (digitalRead(datumPin) == LOW){
stepper.run();
}
stepper.stop();
Serial.println(stepper.currentPosition());
stepper.setCurrentPosition(0);
Serial.println("current position is now zero");
Serial.println(stepper.currentPosition());
delay(500);
stepper.moveTo(-100);
stepper.setMaxSpeed(1000);
while (stepper.distanceToGo() != 0)
{
stepper.run();
Serial.println(stepper.distanceToGo());
}
stepper.setCurrentPosition(0);
digitalWrite(sleep, LOW);
Serial.println("init complete");
}
else {
digitalWrite(sleep, HIGH);
Serial.print("init limit pressed");
delay(100);
stepper.move(-400);
stepper.setMaxSpeed(1000);
while (stepper.distanceToGo() != 0)
{
stepper.run();
}
stepper.move(1000);
stepper.setMaxSpeed(400);
while (digitalRead(datumPin) == LOW){
stepper.run();
}
stepper.setCurrentPosition(0);
stepper.moveTo(homePos);
stepper.setMaxSpeed(1000);
while (stepper.distanceToGo() != 0){
stepper.run();
}
stepper.setCurrentPosition(0);
digitalWrite(sleep, LOW);
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" INITIALISATION ");
lcd.setCursor(0,1);
lcd.print(" COMPLETE ");
delay(2000);
if (currentMenuItem == 0) {
clearPrintTitle();
}
else {
displayMenu(currentMenuItem);
}
}