gpt4 book ai didi

无法使用 Arduino Uno 使用 RF 433 MHz 发射器发送消息

转载 作者:太空宇宙 更新时间:2023-11-04 03:29:35 24 4
gpt4 key购买 nike

首先,我是 Arduino 的初学者。我有一个电路,包括:一个 Arduino Uno、一个 RFID RC522 读卡器和一个 RF 433 MHz 模块的发送器。

我正在尝试使用 RF 433MHz 发送器传输 RFID 卡的 ID 代码,但它不起作用。首先,我读取卡片,然后将 RFID 代码组成一个字符串,例如 "18016518623564"

使用下面的代码,我无法读取 RFID 卡。 (串行监视器上什么也看不到。)这似乎是 vw_setup(2000); 的问题。如果我注释掉它,我可以阅读一次代码。如果我也注释掉 vw_wait_tx();,我可以读两遍代码。如果我也注释掉 send("1");,我可以无限次地阅读代码。但是,无论我做什么,都无法发送(程序停止在 vw_wait_tx();)。

有人可以帮我解决这个问题,并且能够将 RFID 代码发送给接收者吗?

代码如下:

// includes for RFID
#include <SPI.h>
#include <RFID.h>
// includes for RF communication
#include <VirtualWire.h>
// defines for RFID
#define SS_PIN 10
#define RST_PIN 5
// variables for RFID
RFID rfid(SS_PIN, RST_PIN);
int serNum[5]; // array to store the RFID code
String rfid_string; // variable to store the concatenated card identifier

void setup() {
Serial.begin(9600);
SPI.begin();
rfid.init();
// initialize the IO and ISR for RF
vw_setup(2000); // Bits per sec
}

void loop() {
read_and_compose_rfid_code(); // read and compose the rfid code into one string
send("1"); // send 1 throug RF - just for testing
delay(1000);
}

void read_and_compose_rfid_code (void) {
if (rfid.isCard()) {
// check if rfid card is present nearby to the sensor antenna
if (rfid.readCardSerial()) {
// read card identifier into array defined in RFID class
// concatenate the card identifier as one string
rfid_string = String(rfid.serNum[0]) + String(rfid.serNum[1]) + String(rfid.serNum[2]) + String(rfid.serNum[3]) + String(rfid.serNum[4]);
Serial.print(rfid_string); // test to see if the code is properly composed
Serial.println();
}
rfid.halt();
delay(1000); // set a delay of 1 second before the next card read
}
}

/* function used to send the card identifier through RF */
void send (char *message) {
vw_send((uint8_t *)message, strlen(message));
// "message" is an array of the bytes to send, and strlen(message) is the number of bytes stored in the array
vw_wait_tx(); // wait until the whole message has been transmitted
}

最佳答案

经过多次尝试,我找到了解决方案。
如果我在 SPI 和 rfid 之前初始化 RF (vw_setup(2000);),我可以读取卡并发送消息。我不知道它为什么有效,但它确实有效。也许有人可以解释。

关于无法使用 Arduino Uno 使用 RF 433 MHz 发射器发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38021292/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com