Stepper motor Stop button

Hello,

I have a question. I am working to automate my curtains with a stepper motor nema 17 stepper, DVR8825 stepper driver, wemos D1 mini.
I made a code with 3 pulse switch.
Input D5 move to step 1275
Input D6 move to step 0
Input D7 stop

however the stop does not work, I would like it to stop immediately when it is doing the movement.
can someone help me?

#include <AccelStepper.h>

#define dirPin D1
#define stepPin D2
#define motorInterfaceType 1


AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

void setup() {
  stepper.setMaxSpeed(600);
  stepper.setAcceleration(200);
  pinMode(D5,INPUT); // open knop 
  pinMode(D6,INPUT); // dicht knop
  pinMode(D7,INPUT); // stop knop
}

void loop() {
  stepper.run();
  if(digitalRead(D5)==HIGH){
    stepper.moveTo(1275);
    //stepper.runToPosition();
    }


  if(digitalRead(D6)==HIGH){
    stepper.moveTo(0);
    //stepper.runToPosition();

  }
  if(digitalRead(D7)==HIGH){
     stepper.stop();

  }
}

Add some serial prints so that you can be sure that the stop button is being detected. Also, that the other buttons aren't just immediately kicking off movement again.

First question: How are your buttons wired up? They will need an external pullup/pulldown resistor to work. Are they installed? Much better to wire one side of your button to the pin, the other side to ground and declare it as INPUT_PULLUP. It will then read LOW when pressed.

You should look at the State Change Detection example that is part of the IDE (File->examples->02.digital->State Change Detection) to learn how to detect when those buttons get pressed, not if they are currently pressed (or not).

I connected the buttons with a resistor. that part i understand. The other 2 buttons do work. only the stop konp does not work

Why do you open a new topic for the same problem?

I tested your sketch and it worked fine. But because of the acceleration it doesn't stop instanteniously.
Maybe a hardwareproblem?

Hello,

I checked the hardware.
The buttons are well connected.

The stop button doesn't work for me.

I think there is something wrong in the code.

Easy enough to check. Does this work? What is on the serial Monitor?


void setup() {
  Serial.begin(9600);
  pinMode(D5, INPUT); // open knop
  pinMode(D6, INPUT); // dicht knop
  pinMode(D7, INPUT); // stop knop
  Serial.println("begin pressing buttons");
}

void loop() {
  if (digitalRead(D5) == HIGH) {
    Serial.println("D5 HIGH");
  }
  else {
    Serial.println("D5 LOW");
  }

  if (digitalRead(D6) == HIGH) {
    Serial.println("D6 HIGH");
  }
  else {
    Serial.println("D6 LOW");
  }

  if (digitalRead(D7) == HIGH) {
    Serial.println("D7 HIGH");
  }
  else {
    Serial.println("D7 LOW");
  }
  delay(1000);
}

thanks, blh64

i checked it in the serial monitor. The buttons work.

now I also read somewhere that the function stepper.runToPosition. blocks until executed

I think that's why the stop button doesn't work

Does anyone have an idea how I can get this to work?

I would like to move the button (closed) from position 0 to 1275
and with the button (open) goes from 1275 to 0.
and with the button (stop) can stop it immediately.

Your original code didn't use runToPosition. Does it work?

I use this code


#include <AccelStepper.h>

#define dirPin D1
#define stepPin D2
#define motorInterfaceType 1


AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

void setup() {
  Serial.begin(115200);
  stepper.setMaxSpeed(600);
  stepper.setAcceleration(200);
  stepper.setCurrentPosition(0);
  pinMode(D0,OUTPUT); // Zet driver aan
  digitalWrite(D0, LOW);
  pinMode(D5,INPUT); // dicht knop 
  pinMode(D6,INPUT); // open knop
  pinMode(D7,INPUT); // stop knop
}

void loop() {
  if(digitalRead(D5)==HIGH){
    Serial.println("Dicht");
    stepper.moveTo(1275);
    stepper.runToPosition();
    }
  }

  if(digitalRead(D6)==HIGH){
    Serial.println("Open");
    stepper.moveTo(0);
    stepper.runToPosition();
    }
  
  if(digitalRead(D7)==HIGH){
    Serial.println("Stop");
    stepper.stop();

  }
}

Don't. You said yourself that it blocks. Apparently, moveTo doesn't.

no I mean that the stepper.runToPosition is blocked until it is executed.

Right, and as you deduced, it prevents your stop button from working. So use moveTo and run instead.

You need to call the .run() function every time through loop()

#include <AccelStepper.h>

#define dirPin D1
#define stepPin D2
#define motorInterfaceType 1


AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

void setup() {
  Serial.begin(115200);
  stepper.setMaxSpeed(600);
  stepper.setAcceleration(200);
  stepper.setCurrentPosition(0);
  pinMode(D0, OUTPUT); // Zet driver aan
  digitalWrite(D0, LOW);
  pinMode(D5, INPUT); // dicht knop
  pinMode(D6, INPUT); // open knop
  pinMode(D7, INPUT); // stop knop
}

void loop() {
  stepper.run();

  if (digitalRead(D5) == HIGH) {
    Serial.println("Dicht");
    stepper.moveTo(1275);
  }

  if (digitalRead(D6) == HIGH) {
    Serial.println("Open");
    stepper.moveTo(0);
  }

  if (digitalRead(D7) == HIGH) {
    Serial.println("Stop");
    stepper.stop();
  }
}

With this code, crazy things may happen if you press multiple buttons and the buttons are not debounced, etc. It would be much better to look at the State Change Detection example in the IDE (File->examples->02.digital->State Change Detection) and then only change the value when a button is pressed.

Thank you,
I'll have a look in the IDE (File->examples->02.digital->State Change Detection).

This code does work.
But when I use the (stop) button it only works when the motor is running at full speed and it doesn't stop immediately.
I would like it to stop immediately when the button (stop) is pressed.
idea ?

If you want it to stop without doing any deceleration, just tell it to move to its current position. This is exactly what the .move() function does [relative move]

  if (digitalRead(D7) == HIGH) {
    Serial.println("Stop");
    //stepper.stop();
    stepper.move(0);  // stop stepping
  }

I have changed the code.
but now when i press the button (stop) it stops accelerating and slowly comes to a standstill, then moves back to the point where i pressed stop

void loop() {
  stepper.run();

  if (digitalRead(D5) == HIGH) {
    Serial.println("Dicht");
    stepper.moveTo(1975); 
  }

  if (digitalRead(D6) == HIGH) {
    Serial.println("Open");
    stepper.moveTo(0);
  }

  if (digitalRead(D7) == HIGH) {
    Serial.println("Stop");
    stepper.move(0);
  }
}

This means YOU will have to code the motor movement so you can include a test for the switch between each step.

can you tell me how i do that

Not to the coding level. But do have the data sheet for the controller printed and handy? From it I see this: The first step in configuring the DRV8825 requires the desired motor speed and microstepping level. If the target
application requires a constant speed, then a square wave with frequency ƒstep must be applied to the STEP pin.". That is where you calculate the rate to pulse the step pin. Then follows a warning about attempting to set the rate too high, but by coding the individual steps, you likely will not exceed the limit where acceleration is necessary.
So, identify the step, direction and other pins used by the control board. Your program is already using them with libraries.
This is going to be a long project, so are you REALLY, REALLY sure you want to to stop the motor immediately?