TWO RFID READERS and ARDUINO MEGA, only one reader will read - arduino

I am trying to run 2 rfid readers (RDM 630) in an Arduino Mega 2560. I just can't figure it out why only one reader will read and the other won't. (The readers are both functional).
#include <SoftwareSerial.h>
SoftwareSerial Reader1(50, 51);
SoftwareSerial Reader2(52, 53);// RX and TX
int rfid, i;
char newtag[14];
void setup()
{
Reader1.begin(9600); // start serial to RFID reader
Reader2.begin(9600);
Serial.begin(9600); // start serial to PC
}
void loop()
{
if (Reader1.available() > 0)
{
Serial.println();
Serial.println();
Serial.println("Reading RFID Tag...");
delay(100);
for (i=0; i < 13; i++)
{
rfid = Reader1.read();
newtag[i]=rfid;
}
Reader1.flush();
Serial.print("RFID Tag No:");
Serial.print(newtag);
}
if (Reader2.available() > 0)
{
Serial.println();
Serial.println();
Serial.println("Reading RFID Tag...");
delay(100);
for (i=0; i < 13; i++)
{
rfid = Reader2.read();
newtag[i]=rfid;
}
Reader2.flush();
Serial.print("RFID Tag No:");
Serial.print(newtag);
}
}

SofwareSerial has shared resources so needs a little extra thought on implementation. When you initialise a device it is the listening device. If you initialise another the listening device changes. You need to put:
Reader1.listen();
Before your Reader1 code and switch again for your Reader2 code.
See this for example code: http://arduino.cc/en/Tutorial/TwoPortReceive
However, you say you have a Mega. Why not use the multiple serials you have onboard? Your code base will be smaller and the coding is cleaner. http://arduino.cc/en/Tutorial/MultiSerialMega

After around million of trying and trying I discovered that the correct way to connect 4 RFID RC522 is to put them in the same line on test board except SS pins and the code as usual is ReadUidMultiReader from RFID library like this :

Related

Having problems blinking an LED on Arduino using information sent over CAN-bus

I've been messing around with CAN-bus to try and learn the basics so I can use it in a future project. I have 2 arduinos with MCP2515 chips connected that I am sending messages between.
I have been able to wire up the chips and send messages between the arduinos just fine, but when I went to modify the code to blink an LED if the first byte is 0x00 or 0x01 it wont blink. I added print statements to check that it is entering the loop, and it is, using the serial monitor I can see it yeet and yote, but digital pin 3 remains at ~0V.
This is probably more an arduino question than a CAN-bus question but could somebody help me understand why my LED wont blink? The code is entering the loop so it should be processing the command, and I initialized the pin to be an output, but i'm still not getting any blinking.
As a note, the transmitting arduino is sending alternating packets of data, first a packet with 0X01 as the first data byte, then with 0x00 as the first data byte. These data packets are separated by 5000 ms.
I am currently using the CAN library available here https://github.com/autowp/arduino-mcp2515
Code for receiving arduino
#include <SPI.h>
#include <mcp2515.h>
struct can_frame canMsg;
MCP2515 mcp2515(10);
int LED_PIN = 3;
void setup() {
Serial.begin(115200);
SPI.begin();
pinMode(LED_PIN, OUTPUT);
mcp2515.reset();
mcp2515.setBitrate(CAN_125KBPS);
mcp2515.setNormalMode();
Serial.println("------- CAN Read ----------");
Serial.println("ID DLC DATA");
}
void loop() {
if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {
Serial.print(canMsg.can_id, HEX); // print ID
Serial.print(" ");
Serial.print(canMsg.can_dlc, HEX); // print DLC
Serial.print(" ");
if(canMsg.data[0] == 0x00){
digitalWrite(LED_PIN,HIGH);
Serial.print("YEET");
}
if(canMsg.data[0] == 0x01){
digitalWrite(LED_PIN,LOW);
Serial.print("YOTE");
}
for (int i = 0; i<canMsg.can_dlc; i++) { // print the data
Serial.print(canMsg.data[i],HEX);
Serial.print(" ");
}
Serial.println();
}
}
and the code for transmitting arduino for completeness
#include <SPI.h>
#include <mcp2515.h>
struct can_frame canMsg1;
struct can_frame canMsg2;
MCP2515 mcp2515(10);
void setup() {
canMsg1.can_id = 0x0F6;
canMsg1.can_dlc = 8;
canMsg1.data[0] = 0x01;
canMsg1.data[1] = 0x87;
canMsg1.data[2] = 0x32;
canMsg1.data[3] = 0xFA;
canMsg1.data[4] = 0x26;
canMsg1.data[5] = 0x8E;
canMsg1.data[6] = 0xBE;
canMsg1.data[7] = 0x86;
canMsg2.can_id = 0x036;
canMsg2.can_dlc = 8;
canMsg2.data[0] = 0x00;
canMsg2.data[1] = 0x00;
canMsg2.data[2] = 0x00;
canMsg2.data[3] = 0x08;
canMsg2.data[4] = 0x01;
canMsg2.data[5] = 0x00;
canMsg2.data[6] = 0x00;
canMsg2.data[7] = 0xA0;
while (!Serial);
Serial.begin(115200);
SPI.begin();
mcp2515.reset();
mcp2515.setBitrate(CAN_125KBPS);
mcp2515.setNormalMode();
Serial.println("Example: Write to CAN");
}
void loop() {
mcp2515.sendMessage(&canMsg1);
delay(5000);
mcp2515.sendMessage(&canMsg2);
Serial.println("Messages sent");
delay(5000);
}
Sorry, i'm an idiot. I forgot that breadboard power lines are not connected all the way down the board. I connected the Arduino to the proper section of the breadboard to receive power and ground, but connected the LED to an unconnected ground pin.

