我正在做一个项目,我通过串行端口向射频发射器发送数字。另一方面,接收器连接到 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++;
}
}