简体   繁体   English

使用 RF 模块控制带有 Arduino 的电机

[英]Using an RF module to control a motor with Arduino

I am working on a project in which 2 Arduinos are used to use wireless comunnication with the help of RF modules.我正在从事一个项目,其中 2 个 Arduino 用于在 RF 模块的帮助下使用无线通信。 The goal of the communication is to drive a motor wirelessly.通信的目的是无线驱动电机。

So far, I have written code for the transmitter and receiver based on example code found on this page: https://randomnerdtutorials.com/rf-433mhz-transmitter-receiver-module-with-arduino/到目前为止,我已经根据此页面上的示例代码编写了发射器和接收器的代码: https://randomnerdtutorials.com/rf-433mhz-transmitter-receiver-module-with-arduino/

Following code is used to drive the transmitter module.以下代码用于驱动发射器模块。 This code is similar to the example code with modified output.此代码类似于修改了 output 的示例代码。

#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile

RH_ASK driver;

void setup()
{
    Serial.begin(9600);    // Debugging only
    if (!driver.init())
         Serial.println("init failed");
}

void loop()
{
    const char *msg = "U";
    driver.send((uint8_t *)msg, strlen(msg));
    driver.waitPacketSent();
    delay(1000);
}

This code is used to receive the message and drive the motor此代码用于接收消息和驱动电机

#include <RH_ASK.h>
#include <SPI.h>

const int motorPin1 = 2;
const int motorPin2 = 3;
RH_ASK driver(2000, 7, 6, 5);

const char *Up = "U";
const char *Down = "D";

void setup() {
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  Serial.begin(9600);
  if (!driver.init())
    Serial.println("init failed");
}

void loop() {
    uint8_t buf[1];
    uint8_t buflen = sizeof(buf);
    if (driver.recv(buf, &buflen)) // Non-blocking
    {
      int i;
      // Message with a good checksum received, dump it.
      if ((char*)buf == Up){
        digitalWrite(motorPin1, HIGH);
        digitalWrite(motorPin2, LOW);
      }
      else if ((char*)buf == Down){
        digitalWrite(motorPin1, LOW);
        digitalWrite(motorPin2, HIGH);
      }
        else{
        digitalWrite(motorPin1, LOW);
        digitalWrite(motorPin2, LOW);
      }
      Serial.println((char*)buf);
    }
}

The final line in the receiver code is used to check via the serial interface if any message is received.接收器代码中的最后一行用于通过串行接口检查是否收到任何消息。 This is the case.是这样的。 But for some reason, the message received is not the same as the provided control text.但是由于某种原因,收到的消息与提供的控制文本不一样。 In essence, the send "U" is not the same as the character "U".本质上,发送“U”与字符“U”不同。

I suppose there is something wrong with the datatype of those variables.我想这些变量的数据类型有问题。 Anyone have any ideas what the problem could be?任何人都知道问题可能是什么?

This code is comparing pointers:此代码正在比较指针:

 if ((char*)buf == Up){

else if ((char*)buf == Down){

You could change it to compare the characters that are pointed to, for example:您可以更改它以比较指向的字符,例如:

if (buf[0] == Up[0]){

or或者

if( buf[0] == 'U' )

or或者

if( memcmp( buf, Up, 1 ) == 0 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM