Programming Newbie; Need help returning stepper to a specific position

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

Take a DMM and check that end switch up. To me it looks like the switch has thrown the towel in.

The motor does stop when it hits the end-limit switch and does back off. I can also see when the switch activates and de-activates as it has an led on it's board. These are the switches i'm using:

I'll keep an eye on it though, thanks for your reply!

I have no experience of exactly those switches.
Guessing...... Maybe the travel of the mechanics pushed the switch too far, beyond the mechanical range and it got damaged?
Fabrication faults are always possible especially on Chinese low cost stuff.
Can You estimate how many times the switch has been operated? All mechanical stuff has a certain life time.

Check the cables from the switch to the controller.

Great point! It's a new switch but I do have spares, I'll swap it out and test.

Yes, do that, swap it out. You can check it up later.

I once worked with space electronics. Every board that will go to space is more than carefully tested. No service engineers up there in the blue. During the last part of the testing a high quality relay died. Summing up the previous tests and the number of relay operations the tests had exceeded the specified number of operations! Don't know what they did then.....

1 Like

Swaped it out and tested. I'm still having the same problem, I suspect I'm doing something wrong with my code. I'm still fairly new to programming and just cobbled this code together from what I've read online. I just need it to stop after coming off the end-switch so I can add code to have it rapidly return near the home switch.

How are Your end switches wired? Make a little drawing and post it.
I suggest using INPUT_PULLUP in pinMode and wire the NO of the switch to the input pin and COM wired to GND. A guess.....

I'd suggest using physical pull-up resistors as they are more immune to noise pulses. Something in the range 1k to 4k7 is a good starting place. But you definitely need some sort of pullup if you want to use switches like this, otherwise anything could happen.