0

我有 9 通道 RF RX/TX,我想将 3 个电机连接到它。我可以将通道 1 与电机 1 连接,但无法将通道 2 与电机 2 同时与 ardunio 连接。请看一下代码。无法旋转不同通道的电机

    int motor1Left = 7;// defines pin 5 as connected to the motor
int motor1Right= 9;// defines pin 6 as connected to the motor
int motor2Left = 22;// defines pin 7 as connected to the motor
int motor2Right = 26;// defines pin 8 as connected to the motor
int enable = 5;
int enable2 = 10;
int channel1 = 2; // defines the channels that are connected
int channel2 = 3;// to pins 9 and 10 of arduino respectively

int Channel1 ; // Used later to 
int Channel2 ; // store values

void  setup ()
{
   pinMode (motor1Left, OUTPUT);// initialises the motor pins
   pinMode (motor1Right, OUTPUT);
   pinMode (motor2Left, OUTPUT);
   pinMode (motor2Right, OUTPUT);// as outputs
   pinMode (channel1, INPUT);// initialises the channels
   pinMode (channel2, INPUT);// as inputs
  //pinMode (enable, OUTPUT);



   Serial.begin (9600); // Sets the baud rate to 9600 bps


}

void  loop ()
{
  Channel1 = (pulseIn (channel1, HIGH)); // Checks the value of channel1
   Serial.println (Channel1); //Prints the channels value on the serial monitor
  delay(1000);

  Channel2 = (pulseIn (channel2, HIGH)); // Checks the value of channel1
  Serial.println (Channel2); //Prints the channels value value on the serial monitor
  delay(1000);

  if (Channel1 > 1470 && Channel1 < 1500) /*If these conditions are true, do the following. These are the values that I got from my transmitter, which you may customize according to your transmitter values */
  {
    digitalWrite (motor1Left, LOW); // Sets both the
    digitalWrite (motor1Right, LOW);// motors to low
   analogWrite(enable, 100);  
}

  if (Channel1 < 1460) // Checks if Channel1 is lesser than 1300
  {
    digitalWrite (motor1Left, HIGH);// Turns the left
    digitalWrite (motor1Right, LOW); // motor forward
    analogWrite(enable, 100);
    //delay(500); 
    //delay(500);
    //digitalWrite(motor1Left, LOW);
    //delay(1);
}

  if (Channel1 > 1510) // Checks if Channel1 is greater than 1500
  {
    digitalWrite (motor1Left, LOW);// Turns the right

    digitalWrite (motor1Right, HIGH);// motor forward
     analogWrite(enable, 70);
     //delay(500);
    //digitalWrite (motor1Right, LOW);
   // delay(50);
    //digitalWrite (motor1Right, HIGH);


}

  if (Channel2 > 1480 && Channel1 < 1500 ) // If these conditions are true, do the following
  {
    digitalWrite (motor2Left, LOW);// Sets both the
    digitalWrite (motor2Right, LOW);// motors to low
    analogWrite (enable2, 100);
}

  if (Channel2 < 1300) // Checks if Channel2 is lesser than 1300
  {
    digitalWrite (motor2Left, LOW);// Turns the left
    digitalWrite (motor2Right, HIGH);// motor backward
    analogWrite (enable2, 100);
  }
  if (Channel2 > 1500) // Checks if Channel2 is greater than 1500
  {
    digitalWrite (motor2Left, HIGH);// Turns the right
    digitalWrite (motor2Right, LOW);// motor backward
    analogWrite (enable2, 100);
  }
}
4

1 回答 1

0

我看到几个问题:

  • 您提到了 3 个电机,但似乎只有 2 个。
  • 您打印出读取的值,但没有任何帮助来识别哪个值。考虑添加“Serial.print("Ch1 = ");" 就在第一个 println 之前进行区分。
  • 关于第 72 行,您混合了 Channel2 和 Channel1。
  • 在这两个通道中,您都有一些读数盲区。例如,对于 Channel 1,如果 Channel1 是 1465,会发生什么?还是 1460 或 1470?在所有这些情况下,您的“if”语句都不会触发。在 Channel2 中,下盲区要大得多,而上盲区仍然存在(如果 Channel2 正好是 1500 怎么办?)。

最后,您的电机似乎仍然应该做点什么。也许是因为通道 1 小于 1500。但如果仍然无法正常工作,请检查您的接线。

另外,风格说明:您使用 channel1 作为输入引脚来读取。您使用 Channel1 作为从中读取的值。这些看起来非常相似。如果你打错了,那几乎是不可能被发现的。您应该考虑以一种更容易发现错误且更明显的方式命名它们。例如,channel1 可以变成 pinCh1,而 Channel1 可以变成 inpCh1。其他人可能会建议更具描述性的名称,可能第一个字母表示数据类型。例如,int iChannel1_PinNumber;int iChannel1_InputValue;

于 2013-04-19T16:47:46.147 回答