Bonjour, après de multiple tentatives, je n'y arrive toujours pas. Etrange lorsque je sais que j'ai réussi pour d'autre arduino. D'ailleurs j'utilise un arduino nano. Bref, trêve de bavardage. Je dois créer un props avec 6 rfid. J'ai déjà réussi à en créer avec 15, 7, 3 et d'autre encore mais là je sèche... Dès que je reset l'arduino, certains rfid fonctionnent, d'autres non, et cela change à chaque fois. Quelq'un a une solution s'il vous plaît ? Merci d'avance.
PS : Je sais que le code n'est pas super clean, surtout les conditions mais c'était juste du fonctionnel pour l'instant.
#include <MFRC522.h>
#include <SPI.h>
#define RST_PIN 9
#define SS_PIN1 6
#define SS_PIN2 3
#define SS_PIN3 4
#define SS_PIN4 5
#define SS_PIN5 2
#define SS_PIN6 8
MFRC522 rfid1(SS_PIN1, RST_PIN); // Instance of the class for RFID 1
MFRC522 rfid2(SS_PIN2, RST_PIN); // Instance of the class for RFID 2
MFRC522 rfid3(SS_PIN3, RST_PIN); // Instance of the class for RFID 3
MFRC522 rfid4(SS_PIN4, RST_PIN); // Instance of the class for RFID 4
MFRC522 rfid5(SS_PIN5, RST_PIN); // Instance of the class for RFID 5
MFRC522 rfid6(SS_PIN6, RST_PIN); // Instance of the class for RFID 6
String uidString1;
String uidString2;
String uidString3;
String uidString4;
String uidString5;
String uidString6;
String correctValueForRFID1[] = { "4 182 102 142", "4 30 109 142", "4 120 112 142" }; //1
String correctValueForRFID2[] = { "4 17 103 142", "4 222 118 142", "4 253 118 142" }; //2
String correctValueForRFID3[] = { "4 241 102 142", "4 214 112 142" , "4 190 118 142" }; //3
String correctValueForRFID4[] = { "4 213 102 142", "4 151 112 142", "4 183 112 142" }; //4
String correctValueForRFID5[] = { "4 173 108 142", "4 28 119 142", "4 36 122 142" }; //5
String correctValueForRFID6[] = { "4 205 108 142", "4 68 122 142", "4 100 122 142" }; //6
int nbRFID = 6;
bool cardDetected1;
bool cardDetected2;
bool cardDetected3;
bool cardDetected4;
bool cardDetected5;
bool cardDetected6;
bool cardIsOk1;
bool cardIsOk2;
bool cardIsOk3;
bool cardIsOk4;
bool cardIsOk5;
bool cardIsOk6;
int Relais = 7;
bool open;
void setup() {
Serial.begin(9600);
pinMode(Relais, OUTPUT);
digitalWrite(Relais, LOW);
SPI.begin(); // Init SPI bus
rfid1.PCD_Init(); // Init MFRC522 for RFID 1
delay(500);
rfid2.PCD_Init(); // Init MFRC522 for RFID 2
delay(500);
rfid3.PCD_Init(); // Init MFRC522 for RFID 3
delay(500);
rfid4.PCD_Init(); // Init MFRC522 for RFID 4
delay(500);
rfid5.PCD_Init(); // Init MFRC522 for RFID 5
delay(500);
rfid6.PCD_Init(); // Init MFRC522 for RFID 6
Serial.println("Initialization");
}
void loop() {
readRFIDCard(rfid1, uidString1, cardDetected1, cardIsOk1, correctValueForRFID1);
readRFIDCard(rfid2, uidString2, cardDetected2, cardIsOk2, correctValueForRFID2);
readRFIDCard(rfid3, uidString3, cardDetected3, cardIsOk3, correctValueForRFID3);
readRFIDCard(rfid4, uidString4, cardDetected4, cardIsOk4, correctValueForRFID4);
readRFIDCard(rfid5, uidString5, cardDetected5, cardIsOk5, correctValueForRFID5);
readRFIDCard(rfid6, uidString6, cardDetected6, cardIsOk6, correctValueForRFID6);
//Vérification, est-ce que les badges sont les bons ?
if (cardDetected1 && cardIsOk1 && cardDetected2 && cardIsOk2 && cardDetected3 && cardIsOk3 && cardDetected4 && cardIsOk4 && cardDetected5 && cardIsOk5 && cardDetected6 && cardIsOk6 && !open) {
open = true;
digitalWrite(Relais, HIGH);
Serial.println("OPEN !");
delay(1000);
} else if ((!cardDetected1 || !cardDetected2 || !cardDetected3 || !cardDetected4 || !cardDetected5 || !cardDetected6) && open ) {
delay(30000); //Wait 30 seconds bfore reset
open = false;
digitalWrite(Relais, LOW);
Serial.println("CLOSE !");
}
}
void readRFIDCard(MFRC522& rfid, String& uidString, bool& cardDetected, bool& cardOk, String correctValueForRFID[]) {
static int attemptCount = 0; // Variable statique pour compter les tentatives
// Détecte s'il y a une carte ou non
if (rfid.PICC_IsNewCardPresent()) {
cardDetected = true;
attemptCount = 0; // Réinitialise les tentatives lorsqu'une carte est détectée
// Lire les données de la carte
readRFID(&rfid, uidString, cardDetected, correctValueForRFID, cardOk);
// DEBUG
Serial.print(cardDetected ? "1" : "0");
Serial.println(cardOk ? " : 1" : " : 0");
} else {
// Si aucune carte n'est détectée, on augmente le compteur de tentatives
if (!cardDetected) {
attemptCount++;
}
// Une fois 5 tentatives effectuées sans détection, on réinitialise les variables
if (attemptCount >= 5) {
cardDetected = false;
uidString = "";
cardOk = false;
attemptCount = 0; // Réinitialise le compteur après 5 tentatives
// DEBUG
Serial.print(cardDetected ? "1" : "0");
Serial.println(cardOk ? " : 1" : " : 0");
}
}
}
void readRFID(MFRC522* rfid, String& uidString, bool& cardDetected, String correctValueForRFID[], bool& cardOk) {
rfid->PICC_ReadCardSerial();
Serial.print(F("\nPICC type: "));
MFRC522::PICC_Type piccType = rfid->PICC_GetType(rfid->uid.sak); // Use -> instead of .
//Serial.println(rfid->PICC_GetTypeName(piccType)); // Use -> instead of .
// // Check if the PICC is of Classic MIFARE type
// 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;
// }
Serial.println("Scanned PICC's UID:");
//printDec(rfid->uid.uidByte, rfid->uid.size);
uidString = String(rfid->uid.uidByte[0]) + " " + String(rfid->uid.uidByte[1]) + " " + String(rfid->uid.uidByte[2]) + " " + String(rfid->uid.uidByte[3]);
Serial.println(uidString);
Serial.println("-----------------------------");
//Verification, si la carte est bonne ou non
for (int i = 0; i < 4; i++) {
if (cardDetected) {
if (uidString == correctValueForRFID[i]) {
//Serial.println("\nI know this card! " + correctValueForRFID1[i]);
cardOk = true;
break;
} else {
//Serial.println("\nI don't know this card! " + correctValueForRFID1[i]);
cardOk = false;
}
}
}
}