读取9通道发射器接收器的多个通道

时间:2013-04-19 06:34:25

标签: arduino

我有9通道RF RX / TX,我想连接3个电机。我能够将通道1与motor1连接,但无法与ardunio同时连接channel2和motor2。请查看代码。无法旋转不同通道的电机

    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);
  }
}

1 个答案:

答案 0 :(得分:0)

我看到了几个问题:

  • 你提到3个电机,但似乎只有2个。
  • 您打印出值读取,但没有任何帮助来确定哪一个。考虑添加“Serial.print(”Ch1 =“);”就在第一张印刷品出现差异之前。
  • 关于第72行,您已混合使用Channel2和Channel1。
  • 在两个频道中,您的读数都有一些死区。例如,对于通道1,如果Channel1为1465会发生什么?还是1460或1470?在所有这些情况下,你的“if”语句都不会触发。在Channel2中,下死区要大得多,上死区仍在那里(如果Channel2恰好是1500?)。

最后,看起来您的电机似乎还应该做点什么。也许是因为第1频道小于1500.但是如果它仍然不起作用,请检查你的接线。

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