1

Here's my code for starting a Stepper motor and trying to stop the motor:

#include <BasicStepperDriver.h>

#define DIR 34
#define STEP 36
#define ENBL 30

BasicStepperDriver stepper1(10000, DIR, STEP, ENBL);
BasicStepperDriver stepper2(10000, DIR, STEP, ENBL);

bool enableMotor1 = false;
bool enableMotor2 = false;

void rotate1() {
  Serial.println("Rotate start 1");
  stepper1.rotate(360);
  Serial.println("Rotate end 1");
}

void rotate2() {
  Serial.println("Rotate start 2");
  stepper2.rotate(360);
  Serial.println("Rotate end 2");
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  stepper1.enable();
  stepper1.setMicrostep(1);
  stepper1.setRPM(50);
  stepper2.enable();
  stepper2.setMicrostep(1);
  stepper2.setRPM(50);
}

void loop() {
  if(enableMotor1 == true) {
    rotate1();
  } else if(enableMotor2 == true) {
    rotate2();
  }  
  if (Serial.available ()) {
     String first  = Serial.readStringUntil(','); 
     Serial.read(); 
     String second = Serial.readStringUntil(','); 
     if(first == "start"){
        if(second == "1") {
          Serial.println("Starting motor 1");
          enableMotor1 = true;
        }
        if(second == "2") {
          Serial.println("Starting motor 2");
          enableMotor2 = true;
        }
     } else if(first == "stop") {
        if(second == "1") {
          Serial.println("Stopping motor 1");
          enableMotor1 = false;
          stepper1.disable();
        }
        if(second == "2") {
          enableMotor2 = false;
          Serial.println("Stopping motor 1");
          stepper2.disable();
        }
     }
  }
}

The problem with this code is that it is not possible to stop the motor through Serial interface. The goal is to have the Stepper motor running until the stop command is entered in the serial monitor. What could be wrong in my code?

This image shows that we can start the motor but can't be stopped:

enter image description here

4
  • 2
    Did you speedup the picture? You send start,1 instead of start,1,, so the second call to readStringUntil(',') should incur in a timeout of 1 s. because no comma appears after 1. Note that this delay could potentially disrupt your stepper by preventing it from rotating at a constant rate. Removing the delay isn't an option either, because with the current parsing functionality that you implement you increase the chance of parsing fragments of commands rather than the whole thing. Note that you would need an extra Serial.read() to account for the extra , too. Feb 13, 2017 at 21:44
  • 1
    Anyway, this looks like an issue that should be easy to debug. Did you try: 1. print the content of first and second to serial after parsing, what's inside? 2. repeat the command start, 1 and see if it is taken again? 3. send the command stop, 1 for first and see whether it is taken? Feb 13, 2017 at 21:45
  • I wonder why stop, 1 and stop, 1, works?
    – quarks
    Feb 14, 2017 at 6:19
  • interesting, what about other tests =) Feb 14, 2017 at 6:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.