Push button control over Stepper motor TB6600

Hi all, I'm confusing on the push button control stepper motor. My requirement is to control a stepper motor to turn CW or CCW using a push button. So that, when the push button is pressed, the stepper motor will turn CW or CCW until the button is released. Then after the stepper motor was move to the required position, another push button is press to execute the code below. Please advise me to do so. Thanks.

// Define stepper motor connections and steps per revolution:
#define enaPin 10
#define dirPin 11
#define stepPin 12

void setup() {
  // Declare pins as output:
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
}

void loop() {
  // Set the spinning direction clockwise:
  digitalWrite(dirPin, HIGH);

  // Spin the stepper motor 1 revolution slowly:
  for (int i = 0; i < 200; i++) {
    // These four lines result in 1 step:
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
  }

  delay(300);

  // Set the spinning direction counterclockwise:
  digitalWrite(dirPin, LOW);

  // Spin the stepper motor 1 revolution quickly:
  for (int i = 0; i < 200; i++) {
    // These four lines result in 1 step:
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
  }

  delay(300);

  
}

Hi,

Which do you want;

  1. The stepper to stop when you release the button?
    OR
  2. The stepper to continue to the required position after you have released the button?
    OR
  3. The stepper to move to the required position and stop even if the button is still pressed?
    OR
  4. Cases 2) and 3)?

What are you using to drive the stepper motor?
Your code at the moment will make the stepper rotate continuously.

Have you tried it on your hardware?

Thanks... Tom... :grinning: :+1: :coffee: :australia:

Is Condition 1. I'm using TB6600 motor driver to drive the stepper.
Yup. I have tried it on my hardware for this code. Thanks

Hi,
Can you please post a circuit diagram?
Show power supplies.

You are using microsecond delays, make the pulse bigger, give you stepper a chance to respond to commands.
100us is very very small. = 5Mhz for a 200us period.
Try;

 delay(100);

100ms it will let you see if the stepper is being activated or not.
Can you please post a link to stepper data/specs?

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

Could u just give an example on how to write the coding so that when the power is on. The stepper is remain in offstate until I press and hold the button, then it move. After released, it stop. Thanks

Hi,
Have you got the existing code to move the stepper motor?

Tom... :grinning: :+1: :coffee: :australia:

Yup. I have posted at the beginning ya.

Hi,
Use an if statement.
Wire up a button, then code to check if you can read it.
Get the button to turn the D13 onboard LED.
Are you using a UNO?

Tom... :grinning: :+1: :coffee: :australia:

Try this concept:

while(digitalRead(CWPin){

  stepMotorCW;

}

while(digitalRead(CCWPin){

  stepMotorCCW;

}