Arduino LORA Send and Receive Data

I tried to allow sending and receiving data using 2 Arduino Unos, 2 LORA chips (SX1278 433MHz), 2 antennas and 2 Arduino IDE.
Problem is with the receiving data command.
This is the code for SENDING command:
#include <SPI.h>
#include <LoRa.h>
int counter = 0;
const int ss = 10;
const int reset = 9;
const int dio0 = 2;
void setup() {
Serial.begin(9600);
LoRa.begin(433E6);
LoRa.setPins(ss, reset, dio0);
while (!Serial);
Serial.println("LoRa Sender");
}
void loop() {
Serial.print("Sending packet: ");
Serial.println(counter);
// send packet
LoRa.beginPacket();
LoRa.print("hello ");
LoRa.print(counter);
LoRa.endPacket();
counter++;
delay(5000);
}
On serial monitor, I succeed in sending packages, but not receiving. This is the RECEIVING code:
#include <SPI.h>
#include <LoRa.h>
const int ss = 10;
const int reset = 9;
const int dio0 = 2;
void setup() {
Serial.begin(9600);
LoRa.begin(433E6);
LoRa.setPins(ss, reset, dio0);
while (!Serial);
Serial.println("LoRa Receiver");
}
void loop() {
// try to parse packet
int packetSize = LoRa.parsePacket();
if (packetSize) {
// received a packet
Serial.print("Received packet '");
// read packet
while (LoRa.available()) {
Serial.print((char)LoRa.read());
Serial.print("hello ");
}
// print RSSI of packet
Serial.print("' with RSSI ");
Serial.println(LoRa.packetRssi());
}
}
I used instructions about connections from this git page:
https://github.com/sandeepmistry/arduino-LoRa
To make this board work, I had to explicitly initialize SPI
SPI.begin(/*sck*/ 5, /*miso*/ 19, /*mosi*/ 27, /*ss*/ cs);
It may be the same for yours. Also, you should properly initialize Lora:
while (!LoRa.begin(433E6)) {
Serial.print(".");
delay(500);
}
Serial.println("LoRa Initializing OK!");
With the code you posted, you don't really know if it initialized correcly or if it's actually sending any data.
First I haven't worked with Lora library. I worked with SX1278 libaray. So I can help you with that.
At first here is the link to the libaray - Lora SX1278.h library
Now you might ask why I'm not using the library from the original GitHub repo. Well I faced issue with that library and the issue is this:
The sx1278::getPacket() library function is modified to stabilize the Lora receive functionality. There was a bug which led the esp to panic. The payloadlength read from the REG_FIFO register was not checked for valid value which led to reading the REG_FIFO register read for over 65000 times. Moreover yield() is added in time consuming parts of this function.
That is why I'm using this custom library. Anyway, for you:
You can use this function to send packet:
T_packet_state = sx1278.sendPacketTimeoutACK(ControllerAddress, message); //T_packet_state is 0 if the packet data is sent successful.
Also to receive data use this function:
R_packet_state = sx1278.receivePacketTimeoutACK(); //R_packet_state is 0 if the packet data is received successfully.
And at the beginning of the code just define this few things:
//Lora SX1278:
#define LORA_MODE 10 //Add your suitable mode from the library
#define LORA_CHANNEL CH_6_BW_125 //Ch number and Bandwidth
#define LORA_ADDRESS 3 //Address of the device from which you will send data
uint8_t ControllerAddress = 5; //Address of receiving end device
My first decent answer on StackOverflow. Finger Crossed

Arduino Nano Gnss Software Serial

