Bonjour j'ai ce code ci-joint fonctionnel pour un projet de serrure électronique NFC j'utilise le shield NFC V2.1 de chez SEEED Studio : lien vers Gotronic avec la fiche technique
mon code:
#include <NfcAdapter.h>
#include <PN532/PN532/PN532.h>
#if 1
#include <SPI.h>
#include <PN532/PN532_SPI/PN532_SPI.h>
PN532_SPI pn532spi(SPI, 10);
NfcAdapter nfc = NfcAdapter(pn532spi);
#else
#include <Wire.h>
#include <PN532/PN532_I2C/PN532_I2C.h>
PN532_I2C pn532_i2c(Wire);
NfcAdapter nfc = NfcAdapter(pn532_i2c);
#endif
const byte Buzzer_PIN = 2;
const byte LedR_PIN = 3;
const byte LedV_PIN = 4;
const byte Serrure_PIN = 5;
unsigned long chrono = 0ul;
unsigned long chronoLED = 0ul;
const unsigned long TimeOutEtat = 7000ul;
int LedState = LOW;
boolean PremiereFoisRepos = false;
boolean PremiereFoisOuvert = false;
boolean PremiereFoisADD = false;
boolean PremiereFoisDEL = false;
enum {REPOS, ETATADD, ETATDEL, ETATOUVERT} EtatSysteme;
int CarteAdminADD = "EB 3F B7 FF";
int CarteAdminDEL = "5C 83 B5 FF";
int acces1 = "04 AF C7 0A 93 13 90";
int acces2 = "04 7B F 0A 93 13 94";
void setup() {
pinMode (Buzzer_PIN, OUTPUT);
pinMode (LedR_PIN, OUTPUT);
pinMode (LedV_PIN, OUTPUT);
pinMode (Serrure_PIN, OUTPUT);
nfc.begin ();
EtatSysteme = REPOS;
Serial.begin (9600);
tone (Buzzer_PIN, 900, 50);
delay (100);
tone (Buzzer_PIN, 900, 50);
delay (50);
Serial.println ("");
Serial.println ("");
Serial.println ("");
Serial.println ("");
Serial.println ("");
Serial.println ("");
Serial.println ("");
SERIAL.println ("Serrure Atelier");
Serial.println ("");
Serial.println ("Setup & Serial OK");
Serial.println ("Etat actuel = Initialisation finie");
Serial.println ("");
}
void loop() {
while (EtatSysteme == ETATOUVERT) {
if (!PremiereFoisOuvert) {
Serial.println ("Etat actuel = Ouverture ");
PremiereFoisOuvert = true;
}
PremiereFoisRepos = false;
Ouverture ();
if (millis () - chrono >= TimeOutEtat) {
TimeOut ();
chrono = millis ();
}
}
while (EtatSysteme == ETATADD) {
if (!PremiereFoisADD) {
Serial.println("Etat actuel = Etat Admin: ajout d'un utilisateur");
PremiereFoisADD = true;
}
PremiereFoisRepos = false;
BlinkLED();
//ajouter un utilisateur autorisé
if (millis () - chrono >= TimeOutEtat) {
TimeOut ();
chrono = millis ();
}
}
while (EtatSysteme == ETATDEL) {
if (!PremiereFoisDEL) {
Serial.println("Etat actuel = Etat Admin: retrait d'un utilisateur");
PremiereFoisDEL = true;
}
PremiereFoisRepos = false;
BlinkLED();
//retirer un utilisateur autorisé
if (millis () - chrono >= TimeOutEtat) {
TimeOut ();
chrono = millis ();
}
}
while (EtatSysteme == REPOS) {
if (nfc.tagPresent ()) {
NfcTag tag = nfc.read ();
Serial.println ("BADGE/CARTE scanné et récupéré");
Serial.println ("son UID est : ");
Serial.print (tag.getUidString ());
Serial.println ("");
Serial.println ("");
if (tag.getUidString () == CarteAdminADD) {
tone (Buzzer_PIN, 2000, 50);
chrono = millis ();
EtatSysteme = ETATADD;
}
else if (tag.getUidString () == CarteAdminDEL) {
tone (Buzzer_PIN, 2000, 50);
chrono = millis ();
EtatSysteme = ETATDEL;
}
else if (tag.getUidString () == acces1 || tag.getUidString () == acces2) {
chrono = millis ();
EtatSysteme = ETATOUVERT;
}
else{
PremiereFoisRepos = false;
Refus();
}
}
else {
Repos ();
}
}
}
void TimeOut() {
if (EtatSysteme != REPOS) {
Serial.println ("Time Out !");
EtatSysteme = REPOS;
}
}
void Repos() {
if (!PremiereFoisRepos){
Serial.println ("Etat actuel = REPOS");
Serial.println ("Attente : ");
Serial.println ("");
PremiereFoisRepos = true;
}
PremiereFoisOuvert = false;
PremiereFoisADD = false;
PremiereFoisDEL = false;
digitalWrite (Buzzer_PIN, LOW);
digitalWrite (LedR_PIN, LOW);
digitalWrite (LedV_PIN, LOW);
digitalWrite (Serrure_PIN, LOW);
}
void Ouverture() {
digitalWrite (Serrure_PIN, HIGH);
digitalWrite (LedV_PIN, HIGH);
tone (Buzzer_PIN, 1100, 100);
delay (100);
tone (Buzzer_PIN, 1300, 50);
delay (50);
}
void Refus() {
Serial.println ("Refus d'acces: Utilisateur non reconnu");
digitalWrite (LedR_PIN, HIGH);
tone (Buzzer_PIN, 900, 100);
delay (100);
tone (Buzzer_PIN, 700, 50);
delay (1000);
}
void BlinkLED() {
if (millis() - chronoLED >= 250) {
chronoLED = millis();
if (LedState == LOW) LedState = HIGH;
else LedState = LOW;
}
digitalWrite (LedR_PIN, LedState);
digitalWrite (LedV_PIN, LedState);
}
j'aimerais qu'au passage devant le capteur NFC d'une carte dite "AdminADD", le programme puisse rajouter soit dans un tableau soit dans une liste de variable le prochain badge scanné (en attendant 7 secondes le scan du prochain badge). Ainsi ce nouveau badge pourrait déverrouiller la serrure
et inversement même système pour "AdminDEL", pour le retrait d'accès.
j'espère que c'est possible facilement je m'y connais moyennement, je me suis aider d'un tuto de J-M-L sur les machines fini ou à état, un grand merci à lui d'ailleurs.
j'ai mis en gros là ou je voyais les modifications en commentaire.
Merci beaucoup d'avance !