私は、シリアル ポート経由で RF 送信機に番号を送信するプロジェクトに取り組んでいます。反対側では、受信機が Arduino に取り付けられています。Visual Basic プログラムは数値を送信し、Arduino プログラムはこれらの数値を受信してモーターを制御するために使用します。(モーター用の PWM 信号が必要です) しかし、何らかの理由で、Arduino プログラムが正しい番号を受信していないため、モーターが必要に応じて機能していません。Arduino から PC に番号を送信しようとしましたが、ゴミしか受信しません。これらは、私が使用している送信機と受信機です。
http://www.alldatasheet.com/datasheet-pdf/pdf/344352/KYL/KYL-210.html
送信機と受信機が送信する値のタイプに問題がありますか? それともArduinoプログラムの問題でしょうか?
プログラムは次のとおりです。
int i = 0;
int MLmosfet1_4 = 10;
int MLmosfet2_3 = 12;
int MRmosfet1_4 = 2;
int MRmosfet2_3 = 5;
void setup()
{
pinMode(MLmosfet1_4, OUTPUT);
pinMode(MLmosfet2_3, OUTPUT);
pinMode(MRmosfet1_4, OUTPUT);
pinMode(MRmosfet2_3, OUTPUT);
Serial.begin(9600); // This opens serial port
// and sets data rate at 9600 bps
}
void loop()
{
byte record[2] = {0};
byte state = 0;
byte currentState = 0;
if (Serial.available()) // check if data has been sent from the computer
{
state = Serial.read(); // reads the most recent byte (from 0 - 255)
if(i % 2 == 0) // THIS PART CHECKS WERE TO FILL IN ARRAY SO AS TO HAVE MEMORY EFFECT
{
record[0] = state;
}
else
{
record[1] = state;
}
if (state>=0 && state <=63) //forward
{
currentState = state * 4; //0 - 252
analogWrite(MLmosfet1_4, currentState); //steady 5V
analogWrite(MLmosfet2_3, 0); // 0V
analogWrite(MRmosfet1_4, currentState); //steady 5V
analogWrite(MRmosfet2_3, 0); // 0V
}
else if (state>=64 && state<=127) //reverse
{
currentState = (state-64)*4; //0 - 252
analogWrite(MLmosfet1_4, 0); // 0V
analogWrite(MLmosfet2_3, currentState); // steady 5V
analogWrite(MRmosfet1_4, 0); // 0V
analogWrite(MRmosfet2_3, currentState); // steady 5V
}
else if(state>=128 && state <=191) //left
{
currentState = (state - 118); //input will be 10 - 73
if(i % 2 == 0)
{
if (record[1] >=0 && record[1] <=63) //previous action was move forward
{
analogWrite(MLmosfet1_4, currentState); //approx 25% duty cycle
analogWrite(MLmosfet2_3, 0); // 0V
analogWrite(MRmosfet1_4, record[1]); //keeps previous forward speed
analogWrite(MRmosfet2_3, 0); // 0V
}
else //means that previous action was reverse
{
analogWrite(MLmosfet1_4, 0); // 0V
analogWrite(MLmosfet2_3, currentState); // approx 25% duty cycle
analogWrite(MRmosfet1_4, 0); // 0V
analogWrite(MRmosfet2_3, record[1]); // keeps previous reverse speed
}
}
else // i is odd, therefore we have to read from reord[0]
{
if (record[0] >=0 && record[0] <=63) //previous action was move forward
{
analogWrite(MLmosfet1_4, currentState); //approx 25% duty cycle
analogWrite(MLmosfet2_3, 0); // 0V
analogWrite(MRmosfet1_4, record[0]); //keeps previous forward speed
analogWrite(MRmosfet2_3, 0); // 0V
}
else //means that previous action was reverse
{
analogWrite(MLmosfet1_4, 0); // 0V
analogWrite(MLmosfet2_3, currentState); // approx 25% duty cycle
analogWrite(MRmosfet1_4, 0); // 0V
analogWrite(MRmosfet2_3, record[0]); // keeps previous reverse speed
}
}
}
else if(state>=192 && state <=255) //right
{
currentState = (state - 118); //input will be 10 - 73
if(i % 2 == 0)
{
if (record[1] >=0 && record[1] <=63) //previous action was move forward
{
analogWrite(MLmosfet1_4, record[1]); //keeps previous forward speed
analogWrite(MLmosfet2_3, 0); // 0V
analogWrite(MRmosfet1_4, currentState); //approx 25% duty cycle
analogWrite(MRmosfet2_3, 0); // 0V
}
else //means that previous action was reverse
{
analogWrite(MLmosfet1_4, 0); // 0V
analogWrite(MLmosfet2_3, record[1]); // keeps previous reverse speed
analogWrite(MRmosfet1_4, 0); // 0V
analogWrite(MRmosfet2_3, currentState); // approx 25% duty cycle
}
}
else // i is odd, therefore we have to read from reord[0]
{
if (record[0] >=0 && record[0] <=63) //previous action was move forward
{
analogWrite(MLmosfet1_4, record[0]); //keeps previous forward speed
analogWrite(MLmosfet2_3, 0); // 0V
analogWrite(MRmosfet1_4, currentState); //approx 25% duty cycle
analogWrite(MRmosfet2_3, 0); // 0V
}
else //means that previous action was reverse
{
analogWrite(MLmosfet1_4, 0); // 0V
analogWrite(MLmosfet2_3, record[0]); // keeps previous reverse speed
analogWrite(MRmosfet1_4, 0); // 0V
analogWrite(MRmosfet2_3, currentState); // approx 25% duty cycle
}
}
}
i++;
}
}