We're using two RDM6300 RFID readers for this, and the code works when plugged into an UNO, but not when connected to the Yun. We have one of the rfids plugged into the rx pin (0 on the digital side) and the other plugged into pin 2 (same exact way we had it working on the UNO). Anyway, is there something majorly different between working with just sketches on Yun vs UNO? This code doesn't even involve using the Linux side yet as we couldn't get this major part figured out. Any help would be appreciated
#include <SoftwareSerial.h>
//2nd RFID using SoftwareSerial
SoftwareSerial RFID(2, 3); // RX and TX
// define constants for pins
int UNLOCK = 8;
// variables to keep state
int readVal = 0; // individual character read from serial
unsigned int readData[10]; // data read from serial
int counter = -1; // counter to keep position in the buffer
char tagId[11]; // final tag ID converted to a string
char* authorizedTags[4]; // array to hold the list of authorized tags
// fills the list of authorzied tags
void initAuthorizedTags() {
//tags
authorizedTags[0] = "02006750B7";
authorizedTags[1] = "02006750B1";
authorizedTags[2] = "02006750B2";
authorizedTags[3] = "02006750B3";
authorizedTags[4] = "02006750B4";
}
void setup() {
Serial.begin(9600);
//start serial to RFID reader2
RFID.begin(9600);
pinMode(UNLOCK, OUTPUT);
// pinMode(SUCCESS, OUTPUT);
// pinMode(ERROR, OUTPUT);
initAuthorizedTags();
}
// check if the tag ID we just read is any of the authorized tags
int checkTag() {
int i;
for (i = 0; i < 4; ++i) {
if (strcmp(authorizedTags[i], tagId) == 0) {
return 1;
}
}
return 0;
}
// convert the int values read from serial to ASCII chars
void parseTag() {
int i;
for (i = 0; i < 10; ++i) {
tagId[i] = readData[i];
}
tagId[10] = 0;
}
// once a whole tag is read, process it
void processTag() {
// convert id to a string
parseTag();
// print it
printTag();
// check if the tag is authorized
if (checkTag() == 1) {
tagSuccess(); // unlock solenoid
} else {
tagFailed();
}
}
void printTag() {
Serial.print("Tag value: ");
Serial.println(tagId);
}
// perform an action when an authorized tag was read
void tagSuccess() {
Serial.println("Tag authorized.");
//activate solenoid,
digitalWrite(8, HIGH);
delay(3000);
digitalWrite(8, LOW);
}
// inform the user that the tag is not authorized
void tagFailed() {
Serial.println("Unauthorized access!");
}
// this function clears the rest of data on the serial, to prevent multiple scans
void clearSerial() {
while (Serial.read() >= 0) {
; // do nothing
}
}
void loop() {
//RFID 1
if (Serial.available() > 0) {
// read the incoming byte:
readVal = Serial.read();
// a "2" signals the beginning of a tag
if (readVal == 2) {
counter = 0; // start reading
}
// a "3" signals the end of a tag
else if (readVal == 3) {
// process the tag we just read
processTag();
// clear serial to prevent multiple reads
clearSerial();
// reset reading state
counter = -1;
}
// if we are in the middle of reading a tag
else if (counter >= 0) {
// save valuee
readData[counter] = readVal;
// increment counter
++counter;
}
}
if (RFID.available() > 0)
{
// read the incoming byte:
readVal = RFID.read();
// a "2" signals the beginning of a tag
if (readVal == 2) {
counter = 0; // start reading
}
// a "3" signals the end of a tag
else if (readVal == 3) {
// process the tag we just read
processTag();
// clear serial to prevent multiple reads
clearSerial();
// reset reading state
counter = -1;
}
// if we are in the middle of reading a tag
else if (counter >= 0) {
// save valuee
readData[counter] = readVal;
// increment counter
++counter;
}
}
}
Serial
is reserved for comms between the atmega32u4 and the PC, whileSerial1
(on pins 0 and 1) is reserved for comms btw the Arduino and Linux parts of the Yun. You have to use 2SoftwareSerial
ports for your project; and it seems even that option is buggy on the Yun. But try with one reader anyways; disconnect the other from pins 0 and 1 and try printing whatever you read from theSoftSerial
reader to the PC; keep the sketch simple