首先,我是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
}
1 回答
经过几次试验,我已经达成了解决方案 .
如果我在SPI和rfid之前初始化RF(
vw_setup(2000);
),我可以读取卡并发送消息 . 我不知道它为什么会起作用,但它确实有效 . 也许有人可以解释一下 .