简体   繁体   English

Arduino,在for循环中打破步进电机

[英]Arduino, Breaking Stepper motor in the for loop

Need to break the Stepper motor operation within "for loop". 需要在“for循环”中打破步进电机操作。 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) 来自评论:当我通过串行监视器发送'a'时,步进器开始旋转,当我通过串行发送'b'时它应该打破步进电机旋转,但它只在循环完成后断开(不在循环内)

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. 如果你没有机会让Arduino解析执行for循环之间的串行输入 ,那么显然不能按你的意愿工作。

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() . 在这段代码中,我让Arduino在每次调用motor.step()之后解析一个新字符。 Note that this approach introduces a tiny delay among sub-sequent calls to motor.step() wrt. 请注意,这种方法在对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. 如果您使用此代码连续向Arduino发送多个a ,但Arduino尚未完成for循环,那么使用此代码将忽略附加的a 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. 如果你想将它们作为额外for要求,那么你应该添加一个counter ,以保持未决(全)迭代次数的轨道上for要执行循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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