Hello all! First time posting on this forum. I'm pretty new to Arduino, I've only ever just dabbled in it but now I'm trying to build something and am getting stuck trying to figure out my stepper motion. Here's my project:
I'm building a small machine that will melt grooves into some foam tubing. It's simple enough in that I've only got one stepper that moves a tool down a rail. It homes on a limit switch to start and moves in the opposite direction until it hits the limit switch at the end of a clamp stand (which I can adjust for different lengths). After homing I can hit a start button that starts my sequence, activates a relay (pushing my hot knife into the foam) and moves right until the end-switch is activated at which point the hot knife is retracted. The code is meant to back off the end-switch, stop the motor and rapidly move back to a set position just past the home switch so I can start my sequence again. It worked for a while but now after backing off the end-switch the motor just keeps going until it hits the home switch.
Here's my code:
#include <AccelStepper.h>
AccelStepper Stepper1(1, 12, 11); //pin 11 and 12 for dir and step, 1 is the “external driver” mode
int STARTbutton = 7; //button
const int STATUSled = 4;
int Relay = 6; //relay conneced to pin 6
int HomeSwitch = 3; //Home limit switch
int EndSwitch = 2; //End limit switch
int stepping = false;
long initialhoming = -1; //1 step move
long traverse = 1; //1 step move right
void setup() {
Stepper1.setMaxSpeed(250); //max speed the motor will turn (steps/second)
Stepper1.setAcceleration(150); //acceleration (steps/second^2)
pinMode(Relay, OUTPUT);
pinMode(STATUSled, OUTPUT);
pinMode(HomeSwitch, INPUT);
pinMode(STARTbutton, INPUT);
pinMode(EndSwitch, INPUT);
//my homing sequence
digitalWrite(Relay, HIGH);//deactivate relay
while (digitalRead(HomeSwitch)) {
Stepper1.moveTo(initialhoming); //move left towards home switch
initialhoming--;
Stepper1.run();
}
Stepper1.setCurrentPosition(0);
Stepper1.setMaxSpeed(500);
Stepper1.setAcceleration(250);
initialhoming = 1;
while (!digitalRead(HomeSwitch)) {
digitalWrite(STATUSled, HIGH); // turn the LED on
Stepper1.moveTo(initialhoming); //move right
initialhoming++;
Stepper1.run();
}
digitalWrite(STATUSled, LOW); // turn the LED off
Stepper1.setCurrentPosition(0);
}
void loop() {
//starting my operation by pressing the start button
if (digitalRead(STARTbutton) == HIGH && stepping == false) {
stepping = true;
}
if (stepping == true) {
digitalWrite(STATUSled, HIGH); // turn the LED on
delay(1000); // wait for 1 second
digitalWrite(STATUSled, LOW); // turn the LED off
delay(1000); // wait for 1 second
digitalWrite(Relay, LOW);//activate relay
delay(2000); //wait x sec
while (digitalRead(EndSwitch)) {
Stepper1.moveTo(traverse); //move right
traverse++;
Stepper1.run();
}
traverse = -1;
stepping = false;
digitalWrite(Relay, HIGH);//deactivate relay
//backing off my endswitch
while (!digitalRead(EndSwitch)) {
digitalWrite(STATUSled, HIGH); // turn the LED on
Stepper1.moveTo(traverse); //move left
traverse--;
Stepper1.run();
}
Stepper1.stop(); //stop motor
digitalWrite(STATUSled, LOW); // turn the LED off
}
//my attempt at stopping the motor after coming off the endswitch
else {
stepping = false;
Stepper1.stop(); //stop motor
}
}
I've previously unsuccessfully tried this to return to a good starting position past the home-switch:
Stepper1.moveTo(1000); //move to a 1000 steps past the home switch
Stepper1.run();
I'm using the TB6600 motor driver:
Any insight or help would be greatly appreciated!
-Fred