我正在开展一个项目,我通过串口将数字发送到射频发射器。另一方面,接收器连接到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++;
}
}
答案 0 :(得分:0)
您可以通过将其中一个连接到PC(使用RS232接口)来测试发射器和接收器,将另一个连接到arduino引脚0 Rx和Tx(使用TTL接口)。一旦你连接它,将arduino连接到电源,打开arduino IDE,并设置arduino连接到COM端口1(主板上的串行接口)。只是尝试运行其中一个示例,看看它是否有效。
注意接口方法非常无能,arduino使用TTL和PC使用RS232,根据数据表它应该由一些跳线设置,我对吗?
答案 1 :(得分:0)
我首先调试没有任何代码的连接
将RF RX和RX连接到pc use putty或任何其他终端以连接到两个来端口,在TX终端上键入一些文本并查看RX终端中出现的内容。
如果这样可以将RX连接到arduino并使用终端向其发送数据。 如果现在你的arduino正在工作,那么它的VB有一个错误,如果它不工作,那么arduino代码有问题。
这些RS232问题通常与软件中COM端口的波特率设置有关。确保双方都设置为相同的波特率确保检查数据速率设置为9600的VB代码,就像在arduino上一样。
答案 2 :(得分:0)
我会使用一个简单的程序来点亮LED,以查看问题是否与电机代码无关。
你也不应该检查if (Serial.available() > 0)