How to Set the Stepper motor to stop when command while it is spinning?

How to Set the Stepper motor to stop when command while it is spinning?

Hi
I have a stepper motor connected to esp32. I am running the code below. I want to set the

> Stepper motor to stop when command while it is spinning

At first I set mySetpper.setspeed(0); instead of the FunctionDooropen(); command, causing the board to restart itself, but when I change the command, the board still restarts.

void Stop(){
if(closeFunction && dooropen_close == 0){
FunctionDooropen();
Serial.println("Door stopped by interrupt");
}
}

Thank you very muchh <333

#define m1 19
#define m2 18
#define m3 17
#define m4 16
#define TRIG_PIN 5
#define ECHO_PIN 13
#define IRin 26
#define IRout 27

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Stepper.h>


BlynkTimer timer;
char ssid[] = "XXX";
char pass[] = "XXX";
bool DoorBlynkopen = 0;
bool DoorBlynkclose = 0;
bool DoorBlynkopen_close = 0;
bool DoorAuto = 0;

int dooropen_close = 0;   //1 = open--0=close
int valIRin = 0;
int valIRout = 0;

const int stepsPerRevolution = 2048;
Stepper myStepper(stepsPerRevolution, m1, m3, m2, m4);

float filterArray[20];
float distance;

bool closeFunction = false;

void setup() {
  Serial.begin(115200);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

  pinMode(ECHO_PIN, INPUT);
  pinMode(IRin, INPUT);
  pinMode(IRout, INPUT);

  pinMode(TRIG_PIN, OUTPUT);
  pinMode(m1, OUTPUT);
  pinMode(m2, OUTPUT);
  pinMode(m3, OUTPUT);
  pinMode(m4, OUTPUT);
  
  dooropen_close = 0;
  attachInterrupt(26, Stop, FALLING); //IRin
  attachInterrupt(27, Stop, FALLING); //IRout

}

BLYNK_WRITE(V0){
  DoorBlynkopen = param.asInt();
}

BLYNK_WRITE(V1){
  DoorBlynkclose = param.asInt();
}

BLYNK_WRITE(V2){
  DoorBlynkopen_close = param.asInt();
}

BLYNK_WRITE(V3){
  DoorAuto = param.asInt();
}

/*
DoorBlynkopen = V0 <1 = open>
DoorBlynkclose = V1 <1 = close>
DoorBlynkopen_close = V2 <0 = close && 1 = open>
DoorAuto = V3 <0 = open/close manual && 1 = ultrasonic Auto-open/close>
*/
void loop() {
  Blynk.run();

  valIRin = digitalRead(IRin); // 0 = bar
  valIRout = digitalRead(IRout); // 0 = bar

  
  if(DoorAuto == 1){
    Functionultrasonic();
    if(distance <= 10 && dooropen_close == 0){
      FunctionDooropen();
    } else if(distance <= 10 && dooropen_close == 1){
      FunctionDoorStandby();
    } else if(distance > 10 && distance < 100 && dooropen_close == 1 && valIRin == 1 && valIRout == 1){
      FunctionDoorclose();
    } else if(distance > 10 && dooropen_close == 0){
      FunctionDoorStandby();
    }
  } 
  else {
    if(DoorBlynkopen == 1 && dooropen_close == 0){
      FunctionDooropen();
    } else if(DoorBlynkclose == 1 && dooropen_close == 1 && valIRin == 1 && valIRout == 1){
      FunctionDoorclose();
    } else if(DoorBlynkopen == 1 && dooropen_close == 1){
      Serial.println("Door already Open");
    } else if(DoorBlynkclose == 1 && dooropen_close == 0){
      Serial.println("Door already Close");
    } else if(DoorBlynkopen_close == 1 && dooropen_close == 0){
      FunctionDooropen();
    } else if(DoorBlynkopen_close == 0 && dooropen_close == 1 && valIRin == 1 && valIRout == 1){
      FunctionDoorclose();
    } 
    else {
      FunctionDoorStandby();
    }
  }
  
}


void Stop(){
  if(closeFunction && dooropen_close == 0){
    FunctionDooropen();
    Serial.println("Door stopped by interrupt");
  }
}

void FunctionDooropen() {
  dooropen_close = 1;
  Serial.println("open");
  myStepper.setSpeed(5);
  myStepper.step(stepsPerRevolution);
  delay(250);
}

void FunctionDoorclose(){
  closeFunction = true;
  dooropen_close = 0;
  Serial.println("close");
  myStepper.setSpeed(5);
  myStepper.step(-stepsPerRevolution);
  closeFunction = false;
}

void FunctionDoorStandby(){
  digitalWrite(m1, LOW);
  digitalWrite(m2, LOW);
  digitalWrite(m3, LOW);
  digitalWrite(m4, LOW);
  return;
}

void Functionultrasonic(){
  for (int sample = 0; sample < 20; sample++) {
    filterArray[sample] = ultrasonicMeasure();
    delay(30);
  }

  for (int i = 0; i < 19; i++) {
    for (int j = i + 1; j < 20; j++) {
      if (filterArray[i] > filterArray[j]) {
        float swap = filterArray[i];
        filterArray[i] = filterArray[j];
        filterArray[j] = swap;
      }
    }
  }

  double sum = 0;
  for (int sample = 5; sample < 15; sample++) {
    sum += filterArray[sample];
  }

  distance = sum / 10;

  
  Serial.print("distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  delay(1000);
}

float ultrasonicMeasure() {
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  float duration_us = pulseIn(ECHO_PIN, HIGH);
  float distance_cm = 0.017 * duration_us;
  return distance_cm;
}
  1. Don't "bump" your post.
  2. You expect responses in 15 minutes ?
1 Like

you're not going to be able to say move some # of steps and interrupt up it.

you're going to have to repeatedly specify a smaller # of steps until the target # is reached and check for a command between each

1 Like

I'm sorry, my internet is slow and I'm not sure if my post can be posted.

I do not know how you will "home" your motor, but to stop it, in your Stop() function, have you tried setting m1, m2, m3, m4 to LOW?

1 Like

You mean, we don't have to order it to spin at once, but order it to spin little by little, then check another command, and then continue spinning?

I tried it successfully, but the motor didn't stop.

Thank you so much for the help, I'll give it a try.

If you change from the stepper-library to the MobaTools library you can start any amount of steps to rotate or even to rotate infinitely and stop at any point in time at any number of steps

You can install the MobaTools library from the library-manager of the arduino-IDE

After installing there is a folder MobaTools with the documentation as a pdf-file
MobaTools-260-en.pdf

best regards Stefan

1 Like

Thank you very much, I'll give it a try.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.