-1

我是 Arduino 编程的新手,花了几天时间才走到这一步,但似乎无法弄清楚为什么这段代码不起作用。

我正在尝试使用业余级遥控器 RX/TX 来控制步进电机。

我有一个 RC 接收器将 1000 到 2000 的模拟值发送到我的 Arduino。如果该信号是 1000,我想在步进器上设置 1000 = -360 度,在步进器上设置 2000 = +360 度。

我从接收器接收到正确的信号并将它们打印到串行监视器,但我似乎无法用这个值控制步进电机。该电机只是卡在第一个 while 循环中并继续沿 1 个方向旋转。

int ch1 = 0;                                // RC Reciever Channel Value
int ch1previous = 0;                        // RC Receiver Channel Previous Value
int PUL=7;                                  //define Pulse pin of stepper driver
int DIR=6;                                  //define Direction pin of stepper driver
int ENA=5;                                  //define Enable Pin of stepper driver

void setup() {
  pinMode (PUL, OUTPUT);                    // Stepper Driver Pulse Pin
  pinMode (DIR, OUTPUT);                    // Stepper Driver Direction Pin
  pinMode (ENA, OUTPUT);                    // Stepper Driver Enable Pin
  pinMode(3, INPUT);                        // RC Reciever Pin Input
  Serial.begin(9600);                       

}

void loop() {

  ch1 = pulseIn(3, HIGH, 50000);                    // Read RC Reciever Channel Value

while ( ch1 > ch1previous) {                        // If CH1 is greater than CH1Previous run the differance
      for ( int i = ch1; i < ch1previous; i++);{    // in steps to maintain the setpoint value in forwared position
      digitalWrite(DIR,HIGH);
      digitalWrite(ENA,HIGH);                       // pulsing stepper motor in forward motion
      digitalWrite(PUL,HIGH);
      delayMicroseconds(50);
      digitalWrite(PUL,LOW);
      delayMicroseconds(50);
}
  }

 while ( ch1 < ch1previous) {                      // if CH1 is less than CH1Previous run the differance
      for ( int i = ch1; i<ch1previous; i--);{      // in steps to maintain the setpoint value in reverse motion
      digitalWrite(DIR,LOW);
      digitalWrite(ENA,HIGH);                       // pulsing stepper motor in reverse motion
      digitalWrite(PUL,HIGH);
      delayMicroseconds(50);
      digitalWrite(PUL,LOW);
      delayMicroseconds(50);
    }
  }


  Serial.print ("Channel 1: ");             // print text to the serial monitor
  Serial.println(ch1);                      // print ch1 value to the serial monitor and end line
  Serial.print("CH1 Previous: ");           // print text to the serial monitor 
  Serial.println(ch1previous);              // print ch1previous value to the serial monitor and end line
  ch1previous = ch1;                        // remember the previous value from ch1

  delay(500);                               // just to clean up the serial monitor
}
4

2 回答 2

1

while如果它们启动一次,这两种情况都会无限地持续下去。正如您在评论中所说If CH1 is greater than CH1Previous run the difference,这些 while 条件必须替换为if statements. 也就是说,将它们设为if(ch1 > ch1previous)else if(ch1 < ch1previous)。根据限定它们的 if 条件,for 循环的条件也应该颠倒。

在这些更改之后,您的代码将变成这样

void loop() {
    ch1 = pulseIn(3, HIGH, 50000);

    if ( ch1 > ch1previous) {
        for ( int i = ch1; i > ch1previous; i--){
            //code
        }
    }

    else if ( ch1 < ch1previous) {
        for ( int i = ch1; i<ch1previous; i++){
            //code
        }
    }

    ...
}
于 2017-06-29T15:48:48.330 回答
1

可以 ch1 = pulseIn(3, HIGH, 50000) 是负值吗?如果不是,那么当您考虑以下陈述时,这可以解释这种行为:

 while ( ch1 > ch1previous) { 

ch1previous初始化为零。

于 2017-06-29T13:26:43.363 回答