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 が混在しています。
  • 両方のチャネルで、測定値のデッド ゾーンがいくつかあります。たとえば、チャネル 1 の場合、チャネル 1 が 1465 の場合はどうなりますか? それとも1460か1470?これらすべての場合、「if」ステートメントはどれも起動しません。チャンネル 2 では、下側のデッド ゾーンがはるかに大きく、上側のデッド ゾーンがまだ存在します (チャンネル 2 がちょうど 1500 の場合はどうなるでしょうか?)。

最後に、あなたのモーターは何かをしたはずです。チャンネル 1 が 1500 未満だったことが原因かもしれません。それでも動作しない場合は、配線を確認してください。

また、スタイル ノート: 読み取る入力ピンとして channel1 を使用します。Channel1 を使用して、そこから読み取った値にします。これらは非常によく似ています。1 つをタイプミスした場合、それを見つけることはほとんど不可能です。間違いを見つけやすく、より明白な方法でこれらに名前を付けることを検討する必要があります。たとえば、channel1 は pinCh1 になり、Channel1 は inpCh1 になります。他の人は、おそらく最初の文字がデータ型を示す、さらにわかりやすい名前を提案するかもしれません。例: int iChannel1_PinNumber; int iChannel1_InputValue;

于 2013-04-19T16:47:46.147 に答える