Hello all,
I am trying to send data from 7 RFID MFRC522 readers, each connected to an Arduino behaving as a master, to a single shared Arduino that is behaving as a slave. So 7 master Arduinos and 1 slave Arduino. I've done this so I can control when the data would be sent to the slave Arduino as it should only transmit data when an RFID tag is read by the reader. Atleast, from my basic understanding of I2C protocol, this is what I've implemented because I need to only send data when I want to.
To get started, I only connected one Arduino(Master) to the slave. It worked perfectly and data is being transmitted as I need it to. Now, when I connected two Arduinos (Master) to the slave, nothing is working. None of the masters are sending data to the slave or maybe if they are, it isn't showing on the serial monitor over my slave.
P.S. I haven't used any pull-up resistors as a single master-slave connection was working without any. Is this the problem? If so, how do I use them for a design where I have 7 Masters and 1 slave.
Master Arduino#1
#include <SPI.h>
#include <MFRC522.h>
#include <FileIO.h>
#include <Wire.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
Wire.begin(); // join i2c bus (address optional for master)
//while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
//mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
//Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
pinMode(50,INPUT);
}
void loop() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
//mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
//Serial.print("UID Tag: ");
String content= "";
byte letter;
for (byte i=0; i<mfrc522.uid.size;i++)
{
//Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
//Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? "0": " "));
content.concat(String(mfrc522.uid.uidByte[i] , HEX));
}
Serial.println();
Serial.print("Message : ");
content.toUpperCase();
if (content.substring(1) =="30 D6 3D A6")
{
Wire.beginTransmission(8); // transmit to slave
Wire.write("Person 1 is in the premises");
delay(1000);
Wire.endTransmission(); // stop transmitting
}
if (content.substring(1) =="A0 47 CE A4"){
Wire.beginTransmission(8); // transmit to slave
Wire.write("Person 2 is in the premises");
delay(1000);
Wire.endTransmission(); // stop transmitting
}
Now, this is the code I wrote for the second Master. I changed the adress from 8 to 9.
Master Arduino#2
#include <SPI.h>
#include <MFRC522.h>
#include <FileIO.h>
#include <Wire.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
Wire.begin(); // join i2c bus (address optional for master)
//while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
//mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
//Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
pinMode(50,INPUT);
}
void loop() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
//mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
//Serial.print("UID Tag: ");
String content= "";
byte letter;
for (byte i=0; i<mfrc522.uid.size;i++)
{
//Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
//Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? "0": " "));
content.concat(String(mfrc522.uid.uidByte[i] , HEX));
}
Serial.println();
Serial.print("Message : ");
content.toUpperCase();
if (content.substring(1) =="30 D6 3D A6")
{
Wire.beginTransmission(9); // transmit to slave
Wire.write("Person 1 is in the premises");
delay(1000);
Wire.endTransmission(); // stop transmitting
}
if (content.substring(1) =="A0 47 CE A4"){
Wire.beginTransmission(9); // transmit to slave
Wire.write("Person 2 is in the premises");
delay(1000);
Wire.endTransmission(); // stop transmitting
}
And this is the code for my slave:
#include <Wire.h>
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.begin(9); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
}
void loop() {
delay(100);
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
while ( Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
}