I want to connect Arduino nano and GNSS (SIMCom’s SIM33ELA standalone GNSS module).
First I wrote a program for rx/tx, which is worked well, but now I want to use Software Serial and I got something wrong data.
#include <SoftwareSerial.h>
char incomingByte; // for incoming serial data
double tbs;
SoftwareSerial mySerial(8, 9); // RX, TX
void setup() {
Serial.begin(115200);
while (!Serial) {
}
mySerial.begin(115200);
while (!mySerial) {
}
}
void loop() {
if (mySerial.available()) {
tbs = mySerial.read();
incomingByte = (char)tbs;
Serial.print(incomingByte);
}
/*if (Serial.available() > 0) {
incomingByte = Serial.read();
Serial.print(incomingByte);
}*/
}
Any Idea?
Pictures about results:
Wrong data with Software serial
Good data with Serial
Mostly, don't read one character into a double floating-point variable. Just do this:
void loop()
{
if (mySerial.available()) {
char c = mySerial.read();
Serial.write( c );
}
}
You should also use AltSoftSerial on those two pins. SoftwareSerial is very inefficient, because it disables interrupts for long periods of time. It cannot transmit and receive at the same time. In fact, the Arduino can do nothing else while a character is transmitted or received.
For a GPS library, you could try NeoGPS. It's the only Arduino library that can parse the sentences from the newest devices. It's also smaller, faster, more reliable and more accurate than all other libraries.

MicroSD reader causing RFID reader to fail

I have a sketch running a MFRC522 RFID reader which works fine but my aim is to log the card swipes on a microSD card.
The problem is that as soon as I add the SD card reader, whether it is initialised or not, the RFID reader stops working.
It seems to be a problem on the SPI bus. I have tried adding pull up resistors to the circuit and setting the chip select pins to HIGH before initialising either of the boards but nothing seems to work.
Here is my code:
#include <SPI.h>
#include <MFRC522.h>
#include <SD.h>
// RFID constants & objects
#define RFPin 10
#define resetPin 9
MFRC522 mfrc522(RFPin, resetPin);
// SD constants
#define SDPin 8
// Other global variables
String IDString;
byte IDList[4];
void setup() {
pinMode(2, OUTPUT);// For testing
// Set both chip select pins high
pinMode(SDPin, OUTPUT);
digitalWrite(SDPin, HIGH);
pinMode(RFPin, OUTPUT);
digitalWrite(RFPin, HIGH);
delay(10);
// Init serial bus
Serial.begin(9600);
// Wait for serial bus to open
while (!Serial);// Opens even when not USB connected
// Init SPI bus
SPI.begin();
// Initialise RFID board
mfrc522.PCD_Init();
delay(1000);// Just in case SPI is still busy
// Initialise SD card board
if (!SD.begin(SDPin)) {
// SD card board failed to initialise
Serial.println("SD failed");
return;
}
}
void loop() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
for(int i = 0; i < 4; i++) {
digitalWrite(2, HIGH);
delay(100);
digitalWrite(2, LOW);
delay(100);
}
IDString = "";
// Get 4 byte ID
for (int i = 0; i < 4; i++) {
IDList[i] = mfrc522.uid.uidByte[i];
// Serial.print(IDList[i], HEX);
if(IDList[i] < 16) {
IDString += "0" + String(IDList[i], HEX) + "-";
} else {
IDString += String(IDList[i], HEX) + "-";
}
}
mfrc522.PICC_HaltA(); // Stop reading
IDString = IDString.substring(0, IDString.length() - 1);
Serial.println(IDString);
.
.
.
Rest of code
Everything initialised ok but Once in the main loop, the program never gets past the first statement: to check if an RFID card is present.
Can anybody help?
Could it be de to the fact that the RFDI board is 3.3V driven and the SD board is 5V driven (but has a 3.3V regulator)? Each has it's own separate power wire
EDIT #1:
When the MicroSD board is plugged in but not powered the RFID board works fine
I have also tried adding a subroutine to pull both chip select pins high at the beginning of every loop to no avail.
EDIT #2:
The SD board works in this sketch and I can get the card details from it.
EDIT #3:
The RFID card works again once I remove the MISO line from the SD board.
Obviously the SD module isn't releasing the MISO line...
It's not the most elegant solution but I have it working now by attaching an NPN transistor between the MISO output of the SD board and the MISO line to pin 12. It takes one further pin to block/unblock the MISO line to be used by the SD board but it works.
As I said this is not elegant and I would still be eager to hear a better solution.

C code equivalent to serial monitor commands like Serial.begin,serial.flush,serial.read,serial.available,etc

Hi I am trying to learn coding microcontrollers on my own. I am trying to code my arduino board (ATMEGA8A-PU) in embedded C using the arduino ide itself. I have blinked my LED so far. Now I am trying to control its state using the serial monitor(sending "on" lights it up and "off" switches it off). But I don't know the C commands to do it.I successfully did it using the arduino Serial commands.
int led = 13; // Pin 13
void setup()
{
pinMode(led, OUTPUT); // Set pin 13 as digital out
// Start up serial connection
Serial.begin(9600); // baud rate
Serial.flush();
}
void loop()
{
String input = "";
// Read any serial input
while (Serial.available() > 0)
{
input += (char) Serial.read(); // Read in one char at a time
delay(5); // Delay for 5 ms so the next char has time to be received
}
if (input == "on")
{
digitalWrite(led, HIGH); // on
}
else if (input == "off")
{
digitalWrite(led, LOW); // off
}
}
So please help.
You are using Serial class and functions like begin(), print() etc from the Serial class which is C++ language. These are the C++ commands native to the Arduino development environment. So anyway you ar using C/C++ commands here.

Resources