Hello, can someone please help me in this.
i'm trying to make the master-slave connection between 2 Arduino.
so This is the code used for the master and the slave, and the problem is in the received data from the slave, i dont get why its "? ? ? ?.."
Your help will be much appreciated.
Code slave:
// Include Arduino Wire library for I2C
#include <Wire.h>
// Define Slave I2C Address
#define SLAVE_ADDR 9
// Define Slave answer size
#define ANSWERSIZE 48
// Define nombre de NFC tag
#define TAGSNUM 4
// Define string with response to Master
String answer = "";
//Include RFID library
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key;
// Init array that will store new NUID
byte nuidPICC[4];
int count = 0;
String tags[TAGSNUM];
void setup() {
// Setup Serial Monitor
Serial.begin(9600);
Serial.println("OCEAN PROJET / SLAVE");
SPI.begin();
rfid.PCD_Init(); // Init MFRC522
for (byte i = 0; i < 6; i++) {
key.keyByte = 0xFF;
- }*
- // Initialize I2C communications as Slave*
- Wire.begin(SLAVE_ADDR);*
- // Function to run when data received from master*
- Wire.onReceive(receiveEvent);*
- // Function to run when data requested from master*
- Wire.onRequest(requestEvent);*
}
void receiveEvent() {
-
// Read while data received*
-
while (0 < Wire.available()) {*
-
byte x = Wire.read();*
-
}*
-
// Print to Serial Monitor*
-
Serial.println("Receive event");*
}
void requestEvent() { -
for (byte i = 0; i < TAGSNUM; i++) {*
_ Serial.println(tags*);_
_ }_
_ // Setup byte variable in the correct size*_
* byte response[ANSWERSIZE];*
* // Format answer as one string for easy reading*
* for (byte i = 0; i < TAGSNUM; i++) {*
_ answer += tags*;
}
// Format response as array to send to master*
* for (byte i = 0; i < ANSWERSIZE; i++) {
response = (byte)answer.charAt(i);
}
//for (byte i = 0; i < ANSWERSIZE; i++) {
// Serial.print(response);
//}*_
* // Send response back to Master*
* Wire.write(response,sizeof(response));*
* // Print to Serial Monitor*
* Serial.println("Request event");*
}
void loop() {
* delay(500);*
* requestEvent();*
* // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.*
* if ( ! rfid.PICC_IsNewCardPresent())
_ return;
// Verify if the NUID has been readed*
* if ( ! rfid.PICC_ReadCardSerial())*
* return;
// Check is the PICC of Classic MIFARE type*
* MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
piccType != MFRC522::PICC_TYPE_MIFARE_4K) {*
* Serial.println(F("Your tag is not of type MIFARE Classic."));
return;
}
if (rfid.uid.uidByte[0] != nuidPICC[0] ||
rfid.uid.uidByte[1] != nuidPICC[1] ||
rfid.uid.uidByte[2] != nuidPICC[2] ||
rfid.uid.uidByte[3] != nuidPICC[3] ) {
// Serial.print(F("A new card has been detected. "));
// Store NUID into nuidPICC array*
* for (byte i = 0; i < 4; i++) {_
nuidPICC _= rfid.uid.uidByte;
}
tags[count] = printHex(rfid.uid.uidByte, rfid.uid.size);
Serial.print(F("The NUID tag is:"));
Serial.print(tags[count]);
Serial.println();
count++;
}
else Serial.println(F("Card read previously."));
// Halt PICC*
* rfid.PICC_HaltA();*
* // Stop encryption on PCD*
* rfid.PCD_StopCrypto1();*
}
/**
* Helper routine to dump a byte array as hex values to Serial.
/
String printHex(byte *buffer, byte bufferSize) {
String str = "";
for (byte i = 0; i < bufferSize; i++) {
str += (buffer < 0x10 ? " 0" : " ");
str += String(buffer, HEX);
//Serial.print(buffer < 0x10 ? " 0" : " ");
//Serial.print(buffer, HEX);
}
return str;
}
Code master:
// Include Arduino Wire library for I2C*
#include <Wire.h>
// Define Slave I2C Address
#define SLAVE_ADDR 9
// Define Slave answer size
#define ANSWERSIZE 48
void setup() {_
* // Initialize I2C communications as Master*
* Wire.begin();*
* // Setup serial monitor*
* Serial.begin(9600);*
* Serial.println("MASTER");*
}
void loop() {
* delay(500);*
* Serial.println("Write data to slave");*
* // Write a charater to the Slave*
* Wire.beginTransmission(SLAVE_ADDR);
_ Wire.write(0);
Wire.endTransmission();*_
* Serial.println("Receive data");*
* // Read response from Slave*
* // Read back 5 characters*
* Wire.requestFrom(SLAVE_ADDR,ANSWERSIZE);*
* // Add characters to string*
* String response = "";*
* while (Wire.available()) {*
* char b = Wire.read();*
* response += b;*
* }*
* // Print to Serial Monitor*
* Serial.println(response);*
}
