我有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);
}
}
答案 0 :(得分:0)
我看到了几个问题:
最后,看起来您的电机似乎还应该做点什么。也许是因为第1频道小于1500.但是如果它仍然不起作用,请检查你的接线。
另外,样式注释:您使用channel1作为要读取的输入引脚。您使用Channel1作为从中读取的值。这些非常相似。如果你错了一个,几乎不可能发现。您应该考虑以更容易发现错误的方式命名这些,更明显。例如,channel1可能成为pinCh1,而Channel1可能成为inpCh1。其他人可能会建议更具描述性的名称,可能是第一个字母表示数据类型。例如。, int iChannel1_PinNumber; int iChannel1_InputValue;