0

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

3 回答 3

0

我会使用一个简单的程序来点亮 LED,看看问题是否与电机代码无关。你也不应该检查if (Serial.available() > 0)

于 2012-04-06T07:40:38.217 回答
0

您可以通过将其中一个连接到 PC(使用 RS232 接口)和另一个连接到 arduino 引脚 0 Rx 和 Tx(使用 TTL 接口)来测试发射器和接收器。连接好后,将 arduino 连接到电源,打开 arduino IDE,并将 arduino 设置为连接到 COM 端口 1(主板上的串行接口)。只需尝试运行其中一个示例,看看它是否有效。

请注意,接口方法非常无能为力,arduino 使用 TTL,PC 使用 RS232,根据数据表,它应该通过某种跳线设置,对吗?

于 2012-04-04T19:42:48.810 回答
0

我会首先在没有任何代码的情况下调试连接

将 RF RX 和 RX 都连接到电脑上,使用 putty 或任何其他终端连接到两个端口,在 TX 终端上键入一些文本,然后查看 RX 终端中的内容。

如果可行,请将 RX 连接到 arduino 并使用终端向其发送数据。如果现在您的 arduino 正在工作,那么它的 VB 有错误,如果它不工作,那么 arduino 代码有问题。

大多数情况下,这些 RS232 问题与软件中 COM 端口的波特率设置有关。确保双方设置为相同的波特率确保检查数据速率设置为 9600 的 VB 代码,就像在 arduino 上一样。

于 2012-04-05T06:51:43.137 回答