简体   繁体   中英

Arduino, Breaking Stepper motor in the for loop

Need to break the Stepper motor operation within "for loop". But the code that I have written breaks the operation after the completion of the loop, it does not break the operation between the loop. Please chek the code and tell me any possible way to stop the loop in between..

Code:

#include <Stepper.h>
int in1Pin = 8;
int in2Pin = 9;
int in3Pin = 10;
int in4Pin = 12;

bool entry = false;
int j;

Stepper motor(200, in1Pin, in2Pin, in3Pin, in4Pin);

void setup() {
  pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT);
  pinMode(in3Pin, OUTPUT);
  pinMode(in4Pin, OUTPUT);

  while (!Serial);
  Serial.begin(9600);
  motor.setSpeed(300);
  Serial.println("Type in your selection");
  entry = false;
}

void loop() {
  if (Serial.available() > 0){
    switch(Serial.read()){
      case 'a':
       entry = true;
       break;
      case 'b':
       entry = false;
       break;
      default:break;
    }
  }
  if(entry == true){
        for(j = -20; j <= 20; j++){
          motor.step(j/0.176);
        }
      }
  else if(entry == false){
       digitalWrite(in1Pin,LOW);
       digitalWrite(in2Pin,LOW);
       digitalWrite(in3Pin,LOW);
       digitalWrite(in4Pin,LOW);
  }

}

From Comments: when i send 'a' through serial monitor, the stepper starts rotating, when i send 'b' through serial it should break stepper motor rotation, but its breaking only after the completion of loop (not within the loop)

If you don't give a chance to Arduino to parse the serial input in-between the execution of the for loop, then obviously is not going to work as you want.

You should do something similar to this:

...

void loop() {
  static int j;

  if (Serial.available() > 0){
    switch(Serial.read()){
      case 'a':
       entry = true;

       /**
        * replace next 3 lines with `j = -20` if you want to start
        * the loop from scratch each time `a` is received,
        * instead of resuming from where you stopped.
        */
       if (j >= 20) {
         j = -20;
       }

       break;
      case 'b':
       entry = false;
       break;
      default:break;
    }
  }

  if (entry) {
    if (j <= 20) {
      motor.step(j/0.176);
      ++j;
    }
  } else {
     digitalWrite(in1Pin, LOW);
     digitalWrite(in2Pin, LOW);
     digitalWrite(in3Pin, LOW);
     digitalWrite(in4Pin, LOW);
  }
}

In this code, I let Arduino parse a new character after each call to motor.step() . Note that this approach introduces a tiny delay among sub-sequent calls to motor.step() wrt. your original solution, but it should be negligible in practice.

If you send multiple a in a row to Arduino using this code, but Arduino did not complete the for loop yet, then with this code the additional a will be ignored. If you want to handle them as additional for request, then you should add a counter to keep track of the number of pending (full) iterations of the for loop that you want to perform.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM