Voici donc le fameux code!
Il s'agit de poser 4 tags aux bons endroits. Une fois ceci fait, un relais s'ouvre. Si on retire un des tags, le relais se referme.
J'utilise ce code pour 3 mécanismes différents. Le premier mécanisme fonctionne à merveille. Le second a un dysfonctionnement dont j'aimerais vous parler; et le troisième ne fonctionne pas du tout; probablement à cause de la longueur des câbles (problème que je vais corriger grâce aux soins de @jpbbricole !)
Le deuxième mécanisme a un soucis (je vous ai mis le code de celui-ci). Il est assez dur à décrire, j'aurais dû mieux l'identifier. Au bout de plusieurs utilisations; l'Arduino ne semble plus répondre. Par exemple, si je pose mon dernier tag; le relais ne va pas s'ouvrir. Il arrive aussi parfois que lorsque je retire le dernier tag; je ne puisse plus fermer l'électro-aimant. Hélas; je n'ai jamais pu vérifier cela grâce au port com (je vais essayer d'identifier plus exactement le problème dans la journée). Pour le moment; je ne sais en effet pas si c'est un problème de relay (peu probable, car je l'ai changé deux fois); ou de carte Arduino qui planterait, peut-être? J'aurais aimé savoir si un watchdog pouvait être utile? ![]()
/**
"Object Placement" Puzzle
This puzzle requires the player to place one or more items in the
correct location, at which point an RFID tag embedded in the object
is detected by a sensor.
When all sensors read the tag of the correct object, the puzzle is solved.
Demonstrates:
- SPI interface shared between several devices
- RFID library
*/
//a rajouter
// DEFINES
// Provides debugging information over serial connection if defined
#define DEBUG
// LIBRARIES
// Standard SPI library comes bundled with the Arduino IDE
#include <SPI.h>
// Download and install from https://github.com/miguelbalboa/rfid
#include <MFRC522.h>
// CONSTANTS
// The number of RFID readers
const byte numReaders = 4;
// Each reader has a unique Slave Select pin
const byte ssPins[] = {2, 3, 4, 5};
// They'll share the same reset pin
const byte resetPin = 8;
// This pin will be driven LOW to release a lock when puzzle is solved
const byte lockPin = A0;
// The sequence of NFC tag IDs required to solve the puzzle
const String correctIDs[] = {"ba7cd880", "acf3e98c", "4ae9eb80", "7cd3f48c"};
//zeus: acf3e98c
// athéna: 7cd3f48c
//Poséidon: ba7cd880
//Hermès; 4ae9eb80
// GLOBALS
// Initialise an array of MFRC522 instances representing each reader
MFRC522 mfrc522[numReaders];
// The tag IDs currently detected by each reader
String currentIDs[numReaders];
/**
Initialisation
*/
void setup() {
#ifdef DEBUG
// Initialise serial communications channel with the PC
Serial.begin(9600);
Serial.println(F("Serial communication started"));
#endif
// Set the lock pin as output and secure the lock
pinMode(lockPin, OUTPUT);
digitalWrite(lockPin, LOW);
// Initialise the SPI bus
SPI.begin();
for (uint8_t i = 0; i < numReaders; i++) {
// Initialise the reader
// Note that SPI pins on the reader must always be connected to certain
// Arduino pins (on an Uno, MOSI=> pin11, MISO=> pin12, SCK=>pin13)
// The Slave Select (SS) pin and reset pin can be assigned to any pin
mfrc522[i].PCD_Init(ssPins[i], resetPin);
// Set the gain to max - not sure this makes any difference...
// mfrc522[i].PCD_SetAntennaGain(MFRC522::PCD_RxGain::RxGain_max);
#ifdef DEBUG
// Dump some debug information to the serial monitor
Serial.print(F("Reader #"));
Serial.print(i);
Serial.print(F(" initialised on pin "));
Serial.print(String(ssPins[i]));
Serial.print(F(". Antenna strength: "));
Serial.print(mfrc522[i].PCD_GetAntennaGain());
Serial.print(F(". Version : "));
mfrc522[i].PCD_DumpVersionToSerial();
#endif
// Slight delay before activating next reader
delay(100);
}
#ifdef DEBUG
Serial.println(F("--- END SETUP ---"));
#endif
}
/**
Main loop
*/
void loop() {
// Assume that the puzzle has been solved
boolean puzzleSolved = true;
// Assume that the tags have not changed since last reading
boolean changedValue = false;
// Loop through each reader
for (uint8_t i = 0; i < numReaders; i++) {
// Initialise the sensor
mfrc522[i].PCD_Init();
// String to hold the ID detected by each sensor
String readRFID = "";
// If the sensor detects a tag and is able to read it
if (mfrc522[i].PICC_IsNewCardPresent() && mfrc522[i].PICC_ReadCardSerial()) {
// Extract the ID from the tag
readRFID = dump_byte_array(mfrc522[i].uid.uidByte, mfrc522[i].uid.size);
}
// If the current reading is different from the last known reading
if (readRFID != currentIDs[i]) {
// Set the flag to show that the puzzle state has changed
changedValue = true;
// Update the stored value for this sensor
currentIDs[i] = readRFID;
}
// If the reading fails to match the correct ID for this sensor
if (currentIDs[i] != correctIDs[i]) {
// The puzzle has not been solved
puzzleSolved = false;
}
// Halt PICC
mfrc522[i].PICC_HaltA();
// Stop encryption on PCD
mfrc522[i].PCD_StopCrypto1();
}
#ifdef DEBUG
// If the changedValue flag has been set, at least one sensor has changed
if (changedValue) {
// Dump to serial the current state of all sensors
for (uint8_t i = 0; i < numReaders; i++) {
Serial.print(F("Reader #"));
Serial.print(String(i));
Serial.print(F(" on Pin #"));
Serial.print(String((ssPins[i])));
Serial.print(F(" detected tag: "));
Serial.println(currentIDs[i]);
}
Serial.println(F("---"));
}
#endif
// If the puzzleSolved flag is set, all sensors detected the correct ID
if (puzzleSolved && changedValue) {
onSolve();
}
else if (!puzzleSolved) {
digitalWrite(lockPin, LOW);
}
// Add a short delay before next polling sensors
//delay(100);
}
/**
Called when correct puzzle solution has been entered
*/
void onSolve() {
#ifdef DEBUG
// Print debugging message
Serial.println(F("Puzzle Solved!"));
#endif
// Release the lock
delay(1000);
digitalWrite(lockPin, HIGH);
Serial.println("Electro déverrouillé");
delay(2000);
//while(true) {
//delay(2000);
//digitalWrite(lockPin, HIGH);
//}
}
/**
Helper function to return a string ID from byte array
*/
String dump_byte_array(byte *buffer, byte bufferSize) {
String read_rfid = "";
for (byte i = 0; i < bufferSize; i++) {
read_rfid = read_rfid + String(buffer[i], HEX);
}
return read_rfid;
}