Stopping the Stepper Motor Instantly

I have 1 stepper motor, the driver of my stepper motor is TB6600 and I also have a switch. I want this motor to rotate left and right continuously in the loop. When I turn off the ignition, the engine will suddenly stop and when I start it again, I want the engine to continue from where it stopped. Thank you in advance for your help :grinning:

Thnks for telling. What would the question be? What's the help needed?

I need code that does the requirements I said

My code is this, but in this code, when I stop the engine, it goes to zero point, checks and joins the loop back. I don't want this, it should continue to go from where it stopped.

#include <AccelStepper.h>

#define MOTOR_PIN1 5
#define MOTOR_PIN2 6

AccelStepper stepper(1, MOTOR_PIN1, MOTOR_PIN2);

const int switchpin = 13;
bool previousswitchstate = false;

void setup() {
pinMode(switchpin, OUTPUT);
stepper.setMaxSpeed(1000.0);
stepper.setAcceleration(500);
}

void loop() {
bool switchstate = digitalRead(switchpin);

if (switchstate != previousswitchstate) {
if (!switchstate) {
stepper.stop();
} else {

  long targetlocation = (stepper.currentPosition() >= 2000) ? 0 : 2000;
  stepper.moveTo(targetlocation);
}
previousswitchstate = switchstate; 

}

if (!switchstate) {
if (!stepper.isRunning()) {

  long targetlocation = (stepper.currentPosition() >= 2000) ? 0 : 2000;
  stepper.moveTo(targetlocation);
}
stepper.run(); 

}
}

That's better. Please check up and use "code tags", the "" symbol in this window.

Good. You make the code. Forum is not a fix up shot creating code for You.

The definition of switchpin is wrong. Try "INPUT_PULLUP", not OUTPUT if You want to read anything from it.

1 Like

You are confusing us! You write of a stepper motor, then you write of an internal combustion engine. What is the connection between the two?

In order to continue from where it stopped, you would need to save the position in non-volatile memory. However, the actual position could change while power is off.

For these reasons, it is usual to have home or limit switches, or an encoder so that the actual position can be determined when power is applied.

Therefore with your problem as stated, there is insufficient information to provide a solution.

The position of the motor may be disturbed when the power is turned off, but this does not cause any problems.

I don't have any problems with the switch, I use direct switches. That's how it works. But I don't have any problems, if it needs to be fixed, it can be left as is. The switch only sends power to the 13th pin and cuts off when I turn it off.

When I say ignition, I mean the key.

I need urgent help

I'm sure you do, but you have not given us enought information to properly help you.

First, your code is not posted using the code tags, and is broken into pieces.

Second, as previously stated, your use of this configuration is unusual and I can not understand how the state changes.

pinMode(switchpin, OUTPUT); 
bool switchstate = digitalRead(switchpin);

What is connected to pin 13? Can you provide a hand drawn schematic of how things are connected.

All I want is to fix bugs in the code.
Since my switch pin is connected to pin 13, I use it this way. I also have a TB6600 stepper motor driver. and I also have a nema 23 motor. in the loop
I want it to turn right and left but when the switch is LOW the motor has to stop suddenly and when I turn the switch back on I want it to start from where it stopped. On Saturday or Sunday, I said that the project was urgent.
A machine will work with the project, I said it was urgent, but I'm sorry, it may not sound very convincing. Thanks already for your help

First mention of a key. What key?

Here is my attempt :

#include <AccelStepper.h>

#if 1
#define MOTOR_PIN1 5
#define MOTOR_PIN2 6
const int switchpin = 13;
#else
#define MOTOR_PIN1 63
#define MOTOR_PIN2 62
#define X_EN  48
const int switchpin = 22;
#endif

#define MIN_POS 0
#define MAX_POS 2000

AccelStepper stepper(1, MOTOR_PIN1, MOTOR_PIN2);

bool previousswitchstate = false;
signed long targetlocation;

void setup()
{
  pinMode(switchpin, INPUT_PULLUP);

#if 0
  pinMode (X_EN, OUTPUT);
  digitalWrite (X_EN, 0);
#endif

  stepper.setMaxSpeed(1000.0);
  stepper.setAcceleration(500);

  targetlocation = (stepper.currentPosition() >= MAX_POS) ? MIN_POS : MAX_POS;

  Serial.begin(115200);
}

void loop()
{
  bool switchstate = digitalRead(switchpin);

  if (switchstate != previousswitchstate)
  {
    //Serial.println(switchstate);

    if (switchstate == LOW)
    {
      stepper.stop();
    }
    else
    {
      stepper.moveTo(targetlocation);
    }
    previousswitchstate = switchstate;
  }

  if (switchstate == HIGH)
  {
    if (!stepper.isRunning())
    {
      targetlocation = (stepper.currentPosition() >= MAX_POS) ? MIN_POS : MAX_POS;
      stepper.moveTo(targetlocation);
    }
    stepper.run();
  }
}

Can I ask you one more request?
only at the beginning of the loop,2. Can you move the engine to the right in one direction?
However, every time it is stopped, the second motor position will be reset, the second motor will rotate at a constant speed, and when the switch is closed, the second motor will also stop. Thanks so much for your help.

the other engine will only act at the beginning of the code.

I can't really work out what you are asking here.

What does "at the beginning of the loop" mean?

It would help if you said what the application was, it sounds like a coil winder.

I will add another motor, this motor will go 1000 steps in one direction at a constant speed after the motor completes its right and left cycle.

thank you very much in advance for your help