# Système Alarme bug RFID

**URL:** <https://forum.arduino.cc/t/systeme-alarme-bug-rfid/1093487>\
**Category:** Français\
**Created:** [February 22, 2023, 4:51pm UTC](https://forum.arduino.cc/t/systeme-alarme-bug-rfid/1093487 "2023-02-22T16:51:50Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Geek56](https://avatars.discourse-cdn.com/v4/letter/g/5daacb/32.png) [@Geek56](https://forum.arduino.cc/u/Geek56)\
**Post date:** [February 22, 2023, 4:51pm UTC](https://forum.arduino.cc/t/systeme-alarme-bug-rfid/1093487/1 "2023-02-22T16:51:50Z")

</div>

Bonjour,  
je travaille sur un système d'alarme à vérification RFID puis code dont voici le programme :

```cpp
#include "Wire.h"
#include "LiquidCrystal_I2C.h"
#include <Keypad.h>
#include <Servo.h>
#include <MFRC522.h>

#define buzzer 8
#define trigPin 9
#define echoPin 10
#define tempsdarret 75
#define ouvert 0
#define ferme 90

#define BADGE_PORT_PLAISANCE " C8 45 EB 61"
#define BADGE_BLEU " A9 B2 BB B9" //badge bleu
#define BADGE_NFC "0 4 5B A5 BA 2D 59 81" //tag NFC
#define BADGE_ROUGE " F1 49 2F 3D"
#define JEAN " 2A F4 2E 3D"
#define CPE " A6 40 2F 3D"
#define ERIC_CAMENEN "0 3 42 2F 3D"
#define LEBODIC " 1F 74 2E 3D"
#define DIRECTEUR " D6 FE 2E 3D"
#define DE_CRUZ " 41 4A 2F 3D"
#define DANO " D5 46 2F 3D"
#define EWEN " EE 1A 2F 3D"

#define SS_PIN 53
#define RST_PIN 37

Servo monServomoteur;

long duration;
int distance, initialDistance, currentDistance, i;
int screenOffMsg =0;
String password="1876";
String Mdp="4253";
String tempPassword;
boolean activated = false; // État de l'alarme
boolean active = false;
boolean isActivated;
boolean activateAlarm = false;
boolean alarmActivated = false;
boolean enteredPassword; // État du mot de passe saisi pour arrêter l'alarme
boolean passChangeMode = false;
boolean passChanged = false;
LiquidCrystal_I2C lcd(0x27,16,2); // définit le type d'écran lcd 16 x 2

const byte ROWS = 4; //quatre rangs
const byte COLS = 4; //quatre colonnes
char keypressed;
//définir les symboles sur les boutons du clavier.
char keyMap[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {14, 15, 16, 17}; //Brochage des rangées du clavier
byte colPins[COLS] = {18, 19, A0, A1}; //Brochage des colonnes du clavier

Keypad myKeypad = Keypad( makeKeymap(keyMap), rowPins, colPins, ROWS, COLS); 

MFRC522 myReader (SS_PIN, RST_PIN);

void setup() { 
  lcd.init(); // initialisation de l'afficheur
  lcd.backlight();
  Serial.begin(9600);
  pinMode(buzzer, OUTPUT); // Définir le buzzer comme une sortie
  pinMode(trigPin, OUTPUT); // Définit la trigPin comme une sortie
  pinMode(echoPin, INPUT); // Définit l'echoPin comme une entrée
  monServomoteur.attach(36);
  // Up to version 1.4.5 this call
  myReader.PCD_Init ();
  //debut();
}

void loop() {
  if (activateAlarm) {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("L'alarme sera");
    lcd.setCursor(0,1);
    lcd.print("activee dans");
   
    int countdown = 9; // Compte à rebours de 9 secondes avant l'activation de l'alarme
    while (countdown != 0) {
      lcd.setCursor(13,1);
      lcd.print(countdown);
      countdown--;
      tone(buzzer, 700, 100);
      delay(1000);
    }
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print(" ***ALARME***");
    lcd.setCursor(0,1);
    lcd.print("Alarme activee");
    tone(buzzer, 2000, 600);
    initialDistance = getDistance();
    activateAlarm = false;
    alarmActivated = true;
  }

  if (alarmActivated == true){
      currentDistance = getDistance() + 10;
      if ( currentDistance < initialDistance) {
        tone(buzzer, 1000); // Envoyer un signal sonore de 1KHz 
        lcd.clear();
        enterPassword();
      }
    }

  if (!alarmActivated) {
    if (screenOffMsg == 0 ){
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("A - Activer");
      lcd.setCursor(0,1);
      lcd.print("B - Carte SD");
      screenOffMsg = 1;
    }
    keypressed = myKeypad.getKey();
     if (keypressed =='A'){ //Si vous appuyez sur A, vous activez l'alarme
      tone(buzzer, 1000, 200);
      activateAlarm = true;            
    }

    else if (keypressed =='B') {
      lcd.clear();
      int i=5;
      tone(buzzer, 2000, 100);
      tempPassword = "";
      lcd.setCursor(0,0);
      lcd.print("Entrez le code");
      lcd.setCursor(0,1);
      lcd.print("Code>");
      passChangeMode = true;
      passChanged = true;   
      while(passChanged) {      
      keypressed = myKeypad.getKey();
      if (keypressed != NO_KEY){
        if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
            keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
            keypressed == '8' || keypressed == '9' ) {
         tempPassword += keypressed;
         lcd.setCursor(i,1);
         lcd.print("*");
         i++;
         tone(buzzer, 2000, 100);
        }
      }
      if (i > 9 || keypressed == '#') {
        tempPassword = "";
        i=5;
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Entrez le code");
        lcd.setCursor(0,1);
        lcd.print("Code>"); 
      }
      if ( keypressed == '*') {
        i=1;
        tone(buzzer, 2000, 100);
        if (Mdp == tempPassword) {
          monServomoteur.write(ouvert);
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("Blocage de la");
          lcd.setCursor(0,1);
          lcd.print("carte SD dans ");
          int countdown = 20; // Compte à rebours de 20 secondes avant la fermeture de la boite à carte SD
          while (countdown != 0) {
            lcd.setCursor(14,1);
            lcd.print(countdown);
            countdown--;
            tone(buzzer, 700, 100);
            
            if (countdown <= 8) {
            lcd.setCursor(15,1);
            lcd.print(" ");
            }
            delay(1000);
          }
          tone(buzzer, 2000, 1000);
          monServomoteur.write(ferme);
          Mdp = tempPassword;
          passChangeMode = false;
          passChanged = false;
          screenOffMsg = 0;
        }
        
      }
    }
   }
 }
}

void enterPassword() {
  int k=5;
  tempPassword = "";
  activated = true;
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(" ***ALARME***");
  lcd.setCursor(0,1);
  lcd.print("PRESENTEZ BADGE");
      while(activated) {
if (! myReader.PICC_IsNewCardPresent ())
    {

      // not found, stops searching
      return;
    }
    // Checks whether the key tag can be read
    if (! myReader.PICC_ReadCardSerial ())
    {
      // failed to read
      return;
    }
    // Show UID on serial monitor
    String key = "";
    for (byte i = 0; i < myReader.uid.size; i ++)
    {
      key.concat (String (myReader.uid.uidByte [i] < 0x10 ? "0 " : " "));
      key.concat (String (myReader.uid.uidByte [i], HEX));
    }
    // Uppercase code of found key for better comparison
    key.toUpperCase ();

    // Print to monitor so you know which key is being read
    Serial.println ();
    Serial.print ("Badge d'accès :");
    Serial.print (key);

    // Grants access if key “..” is the same
    // Each key tag has its own code, read your code via monitor
    Serial.println ();
    Serial.print ("ACCES: ");
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("ACCES: ");

    if (key == BADGE_PORT_PLAISANCE)
    {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Bon");
    }

    else if (key == BADGE_BLEU)
    {
      active = true;
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print(" ***ALARME***");
      lcd.setCursor(0,1);
      lcd.print("Code>");
      while(active) {
      keypressed = myKeypad.getKey();
      if (keypressed != NO_KEY){
        if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
            keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
            keypressed == '8' || keypressed == '9' ) {
          tempPassword += keypressed;
          lcd.setCursor(k,1);
          lcd.print("*");
          k++;
        }
      }
      if (k > 9 || keypressed == '#') {
        tempPassword = "";
        k=5;
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(" ***ALARME***");
        lcd.setCursor(0,1);
        lcd.print("Code>");
      }
      if ( keypressed == '*') {
        if ( tempPassword == password ) {
          activated = false;
          alarmActivated = false;
          active = false;
          noTone(buzzer);
          screenOffMsg = 0; 
        }
        else if (tempPassword != password) {
          lcd.setCursor(0,1);
          lcd.print("FAUX! Reesayez");
          delay(2000);
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print(" ***ALARME***");
          lcd.setCursor(0,1);
          lcd.print("Code>");
        }
      }
      } 
    }

    else {

      // Determines the frequency of the buzzer
      tone (buzzer, 500);
      // wait 1 second
      delay (1000);
      // stops sound
      noTone(buzzer);
    }
    Serial.println ();
      
    }
}
void debut() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("A");
  delay(400);
  lcd.clear();
  delay(400);
  lcd.setCursor(0, 0);
  lcd.print("A");
  delay(400);
  lcd.clear();
  delay(500);
  lcd.setCursor(0, 0);
  lcd.print("A");
  delay(tempsdarret);
  lcd.setCursor(1, 0);
  lcd.print("R");
  delay(tempsdarret);
  lcd.setCursor(2, 0);
  lcd.print("D");
  delay(tempsdarret);
  lcd.setCursor(3, 0);
  lcd.print("U");
  delay(tempsdarret);
  lcd.setCursor(4, 0);
  lcd.print("I");
  delay(tempsdarret);
  lcd.setCursor(5, 0);
  lcd.print("N");
  delay(tempsdarret);
  lcd.setCursor(6, 0);
  lcd.print("O");
  delay(tempsdarret);
  lcd.setCursor(8, 0);
  lcd.print("S");
  delay(tempsdarret);
  lcd.setCursor(9, 0);
  lcd.print("E");
  delay(tempsdarret);
  lcd.setCursor(10, 0);
  lcd.print("C");
  delay(tempsdarret);
  lcd.setCursor(11, 0);
  lcd.print("U");
  delay(tempsdarret);
  lcd.setCursor(12, 0);
  lcd.print("R");
  delay(tempsdarret);
  lcd.setCursor(13, 0);
  lcd.print("I");
  delay(tempsdarret);
  lcd.setCursor(14, 0);
  lcd.print("T");
  delay(tempsdarret);
  lcd.setCursor(15, 0);
  lcd.print("Y");
  delay(tempsdarret);
  lcd.setCursor(0, 1);
  lcd.print("C");
  delay(tempsdarret);
  lcd.setCursor(1, 1);
  lcd.print("O");
  delay(tempsdarret);
  lcd.setCursor(2, 1);
  lcd.print("R");
  delay(tempsdarret);
  lcd.setCursor(3, 1);
  lcd.print("P");
  delay(tempsdarret);
  lcd.setCursor(4, 1);
  lcd.print("O");
  delay(tempsdarret);
  lcd.setCursor(5, 1);
  lcd.print("R");
  delay(tempsdarret);
  lcd.setCursor(6, 1);
  lcd.print("A");
  delay(tempsdarret);
  lcd.setCursor(7, 1);
  lcd.print("T");
  delay(tempsdarret);
  lcd.setCursor(8, 1);
  lcd.print("I");
  delay(tempsdarret);
  lcd.setCursor(9, 1);
  lcd.print("O");
  delay(tempsdarret);
  lcd.setCursor(10, 1);
  lcd.print("N");
  delay(tempsdarret);
  lcd.setCursor(12, 1);
  lcd.print("V");
  delay(tempsdarret);
  lcd.setCursor(13, 1);
  lcd.print("1");
  delay(tempsdarret);
  lcd.setCursor(14, 1);
  lcd.print(".");
  delay(tempsdarret);
  lcd.setCursor(15, 1);
  lcd.print("0");
  delay(1000);
  tone(buzzer, 1000);
  delay(250);
  noTone(buzzer);
  delay(250);
  tone(buzzer, 1000);
  delay(250);
  noTone(buzzer);
  delay(250);
  tone(buzzer, 1000);
  delay(250);
  noTone(buzzer);
  lcd.clear();
  lcd.setCursor(2, 0);
  lcd.print("L");
  delay(400);
  lcd.clear();
  delay(400);
  lcd.setCursor(2, 0);
  lcd.print("L");
  delay(400);
  lcd.clear();
  delay(500);
  lcd.setCursor(2, 0);
  lcd.print("L");
  delay(tempsdarret);
  lcd.setCursor(3, 0);
  lcd.print("A");
  delay(tempsdarret);
  lcd.setCursor(4, 0);
  lcd.print("N");
  delay(tempsdarret);
  lcd.setCursor(5, 0);
  lcd.print("C");
  delay(tempsdarret);
  lcd.setCursor(6, 0);
  lcd.print("E");
  delay(tempsdarret);
  lcd.setCursor(7, 0);
  lcd.print("M");
  delay(tempsdarret);
  lcd.setCursor(8, 0);
  lcd.print("E");
  delay(tempsdarret);
  lcd.setCursor(9, 0);
  lcd.print("N");
  delay(tempsdarret);
  lcd.setCursor(10, 0);
  lcd.print("T");
  delay(tempsdarret);
  lcd.setCursor(12, 0);
  lcd.print("D");
  delay(tempsdarret);
  lcd.setCursor(13, 0);
  lcd.print("E");
  lcd.setCursor(0, 1);
  lcd.print("A");
  delay(tempsdarret);
  lcd.setCursor(1, 1);
  lcd.print("L");
  delay(tempsdarret);
  lcd.setCursor(2, 1);
  lcd.print("A");
  delay(tempsdarret);
  lcd.setCursor(3, 1);
  lcd.print("R");
  delay(tempsdarret);
  lcd.setCursor(4, 1);
  lcd.print("M");
  delay(tempsdarret);
  lcd.setCursor(5, 1);
  lcd.print("Y");
  delay(tempsdarret);
  lcd.setCursor(7, 1);
  lcd.print("S");
  delay(tempsdarret);
  lcd.setCursor(8, 1);
  lcd.print("E");
  delay(tempsdarret);
  lcd.setCursor(9, 1);
  lcd.print("C");
  delay(tempsdarret);
  lcd.setCursor(10, 1);
  lcd.print("U");
  delay(tempsdarret);
  lcd.setCursor(11, 1);
  lcd.print("R");
  delay(tempsdarret);
  lcd.setCursor(12, 1);
  lcd.print("I");
  delay(tempsdarret);
  lcd.setCursor(13, 1);
  lcd.print("T");
  delay(tempsdarret);
  lcd.setCursor(14, 1);
  lcd.print("Y");
  delay(3000);
}
// Fonction personnalisée pour le capteur à ultrasons
long getDistance(){
  //int i=10;
  
  //while( i<=10 ) {
  // Efface le trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  // Met la trigPin sur l'état HIGH pendant 10 micro secondes
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Lit l'echoPin, renvoie le temps de parcours de l'onde sonore en microsecondes.
  duration = pulseIn(echoPin, HIGH);

  // Calcul de la distance
  distance = duration*0.034/2;
  //sumDistance += distance;
  //}
  //int averageDistance= sumDistance/10;
  return distance;

}

/*
if (! myReader.PICC_IsNewCardPresent ())
    {

      // not found, stops searching
      return;
    }
    // Checks whether the key tag can be read
    if (! myReader.PICC_ReadCardSerial ())
    {
      // failed to read
      return;
    }
    // Show UID on serial monitor
    String key = "";
    for (byte i = 0; i < myReader.uid.size; i ++)
    {
      key.concat (String (myReader.uid.uidByte [i] < 0x10 ? "0 " : " "));
      key.concat (String (myReader.uid.uidByte [i], HEX));
    }
    // Uppercase code of found key for better comparison
    key.toUpperCase ();

    // Print to monitor so you know which key is being read
    Serial.println ();
    Serial.print ("Badge d'accès :");
    Serial.print (key);

    // Grants access if key “..” is the same
    // Each key tag has its own code, read your code via monitor
    Serial.println ();
    Serial.print ("ACCES: ");
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("ACCES: ");

    if (key == BADGE_PORT_PLAISANCE)
    {

      // LED rouge off, LED verte on
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, HIGH);
      // Determines the frequency of the buzzer
      tone (buzzer, 1000);
      delay (1000);
      // stop, sound
      noTone(buzzer);
      Ouvert();

      // Print on monitor
      Serial.print ("AUTORISE : MALO LENORMAND");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MALO LENORMAND");
      delay (3000);
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, LOW);
      boutonFermeturePorte = true;
    }

    else if (key == BADGE_BLEU)
    {

      // LED rouge off, LED verte on
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, HIGH);
      // Déterminer la fréquence du buzzer
      tone (buzzer, 1000);
      delay (1000);
      // Stoper le son
      noTone(buzzer);
      Ouvert();

      // Écrire sur le moniteur
      Serial.print ("AUTORISE : MALO LENORMAND");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MALO LENORMAND");
      delay (3000);
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, LOW);
      boutonFermeturePorte = true;
    }

    else {

      // Red LED on, green off
      digitalWrite (ledpin1, HIGH);
      digitalWrite (ledpin2, LOW);

      // Determines the frequency of the buzzer
      tone (buzzer, 500);
      // wait 1 second
      delay (1000);
      // stops sound
      noTone(buzzer);

      // Print on monitor
      Serial.print ("REFUSE");
      lcd.setCursor(7, 0);
      lcd.print("REFUSE");
      lcd.setCursor(0, 1);
      lcd.print(key);
      delay (3000);
    }
    Serial.println ();
    */
```

Petit problème lors de la vérification RFID qui débute ligne 199 : ça ne marche pas.  
Est-ce que quelqu'un pourrait m'aider svp ?  
Merci d'avance.  
Cordialement,  
Geek56.

---

<div class="post-metadata">

**Author:** ![flix09](https://avatars.discourse-cdn.com/v4/letter/f/7993a0/32.png) [@flix09](https://forum.arduino.cc/u/flix09)\
**Post date:** [February 23, 2023, 11:09am UTC](https://forum.arduino.cc/t/systeme-alarme-bug-rfid/1093487/2 "2023-02-23T11:09:56Z")

</div>

> [@Geek56](#):
>
> Petit problème lors de la vérification RFID qui débute ligne 199 : ça ne marche pas.

...Dans le code?En vrai?  
Affiche aussi ce que montre la compilations  
Cordialement  
flix09 👾

---

<div class="post-metadata">

**Author:** ![Geek56](https://avatars.discourse-cdn.com/v4/letter/g/5daacb/32.png) [@Geek56](https://forum.arduino.cc/u/Geek56)\
**Post date:** [February 23, 2023, 12:17pm UTC](https://forum.arduino.cc/t/systeme-alarme-bug-rfid/1093487/3 "2023-02-23T12:17:45Z")

</div>

Merci de ta réponse.  
J'ai l'impression que le problème vient du code car j'ai testé chacun des composants séparément.  
Le moniteur série n'affiche rien au moment où le RFID est censé entrer en jeu.  
Voilà ce qu'affiche la compilation :  
_**Le croquis utilise 16330 octets (6%) de l'espace de stockage de programmes. Le maximum est de 253952 octets.**_  
_**Les variables globales utilisent 1065 octets (13%) de mémoire dynamique, ce qui laisse 7127 octets pour les variables locales. Le maximum est de 8192 octets.**_  
Merci de ton aide.  
Cordialement,  
Geek56.

---

<div class="post-metadata">

**Author:** ![flix09](https://avatars.discourse-cdn.com/v4/letter/f/7993a0/32.png) [@flix09](https://forum.arduino.cc/u/flix09)\
**Post date:** [February 23, 2023, 3:24pm UTC](https://forum.arduino.cc/t/systeme-alarme-bug-rfid/1093487/4 "2023-02-23T15:24:24Z")

</div>

Donc:Tu as compilé ton code,tu l'as televerse tu as essaye et ca ne marchait pas c'est ça?

---

<div class="post-metadata">

**Author:** ![Geek56](https://avatars.discourse-cdn.com/v4/letter/g/5daacb/32.png) [@Geek56](https://forum.arduino.cc/u/Geek56)\
**Post date:** [February 23, 2023, 3:25pm UTC](https://forum.arduino.cc/t/systeme-alarme-bug-rfid/1093487/5 "2023-02-23T15:25:18Z")

</div>

Oui, c'est ça. Le RFID ne marchait pas mais les trucs d'avant la partie RFID marchent.

---

<div class="post-metadata">

**Author:** ![flix09](https://avatars.discourse-cdn.com/v4/letter/f/7993a0/32.png) [@flix09](https://forum.arduino.cc/u/flix09)\
**Post date:** [February 23, 2023, 3:27pm UTC](https://forum.arduino.cc/t/systeme-alarme-bug-rfid/1093487/6 "2023-02-23T15:27:28Z")

</div>

Apres,tu as mis en commentaires tout ce qui est d'afficher sur le moniteur...

> [@Geek56](#):
>
> `Serial.print ("AUTORISE : MALO LENORMAND");`

Tiens,par exemple,cette ligne est commenté...Pourquoi?

Ceci dit,le code est joli! Bravo

---

<div class="post-metadata">

**Author:** ![Geek56](https://avatars.discourse-cdn.com/v4/letter/g/5daacb/32.png) [@Geek56](https://forum.arduino.cc/u/Geek56)\
**Post date:** [February 23, 2023, 4:50pm UTC](https://forum.arduino.cc/t/systeme-alarme-bug-rfid/1093487/7 "2023-02-23T16:50:02Z")

</div>

Je ne me suis pas encore penché sur les détails mais pour le RFID je me suis aidé d'un autre programme que j'ai écrit qui est pour un système d'ouverture de porte RFID sans digicode :

```arduino
#define BADGE_PORT_PLAISANCE " C8 45 EB 61"
#define BADGE_BLEU " A9 B2 BB B9" //badge bleu
#define BADGE_NFC "0 4 5B A5 BA 2D 59 81" //tag NFC
#define BADGE_ROUGE " F1 49 2F 3D"
#define JEAN " 2A F4 2E 3D"
#define CPE " A6 40 2F 3D"
#define ERIC_CAMENEN "0 3 42 2F 3D"
#define LEBODIC " 1F 74 2E 3D"
#define DIRECTEUR " D6 FE 2E 3D"
#define DE_CRUZ " 41 4A 2F 3D"
#define DANO " D5 46 2F 3D"
#define EWEN " EE 1A 2F 3D"

/*
  Branchements :
  led verte en 7
  led rouge en 6
  servo en 4
  bouton en 3
  buzzer en 8
  module RFID /
  SDA en 53
  SCK en 52
  MOSI en 51
  MISO en 50
  RQ en rien
  GND en GND
  RST en 5
  3.3V en 3.3V
*/
// CONNECTIONS:
// DS1302 CLK/SCLK --> 32
// DS1302 DAT/IO --> 33
// DS1302 RST/CE --> 34
// DS1302 VCC --> 3.3v - 5v
// DS1302 GND --> GND

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#include <IRremote.h>
#include <LiquidCrystal.h>
#include <ThreeWire.h>
#include <RtcDS1302.h>

// Indicates the pins
#define SS_PIN 53
#define RST_PIN 37
// pour définir la valeur des ouvertures
#define ouvert 0
#define ferme 80
#define tempsdarret 75

Servo monServomoteur;

// Indicates to which pin the LED is connected
const int ledpin1 = 45; //rouge
const int ledpin2 = 39; //verte
const int ledpin3 = 26; //rouge intérieur
const int ledpin4 = 29; //verte intérieur
const int buzzer = 8;
int bouton = 3;
int etatbouton = 0;
int bouton2 = 30;
int etatbouton2 = 0;
int bouton3 = 9;
int etatbouton3 = 0;
int RECV_PIN = 10;
long CODE_IR_9 = 0xFF906F;
long CODE_IR_0 = 0xFF9867;
long CODE_IR_1 = 0xFFA25D;
long CODE_IR1_9 = 0xBFEC0EB0;
word BTDATA;//Donne le nom BTDATA à la donnée reçue sur le Bluetooth

IRrecv irrecv(RECV_PIN);
decode_results results;

//icone de cadenas fermé
byte Lock[8] = {
  0b01110,
  0b10001,
  0b10001,
  0b11111,
  0b11011,
  0b11011,
  0b11111,
};

//icone de cadenas ouvert
byte Unlock[8] = {
  0b01110,
  0b10000,
  0b10000,
  0b11111,
  0b11011,
  0b11011,
  0b11111,
};

// Creates own veriable for card reader
MFRC522 myReader (SS_PIN, RST_PIN);

LiquidCrystal lcd(10, 2, 11, 5, 6, 7); // Crée un objet LC. Paramètres : (rs, enable, d4, d5, d6, d7)

boolean systemeEnMaintenance = false;
boolean boutonFermeturePorte = false;
boolean iconeCadenas = false;

ThreeWire myWire(33,32,34); // IO, SCLK, CE
RtcDS1302<ThreeWire> Rtc(myWire);

void setup ()
{
  // définit la broche 36 pour le servo moteur "monServomoteur"
  monServomoteur.attach(36);
  Ferme();
  lcd.setCursor(0, 0);

  irrecv.enableIRIn();
  lcd.begin(16, 2);

  // définit la pin du buzzer en sortie
  pinMode (buzzer, OUTPUT);

  // Démarrage du moniteur série
  Serial.begin (9600);
  Serial1.begin(9600);//HC06 en pin 18-19 RX sur TX1 et TX sur RX1

  // Démarrage de la librairie SPI
  SPI.begin ();

  // Up to version 1.4.5 this call
  myReader.PCD_Init ();

  // Écrire dans le moniteur série : “Presentez votre badge d'accès”
  Serial.println ("Presentez votre badge d'accès");
  Serial.println ();

  // définit les broches ledpin1, ledpin2, ledpin3, ledpin4, 22, 23 et 24 comme étant des sorties et les broches bouton, bouton2, bouton3 et 25 comme étant des entrées
  pinMode (ledpin1, OUTPUT);
  pinMode (ledpin2, OUTPUT);
  pinMode (ledpin3, OUTPUT);
  pinMode (ledpin4, OUTPUT);
  pinMode(bouton, INPUT);
  pinMode(bouton2, INPUT);
  pinMode(bouton3, INPUT);
  pinMode(22, OUTPUT);
  pinMode(23, OUTPUT);
  pinMode(24, OUTPUT);
  pinMode(25, INPUT);
  // creation d'un nouveau dessin personnalisé
  lcd.createChar(1, Lock);
  // creation d'un nouveau dessin personnalisé
  lcd.createChar(2, Unlock);
  if (iconeCadenas) {
      lcd.setCursor(0, 1);
      lcd.write(2);
      lcd.setCursor(15, 1);
      lcd.write(2);
      digitalWrite(ledpin3, LOW);
      digitalWrite(ledpin4, HIGH);
    }
    else {
      lcd.setCursor(0, 1);
      lcd.write(1);
      lcd.setCursor(15, 1);
      lcd.write(1);
      digitalWrite(ledpin3, HIGH);
      digitalWrite(ledpin4, LOW);
      }
  Rtc.Begin();

    RtcDateTime compiled = RtcDateTime( __DATE__ , __TIME__ );
    printDateTime(compiled);
    Serial.println();

    if (!Rtc.IsDateTimeValid()) 
    {
        // Common Causes:
        // 1) first time you ran and the device wasn't running yet
        // 2) the battery on the device is low or even missing

        Serial.println("RTC lost confidence in the DateTime!");
        Rtc.SetDateTime(compiled);
    }

    if (Rtc.GetIsWriteProtected())
    {
        Serial.println("RTC was write protected, enabling writing now");
        Rtc.SetIsWriteProtected(false);
    }

    if (!Rtc.GetIsRunning())
    {
        Serial.println("RTC was not actively running, starting now");
        Rtc.SetIsRunning(true);
    }

    RtcDateTime now = Rtc.GetDateTime();
    if (now < compiled) 
    {
        Serial.println("RTC is older than compile time! (Updating DateTime)");
        Rtc.SetDateTime(compiled);
    }
    else if (now > compiled) 
    {
        Serial.println("RTC is newer than compile time. (this is expected)");
    }
    else if (now == compiled) 
    {
        Serial.println("RTC is the same as compile time! (not expected but all is fine)");
    }
  debut();
}

void loop () {
  RtcDateTime now = Rtc.GetDateTime();
  printDateTime(now);
    Serial.println();
     
    if (!now.IsValid())
    {
        // Common Causes:
        // 1) the battery on the device is low or even missing and the power line was disconnected
        Serial.println("RTC lost confidence in the DateTime!");
    }
  delay(5000);
  digitalWrite(22, HIGH);
  digitalWrite(23, HIGH);
  digitalWrite(24, HIGH);
  if (!systemeEnMaintenance) { // Si système pas en maintenance
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.write("PRESENTEZ BADGE");
    lcd.setCursor(4, 1);
    lcd.print("D'ACCES");
    if (iconeCadenas) {
      lcd.setCursor(0, 1);
      lcd.write(2);
      lcd.setCursor(15, 1);
      lcd.write(2);
      digitalWrite(ledpin3, LOW);
      digitalWrite(ledpin4, HIGH);
    }
    else {
      lcd.setCursor(0, 1);
      lcd.write(1);
      lcd.setCursor(15, 1);
      lcd.write(1);
      digitalWrite(ledpin3, HIGH);
      digitalWrite(ledpin4, LOW);
    }

  }
  if (Serial.available()) {
    char c = Serial.read();
    Serial.println("Valeur entrée:");
    Serial.println(c);

    if (c == '8') { // si 8 est tapé dans le moniteur série, la porte s'ouvre
      Ouvert();
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, HIGH);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("ACCES:");
      Serial.print ("AUTORISE : MALO LENORMAND");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MALO LENORMAND");
      tone (buzzer, 1000);
      delay (1000);
      // stop, sound
      noTone(buzzer);
      delay(800);
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, LOW);
      boutonFermeturePorte = true;
    }
    if (c == '3') { // si 3 est tapé dans le moniteur série, la porte s'ouvre
      digitalWrite (ledpin1, HIGH);
      digitalWrite (ledpin2, LOW);
      Ferme();
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("ACCES:");
      Serial.print ("AUTORISE : MALO LENORMAND");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MALO LENORMAND");
      tone (buzzer, 500);
      delay (1000);
      // stop, sound
      noTone(buzzer);
      delay(3000);
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, LOW);
      boutonFermeturePorte = false;
    }
    if (c == 'r') { // si r est tapé dans le moniteur série, le système se réinitialise
      Serial.println ("REINITIALISATION");
      tone (buzzer, 1000);
      lcd.clear();
      delay(500);
      noTone(buzzer);
    }
    if (c == 'm') { // si m est tapé dans le moniteur série, le système se met en maintenance

      lcd.clear();
      lcd.setCursor(3, 0);
      lcd.print("SERVICE EN");
      lcd.setCursor(3, 1);
      lcd.print("MAINTENANCE");
      Serial.println("MAINTENANCE");
      tone (buzzer, 500);
      delay(1000);
      noTone(buzzer);
      //delay(200000000);
      systemeEnMaintenance = true;
    }

    if (c == 's') { // si s est tapé dans le moniteur série, le système sort de maintenance

      lcd.clear();
      lcd.setCursor(3, 0);
      lcd.print("FIN DE LA");
      lcd.setCursor(3, 1);
      lcd.print("MAINTENANCE");
      Serial.println("MAINTENANCE OFF");
      tone (buzzer, 500);
      delay(1000);
      noTone(buzzer);
      systemeEnMaintenance = false;
    }
  }

  if (Serial1.available()) {
    BTDATA = Serial1.read();
  }
  if (Serial.available())
    Serial1.write(Serial.read());
  // ********************************************
  if (!systemeEnMaintenance) { // Si système pas en maintenance
    if (BTDATA == 'O') { //Si on reçoit un O, on ouvre la porte
      Ouvert();
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, HIGH);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("ACCES:");
      Serial.print ("AUTORISE : MALO LENORMAND");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MALO LENORMAND");
      tone (buzzer, 1000);
      delay (1000);
      // stop, sound
      noTone(buzzer);
      delay(800);
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, LOW);
      BTDATA = 0;
      boutonFermeturePorte = true;
    }
    if (BTDATA == 'F') { //Si on reçoit un F, on ferme la porte
      digitalWrite (ledpin1, HIGH);
      digitalWrite (ledpin2, LOW);
      Ferme();
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("ACCES:");
      Serial.print ("AUTORISE : MALO LENORMAND");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MALO LENORMAND");
      tone (buzzer, 500);
      delay (1000);
      // stop, sound
      noTone(buzzer);
      delay(3000);
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, LOW);
      BTDATA = 0;
      boutonFermeturePorte = false;
    }
if (BTDATA == 'o') { //Si on reçoit un O, on ouvre la porte
      //LE BODIC
      Ouvert();
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, HIGH);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("ACCES:");
      Serial.print ("AUTORISE : MR LE BODIC");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MR LE BODIC");
      tone (buzzer, 1000);
      delay (1000);
      // stop, sound
      noTone(buzzer);
      delay(800);
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, LOW);
      BTDATA = 0;
      boutonFermeturePorte = true;
    }
    if (BTDATA == 'f') { //Si on reçoit un F, on ferme la porte
    //LE BODIC
      digitalWrite (ledpin1, HIGH);
      digitalWrite (ledpin2, LOW);
      Ferme();
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("ACCES:");
      Serial.print ("AUTORISE : MR LE BODIC");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MR LE BODIC");
      tone (buzzer, 500);
      delay (1000);
      // stop, sound
      noTone(buzzer);
      delay(3000);
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, LOW);
      BTDATA = 0;
      boutonFermeturePorte = false;
    }
    if (BTDATA == 'd') { //Si on reçoit un O, on ouvre la porte
      //Réparateur
      Ouvert();
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, HIGH);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("ACCES:");
      Serial.print ("AUTORISE : MR DE CRUZ");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MR DE CRUZ");
      tone (buzzer, 1000);
      delay (1000);
      // stop, sound
      noTone(buzzer);
      delay(800);
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, LOW);
      BTDATA = 0;
      boutonFermeturePorte = true;
    }
    if (BTDATA == 'e') { //Si on reçoit un F, on ferme la porte
    //Réparateur
      digitalWrite (ledpin1, HIGH);
      digitalWrite (ledpin2, LOW);
      Ferme();
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("ACCES:");
      Serial.print ("AUTORISE : MR DE CRUZ");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MR DE CRUZ");
      tone (buzzer, 500);
      delay (1000);
      // stop, sound
      noTone(buzzer);
      delay(3000);
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, LOW);
      BTDATA = 0;
      boutonFermeturePorte = false;
    }
    if (BTDATA == 'p') { //Si on reçoit un O, on ouvre la porte
      //Camenen
      Ouvert();
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, HIGH);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("ACCES:");
      Serial.print ("AUTORISE : MR CAMENEN");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MR CAMENEN");
      tone (buzzer, 1000);
      delay (1000);
      // stop, sound
      noTone(buzzer);
      delay(800);
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, LOW);
      BTDATA = 0;
      boutonFermeturePorte = true;
    }
    if (BTDATA == 'q') { //Si on reçoit un F, on ferme la porte
    //Camenen
      digitalWrite (ledpin1, HIGH);
      digitalWrite (ledpin2, LOW);
      Ferme();
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("ACCES:");
      Serial.print ("AUTORISE : MR CAMENEN");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MR CAMENEN");
      tone (buzzer, 500);
      delay (1000);
      // stop, sound
      noTone(buzzer);
      delay(3000);
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, LOW);
      BTDATA = 0;
      boutonFermeturePorte = false;
    }
    if (BTDATA == 'b') { //Si on reçoit un O, on ouvre la porte
      //Ewen
      Ouvert();
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, HIGH);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("ACCES:");
      Serial.print ("AUTORISE : EWEN LE PRIOL");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("EWEN LE PRIOL");
      tone (buzzer, 1000);
      delay (1000);
      // stop, sound
      noTone(buzzer);
      delay(800);
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, LOW);
      BTDATA = 0;
      boutonFermeturePorte = true;
    }
    if (BTDATA == 'c') { //Si on reçoit un F, on ferme la porte
    //Ewen
      digitalWrite (ledpin1, HIGH);
      digitalWrite (ledpin2, LOW);
      Ferme();
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("ACCES:");
      Serial.print ("AUTORISE : EWEN LE PRIOL");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MR CAMENEN");
      tone (buzzer, 500);
      delay (1000);
      // stop, sound
      noTone(buzzer);
      delay(3000);
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, LOW);
      BTDATA = 0;
      boutonFermeturePorte = false;
    }
    if (BTDATA == 'h') { //Si on reçoit un O, on ouvre la porte
      //CPE
      Ouvert();
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, HIGH);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("ACCES:");
      Serial.print ("AUTORISE : MR LE BAGOUSSE");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MR LE BAGOUSSE");
      tone (buzzer, 1000);
      delay (1000);
      // stop, sound
      noTone(buzzer);
      delay(800);
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, LOW);
      BTDATA = 0;
      boutonFermeturePorte = true;
    }
    if (BTDATA == 'i') { //Si on reçoit un F, on ferme la porte
    //CPE
      digitalWrite (ledpin1, HIGH);
      digitalWrite (ledpin2, LOW);
      Ferme();
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("ACCES:");
      Serial.print ("AUTORISE : MR LE BAGOUSSE");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MR LE BAGOUSSE");
      tone (buzzer, 500);
      delay (1000);
      // stop, sound
      noTone(buzzer);
      delay(3000);
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, LOW);
      BTDATA = 0;
      boutonFermeturePorte = false;
    }
    if (BTDATA == 'j') { //Si on reçoit un O, on ouvre la porte
      //DIRECTEUR
      Ouvert();
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, HIGH);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("ACCES:");
      Serial.print ("AUTORISE : MR LE PICARD");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MR LE PICARD");
      tone (buzzer, 1000);
      delay (1000);
      // stop, sound
      noTone(buzzer);
      delay(800);
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, LOW);
      BTDATA = 0;
      boutonFermeturePorte = true;
    }
    if (BTDATA == 'k') { //Si on reçoit un F, on ferme la porte
    //DIRECTEUR
      digitalWrite (ledpin1, HIGH);
      digitalWrite (ledpin2, LOW);
      Ferme();
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("ACCES:");
      Serial.print ("AUTORISE : MR LE PICARD");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MR LE PICARD");
      tone (buzzer, 500);
      delay (1000);
      // stop, sound
      noTone(buzzer);
      delay(3000);
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, LOW);
      BTDATA = 0;
      boutonFermeturePorte = false;
    }
if (BTDATA == 'l') { //Si on reçoit un O, on ouvre la porte
      //DANO
      Ouvert();
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, HIGH);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("ACCES:");
      Serial.print ("AUTORISE : MR DANO");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MR DANO");
      tone (buzzer, 1000);
      delay (1000);
      // stop, sound
      noTone(buzzer);
      delay(800);
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, LOW);
      BTDATA = 0;
      boutonFermeturePorte = true;
    }
    if (BTDATA == 'n') { //Si on reçoit un F, on ferme la porte
    //DANO
      digitalWrite (ledpin1, HIGH);
      digitalWrite (ledpin2, LOW);
      Ferme();
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("ACCES:");
      Serial.print ("AUTORISE : MR DANO");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MR DANO");
      tone (buzzer, 500);
      delay (1000);
      // stop, sound
      noTone(buzzer);
      delay(3000);
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, LOW);
      BTDATA = 0;
      boutonFermeturePorte = false;
    }    
  }
  if (BTDATA == 'M') { //Si on reçoit un M, on active la maintenance
    Ouvert();
    lcd.clear();
    lcd.setCursor(3, 0);
    lcd.print("SERVICE EN");
    lcd.setCursor(3, 1);
    lcd.print("MAINTENANCE");
    Serial.println("MAINTENANCE");    
    tone (buzzer, 500);
    delay(1000);
    noTone(buzzer);
    //delay(200000000);
    systemeEnMaintenance = true;
    BTDATA = 0;
  }
  if (BTDATA == 'S') { //Si on reçoit un S, on arrête la maintenance
    lcd.clear();
    lcd.setCursor(3, 0);
    lcd.print("FIN DE LA");
    lcd.setCursor(3, 1);
    lcd.print("MAINTENANCE");
    Serial.println("MAINTENANCE OFF");
    tone (buzzer, 500);
    delay(1000);
    noTone(buzzer);
    systemeEnMaintenance = false;
    BTDATA = 0;
  }
  if (BTDATA == 'm') { //Si on reçoit un M, on active la maintenance
    //LE BODIC  
    Ouvert();
    lcd.clear();
    lcd.setCursor(3, 0);
    lcd.print("SERVICE EN");
    lcd.setCursor(3, 1);
    lcd.print("MAINTENANCE");
    Serial.println("MAINTENANCE");    
    tone (buzzer, 500);
    delay(1000);
    noTone(buzzer);
    //delay(200000000);
    systemeEnMaintenance = true;
    BTDATA = 0;
  }
  if (BTDATA == 'a') { //Si on reçoit un S, on arrête la maintenance
    //LE BODIC
    lcd.clear();
    lcd.setCursor(3, 0);
    lcd.print("FIN DE LA");
    lcd.setCursor(3, 1);
    lcd.print("MAINTENANCE");
    Serial.println("MAINTENANCE OFF");
    tone (buzzer, 500);
    delay(1000);
    noTone(buzzer);
    systemeEnMaintenance = false;
    BTDATA = 0;
  }
  if (BTDATA == 'r') { //Si on reçoit un M, on active la maintenance
    //Réparateur  
    Ouvert();
    lcd.clear();
    lcd.setCursor(3, 0);
    lcd.print("SERVICE EN");
    lcd.setCursor(3, 1);
    lcd.print("MAINTENANCE");
    Serial.println("MAINTENANCE");    
    tone (buzzer, 500);
    delay(1000);
    noTone(buzzer);
    //delay(200000000);
    systemeEnMaintenance = true;
    BTDATA = 0;
  }
  if (BTDATA == 'g') { //Si on reçoit un S, on arrête la maintenance
    //Réparateur
    lcd.clear();
    lcd.setCursor(3, 0);
    lcd.print("FIN DE LA");
    lcd.setCursor(3, 1);
    lcd.print("MAINTENANCE");
    Serial.println("MAINTENANCE OFF");
    tone (buzzer, 500);
    delay(1000);
    noTone(buzzer);
    systemeEnMaintenance = false;
    BTDATA = 0;
  }
  if (BTDATA == 'v') { //Si on reçoit un M, on active la maintenance
    //Camenen  
    Ouvert();
    lcd.clear();
    lcd.setCursor(3, 0);
    lcd.print("SERVICE EN");
    lcd.setCursor(3, 1);
    lcd.print("MAINTENANCE");
    Serial.println("MAINTENANCE");    
    tone (buzzer, 500);
    delay(1000);
    noTone(buzzer);
    //delay(200000000);
    systemeEnMaintenance = true;
    BTDATA = 0;
  }
  if (BTDATA == 't') { //Si on reçoit un S, on arrête la maintenance
    //Camenen
    lcd.clear();
    lcd.setCursor(3, 0);
    lcd.print("FIN DE LA");
    lcd.setCursor(3, 1);
    lcd.print("MAINTENANCE");
    Serial.println("MAINTENANCE OFF");
    tone (buzzer, 500);
    delay(1000);
    noTone(buzzer);
    systemeEnMaintenance = false;
    BTDATA = 0;
  }
  if (BTDATA == 'V') { //Si on reçoit un V, on active le verrouillage du système
    Ferme();
    lcd.clear();
    lcd.setCursor(4, 0);
    lcd.print("SYSTEME ");
    lcd.setCursor(3, 1);
    lcd.print("VERROUILLE");
    Serial.println("SYSTEME VERROUILLE");
    tone (buzzer, 500);
    delay(1000);
    noTone(buzzer);
    //delay(200000000);
    systemeEnMaintenance = true;
    BTDATA = 0;
  }
  if (BTDATA == 's') { //Si on reçoit un s, on arrête le verrouillage du système
    Ouvert();
    lcd.clear();
    lcd.setCursor(4, 0);
    lcd.print("SYSTEME");
    lcd.setCursor(2, 1);
    lcd.print("DEVERROUILLE");
    Serial.println("SYSTEME DEVERROUILLE");
    tone (buzzer, 500);
    delay(1000);
    noTone(buzzer);
    systemeEnMaintenance = false;
    BTDATA = 0;
  }
  if (!systemeEnMaintenance) { // Si système pas en maintenance
    //if (irrecv.decode(&results))
    //{
    // Serial.println(results.value,HEX);
    // irrecv.resume();
    //}
    //if (results.value == CODE_IR_9)
    //{
    // Ouvert();
    //}
    //if (results.value == CODE_IR_0)
    //{
    // Ferme();
    //}
    etatbouton = digitalRead (bouton);
    if (etatbouton == HIGH) {
      Serial.println("FERMETURE EN COURS");
      lcd.clear();
      lcd.setCursor(3, 0);
      lcd.print("FERMETURE");
      lcd.setCursor(3, 1);
      lcd.print("EN COURS");
      delay(1000);
      Ferme();
      tone (buzzer, 500);
      // wait 1 second
      delay (1000);
      // stops sound
      noTone(buzzer);
      delay(250);
      Serial.println("FERMETURE TERMINEE");
      lcd.clear();
      lcd.setCursor(3, 0);
      lcd.print("FERMETURE");
      lcd.setCursor(3, 1);
      lcd.print("TERMINEE");
      delay(2000);
      lcd.clear();
      boutonFermeturePorte = false;
    }
    etatbouton2 = digitalRead (bouton2);
    if (etatbouton2 == HIGH) {
      Serial.println("OUVERTURE EN COURS");
      lcd.clear();
      lcd.setCursor(3, 0);
      lcd.print("OUVERTURE");
      lcd.setCursor(3, 1);
      lcd.print("EN COURS");
      delay(1000);
      Ouvert();
      tone (buzzer, 1000);
      // wait 1 second
      delay (1000);
      // stops sound
      noTone(buzzer);
      delay(250);
      Serial.println("OUVERTURE TERMINEE");
      lcd.clear();
      lcd.setCursor(3, 0);
      lcd.print("OUVERTURE");
      lcd.setCursor(3, 1);
      lcd.print("TERMINEE");
      delay(2000);
      lcd.clear();
      boutonFermeturePorte = true;
    }
    etatbouton3 = digitalRead (bouton3);
    if (boutonFermeturePorte) {
      if (etatbouton3 == LOW) {
        delay(100);
        if (etatbouton3 == HIGH) {
          delay(8000);
          if (etatbouton3 == HIGH) {
            Serial.println("FERMETURE EN COURS");
            lcd.clear();
            lcd.setCursor(3, 0);
            lcd.print("FERMETURE");
            lcd.setCursor(3, 1);
            lcd.print("EN COURS");
            delay(1000);
            Ferme();
            tone (buzzer, 500);
            // wait 1 second
            delay (1000);
            // stops sound
            noTone(buzzer);
            delay(250);
            Serial.println("FERMETURE TERMINEE");
            lcd.clear();
            lcd.setCursor(3, 0);
            lcd.print("FERMETURE");
            lcd.setCursor(3, 1);
            lcd.print("TERMINEE");
            delay(2000);
            lcd.clear();
            boutonFermeturePorte = false;
          }
        }
      }
    }
    // see if no new maps are detected
    digitalWrite (ledpin1, LOW);
    if (! myReader.PICC_IsNewCardPresent ())
    {

      // not found, stops searching
      return;
    }
    // Checks whether the key tag can be read
    if (! myReader.PICC_ReadCardSerial ())
    {
      // failed to read
      return;
    }
    // Show UID on serial monitor
    String key = "";
    for (byte i = 0; i < myReader.uid.size; i ++)
    {
      key.concat (String (myReader.uid.uidByte [i] < 0x10 ? "0 " : " "));
      key.concat (String (myReader.uid.uidByte [i], HEX));
    }
    // Uppercase code of found key for better comparison
    key.toUpperCase ();

    // Print to monitor so you know which key is being read
    Serial.println ();
    Serial.print ("Badge d'accès :");
    Serial.print (key);

    // Grants access if key “..” is the same
    // Each key tag has its own code, read your code via monitor
    Serial.println ();
    Serial.print ("ACCES: ");
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("ACCES: ");

    if (key == BADGE_PORT_PLAISANCE)
    {

      // LED rouge off, LED verte on
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, HIGH);
      // Determines the frequency of the buzzer
      tone (buzzer, 1000);
      delay (1000);
      // stop, sound
      noTone(buzzer);
      Ouvert();

      // Print on monitor
      Serial.print ("AUTORISE : MALO LENORMAND");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MALO LENORMAND");
      delay (3000);
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, LOW);
      boutonFermeturePorte = true;
    }

    else if (key == BADGE_BLEU)
    {

      // LED rouge off, LED verte on
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, HIGH);
      // Déterminer la fréquence du buzzer
      tone (buzzer, 1000);
      delay (1000);
      // Stoper le son
      noTone(buzzer);
      Ouvert();

      // Écrire sur le moniteur
      Serial.print ("AUTORISE : MALO LENORMAND");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MALO LENORMAND");
      delay (3000);
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, LOW);
      boutonFermeturePorte = true;
    }

    else if (key == BADGE_NFC)
    {

      // LED rouge off, LED verte on
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, HIGH);
      // Déterminer la fréquence du buzzer
      tone (buzzer, 1000);
      delay (1000);
      // Stoper le son
      noTone(buzzer);
      Ouvert();

      // Écrire sur le moniteur
      Serial.print ("AUTORISE : MALO LENORMAND");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MALO LENORMAND");
      delay (3000);
      digitalWrite (ledpin1, HIGH);
      digitalWrite (ledpin2, LOW);
      boutonFermeturePorte = true;
    }

    else if (key == BADGE_ROUGE)
    {

      // LED rouge off, LED verte on
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, HIGH);
      // Déterminer la fréquence du buzzer
      tone (buzzer, 1000);
      delay (1000);
      // Stoper le son
      noTone(buzzer);
      Ouvert();

      // Écrire sur le moniteur
      RtcDateTime now = Rtc.GetDateTime();
      printDateTime(now);
      Serial.print ("AUTORISE : MALO LENORMAND Identifiant badge : F1 49 2F 3D");
      // pour sd
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MALO LENORMAND");
      
      delay (3000);
      digitalWrite (ledpin1, HIGH);
      digitalWrite (ledpin2, LOW);
      boutonFermeturePorte = true;
    }

    else if (key == JEAN)
    {

      // LED rouge off, LED verte on
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, HIGH);
      // Déterminer la fréquence du buzzer
      tone (buzzer, 1000);
      delay (1000);
      // Stoper le son
      noTone(buzzer);
      Ouvert();

      // Écrire sur le moniteur
      Serial.print ("AUTORISE : JEAN KERMORVANT");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("JEAN KERMORVANT");
      delay (3000);
      digitalWrite (ledpin1, HIGH);
      digitalWrite (ledpin2, LOW);
      boutonFermeturePorte = true;
    }

    else if (key == CPE)
    {

      // LED rouge off, LED verte on
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, HIGH);
      // Déterminer la fréquence du buzzer
      tone (buzzer, 1000);
      delay (1000);
      // Stoper le son
      noTone(buzzer);
      Ouvert();

      // Écrire sur le moniteur
      Serial.print ("AUTORISE : CPE");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MR LE BAGOUSSE");
      delay (3000);
      digitalWrite (ledpin1, HIGH);
      digitalWrite (ledpin2, LOW);
      boutonFermeturePorte = true;
    }

    else if (key == ERIC_CAMENEN)
    {

      // LED rouge off, LED verte on
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, HIGH);
      // Déterminer la fréquence du buzzer
      tone (buzzer, 1000);
      delay (1000);
      // Stoper le son
      noTone(buzzer);
      Ouvert();

      // Écrire sur le moniteur
      Serial.print ("AUTORISE : ERIC CAMENEN");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MR CAMENEN");
      delay (3000);
      digitalWrite (ledpin1, HIGH);
      digitalWrite (ledpin2, LOW);
      boutonFermeturePorte = true;
    }

    else if (key == LEBODIC)
    {

      // LED rouge off, LED verte on
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, HIGH);
      // Déterminer la fréquence du buzzer
      tone (buzzer, 1000);
      delay (1000);
      // Stoper le son
      noTone(buzzer);
      Ouvert();

      // Écrire sur le moniteur
      Serial.print ("AUTORISE : STEPHANE LE BODIC");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MR LE BODIC");
      delay (3000);
      digitalWrite (ledpin1, HIGH);
      digitalWrite (ledpin2, LOW);
      boutonFermeturePorte = true;
    }

    else if (key == DIRECTEUR)
    {

      // LED rouge off, LED verte on
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, HIGH);
      // Déterminer la fréquence du buzzer
      tone (buzzer, 1000);
      delay (1000);
      // Stoper le son
      noTone(buzzer);
      Ouvert();

      // Écrire sur le moniteur
      Serial.print ("AUTORISE : MR LE PICARD");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MR LE PICARD");
      delay (3000);
      digitalWrite (ledpin1, HIGH);
      digitalWrite (ledpin2, LOW);
      boutonFermeturePorte = true;
    }

    else if (key == DE_CRUZ)
    {

      // LED rouge off, LED verte on
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, HIGH);
      // Déterminer la fréquence du buzzer
      tone (buzzer, 1000);
      delay (1000);
      // Stoper le son
      noTone(buzzer);
      Ouvert();

      // Écrire sur le moniteur
      Serial.print ("AUTORISE : TECHNICIEN");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MR DE CRUZ");
      delay (3000);
      digitalWrite (ledpin1, HIGH);
      digitalWrite (ledpin2, LOW);
      boutonFermeturePorte = true;
    }

    else if (key == DANO)
    {

      // LED rouge off, LED verte on
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, HIGH);
      // Déterminer la fréquence du buzzer
      tone (buzzer, 1000);
      delay (1000);
      // Stoper le son
      noTone(buzzer);
      Ouvert();

      // Écrire sur le moniteur
      Serial.print ("AUTORISE : MR DANO");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("MR DANO");
      delay (3000);
      digitalWrite (ledpin1, HIGH);
      digitalWrite (ledpin2, LOW);
      boutonFermeturePorte = true;
    }

    else if (key == EWEN)
    {

      // LED rouge off, LED verte on
      digitalWrite (ledpin1, LOW);
      digitalWrite (ledpin2, HIGH);
      // Déterminer la fréquence du buzzer
      tone (buzzer, 1000);
      delay (1000);
      // Stoper le son
      noTone(buzzer);
      Ouvert();

      // Écrire sur le moniteur
      Serial.print ("AUTORISE : EWEN LE PRIOL");
      lcd.setCursor(7, 0);
      lcd.print("AUTORISE");
      lcd.setCursor(0, 1);
      lcd.print("EWEN LE PRIOL");
      delay (3000);
      digitalWrite (ledpin1, HIGH);
      digitalWrite (ledpin2, LOW);
      boutonFermeturePorte = true;
    }

    else {

      // Red LED on, green off
      digitalWrite (ledpin1, HIGH);
      digitalWrite (ledpin2, LOW);

      // Determines the frequency of the buzzer
      tone (buzzer, 500);
      // wait 1 second
      delay (1000);
      // stops sound
      noTone(buzzer);

      // Print on monitor
      Serial.print ("REFUSE");
      lcd.setCursor(7, 0);
      lcd.print("REFUSE");
      lcd.setCursor(0, 1);
      lcd.print(key);
      delay (3000);
    }
    Serial.println ();

    //etatbouton2 = digitalRead (bouton2);
    // if (etatbouton2==HIGH) {
    // Serial.println("lecture en cours...");
    // #define BADGE key
    // }

  }
}

void Ouvert() {
  monServomoteur.write(ouvert); // voir #define en haut
  iconeCadenas = true;
}

void Ferme() {
  monServomoteur.write(ferme); // voir #define en haut
  iconeCadenas = false;
}

void debut() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("A");
  delay(400);
  lcd.clear();
  delay(400);
  lcd.setCursor(0, 0);
  lcd.print("A");
  delay(400);
  lcd.clear();
  delay(500);
  lcd.setCursor(0, 0);
  lcd.print("A");
  delay(tempsdarret);
  lcd.setCursor(1, 0);
  lcd.print("R");
  delay(tempsdarret);
  lcd.setCursor(2, 0);
  lcd.print("D");
  delay(tempsdarret);
  lcd.setCursor(3, 0);
  lcd.print("U");
  delay(tempsdarret);
  lcd.setCursor(4, 0);
  lcd.print("I");
  delay(tempsdarret);
  lcd.setCursor(5, 0);
  lcd.print("N");
  delay(tempsdarret);
  lcd.setCursor(6, 0);
  lcd.print("O");
  delay(tempsdarret);
  lcd.setCursor(8, 0);
  lcd.print("S");
  delay(tempsdarret);
  lcd.setCursor(9, 0);
  lcd.print("E");
  delay(tempsdarret);
  lcd.setCursor(10, 0);
  lcd.print("C");
  delay(tempsdarret);
  lcd.setCursor(11, 0);
  lcd.print("U");
  delay(tempsdarret);
  lcd.setCursor(12, 0);
  lcd.print("R");
  delay(tempsdarret);
  lcd.setCursor(13, 0);
  lcd.print("I");
  delay(tempsdarret);
  lcd.setCursor(14, 0);
  lcd.print("T");
  delay(tempsdarret);
  lcd.setCursor(15, 0);
  lcd.print("Y");
  delay(tempsdarret);
  lcd.setCursor(0, 1);
  lcd.print("C");
  delay(tempsdarret);
  lcd.setCursor(1, 1);
  lcd.print("O");
  delay(tempsdarret);
  lcd.setCursor(2, 1);
  lcd.print("R");
  delay(tempsdarret);
  lcd.setCursor(3, 1);
  lcd.print("P");
  delay(tempsdarret);
  lcd.setCursor(4, 1);
  lcd.print("O");
  delay(tempsdarret);
  lcd.setCursor(5, 1);
  lcd.print("R");
  delay(tempsdarret);
  lcd.setCursor(6, 1);
  lcd.print("A");
  delay(tempsdarret);
  lcd.setCursor(7, 1);
  lcd.print("T");
  delay(tempsdarret);
  lcd.setCursor(8, 1);
  lcd.print("I");
  delay(tempsdarret);
  lcd.setCursor(9, 1);
  lcd.print("O");
  delay(tempsdarret);
  lcd.setCursor(10, 1);
  lcd.print("N");
  delay(tempsdarret);
  lcd.setCursor(12, 1);
  lcd.print("V");
  delay(tempsdarret);
  lcd.setCursor(13, 1);
  lcd.print("1");
  delay(tempsdarret);
  lcd.setCursor(14, 1);
  lcd.print(".");
  delay(tempsdarret);
  lcd.setCursor(15, 1);
  lcd.print("0");
  delay(1000);
  tone(buzzer, 1000);
  delay(250);
  noTone(buzzer);
  delay(250);
  tone(buzzer, 1000);
  delay(250);
  noTone(buzzer);
  delay(250);
  tone(buzzer, 1000);
  delay(250);
  noTone(buzzer);
  lcd.clear();
  lcd.setCursor(2, 0);
  lcd.print("L");
  delay(400);
  lcd.clear();
  delay(400);
  lcd.setCursor(2, 0);
  lcd.print("L");
  delay(400);
  lcd.clear();
  delay(500);
  lcd.setCursor(2, 0);
  lcd.print("L");
  delay(tempsdarret);
  lcd.setCursor(3, 0);
  lcd.print("A");
  delay(tempsdarret);
  lcd.setCursor(4, 0);
  lcd.print("N");
  delay(tempsdarret);
  lcd.setCursor(5, 0);
  lcd.print("C");
  delay(tempsdarret);
  lcd.setCursor(6, 0);
  lcd.print("E");
  delay(tempsdarret);
  lcd.setCursor(7, 0);
  lcd.print("M");
  delay(tempsdarret);
  lcd.setCursor(8, 0);
  lcd.print("E");
  delay(tempsdarret);
  lcd.setCursor(9, 0);
  lcd.print("N");
  delay(tempsdarret);
  lcd.setCursor(10, 0);
  lcd.print("T");
  delay(tempsdarret);
  lcd.setCursor(12, 0);
  lcd.print("D");
  delay(tempsdarret);
  lcd.setCursor(13, 0);
  lcd.print("E");
  lcd.setCursor(0, 1);
  lcd.print("R");
  delay(tempsdarret);
  lcd.setCursor(1, 1);
  lcd.print("F");
  delay(tempsdarret);
  lcd.setCursor(2, 1);
  lcd.print("I");
  delay(tempsdarret);
  lcd.setCursor(3, 1);
  lcd.print("D");
  delay(tempsdarret);
  lcd.setCursor(5, 1);
  lcd.print("S");
  delay(tempsdarret);
  lcd.setCursor(6, 1);
  lcd.print("E");
  delay(tempsdarret);
  lcd.setCursor(7, 1);
  lcd.print("C");
  delay(tempsdarret);
  lcd.setCursor(8, 1);
  lcd.print("U");
  delay(tempsdarret);
  lcd.setCursor(9, 1);
  lcd.print("R");
  delay(tempsdarret);
  lcd.setCursor(10, 1);
  lcd.print("I");
  delay(tempsdarret);
  lcd.setCursor(11, 1);
  lcd.print("T");
  delay(tempsdarret);
  lcd.setCursor(12, 1);
  lcd.print("Y");
  delay(400);
  lcd.setCursor(13, 1);
  lcd.print(".");
  delay(400);
  lcd.setCursor(14, 1);
  lcd.print(".");
  delay(400);
  lcd.setCursor(15, 1);
  lcd.print(".");
  delay(400);
  lcd.setCursor(13, 1);
  lcd.print(" ");
  lcd.setCursor(14, 1);
  lcd.print(" ");
  lcd.setCursor(15, 1);
  lcd.print(" ");
  delay(400);
  lcd.setCursor(13, 1);
  lcd.print(".");
  delay(400);
  lcd.setCursor(14, 1);
  lcd.print(".");
  delay(400);
  lcd.setCursor(15, 1);
  lcd.print(".");
  delay(400);
  lcd.setCursor(13, 1);
  lcd.print(" ");
  lcd.setCursor(14, 1);
  lcd.print(" ");
  lcd.setCursor(15, 1);
  lcd.print(" ");
  delay(400);
  lcd.setCursor(13, 1);
  lcd.print(".");
  delay(400);
  lcd.setCursor(14, 1);
  lcd.print(".");
  delay(400);
  lcd.setCursor(15, 1);
  lcd.print(".");
  delay(400);
  lcd.setCursor(13, 1);
  lcd.print(" ");
  lcd.setCursor(14, 1);
  lcd.print(" ");
  lcd.setCursor(15, 1);
  lcd.print(" ");
  lcd.clear();
  delay(1000);
}

#define countof(a) (sizeof(a) / sizeof(a[0]))

void printDateTime(const RtcDateTime& dt)
{
    char datestring[20];
    char dateonly[20];
          
    snprintf_P(datestring, 
            countof(datestring),
            PSTR("%02u/%02u"),
            dt.Day(),
            dt.Month());
            //dt.Year(),
            //dt.Hour(),
            //dt.Minute());
            //dt.Second() );
    Serial.print(datestring);
    snprintf_P(dateonly, 
            countof(dateonly),
            PSTR("%02u:%02u"),
            //dt.Month(),
            //dt.Day(),
            //dt.Year());
            dt.Hour(),
            dt.Minute());
            //dt.Second() );
    Serial.print(datestring);
    Serial.print(" ");
    Serial.print(dateonly);
    Serial.print(" ");
    lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(datestring);
}    
```

> [@flix09](#):
>
> Ceci dit,le code est joli! Bravo

Et merci !

---

<div class="post-metadata">

**Author:** ![flix09](https://avatars.discourse-cdn.com/v4/letter/f/7993a0/32.png) [@flix09](https://forum.arduino.cc/u/flix09)\
**Post date:** [February 23, 2023, 5:24pm UTC](https://forum.arduino.cc/t/systeme-alarme-bug-rfid/1093487/8 "2023-02-23T17:24:27Z")

</div>

> [@flix09](#):
>
> Apres,tu as mis en commentaires tout ce qui est d'afficher sur le moniteur...

Et si tu les decommentais?C'est normal que le monitur n'affiche rien,tu ne lui dis pas d'afficher quelques choses!Aucun Serial.print dans le code qui ne soit pas en commentaires...

---

<div class="post-metadata">

**Author:** ![Geek56](https://avatars.discourse-cdn.com/v4/letter/g/5daacb/32.png) [@Geek56](https://forum.arduino.cc/u/Geek56)\
**Post date:** [February 23, 2023, 5:28pm UTC](https://forum.arduino.cc/t/systeme-alarme-bug-rfid/1093487/9 "2023-02-23T17:28:42Z")

</div>

> [@Geek56](#):
>
> ```arduino
> // Print to monitor so you know which key is being read
> Serial.println ();
> Serial.print ("Badge d'accès :");
> Serial.print (key);
> 
> // Grants access if key “..” is the same
> // Each key tag has its own code, read your code via monitor
> Serial.println ();
> Serial.print ("ACCES: ");
> lcd.clear();
> lcd.setCursor(0, 0);
> lcd.print("ACCES: ");
> 
> ```

Regarde bien ! Il faut regarder dans le programme du haut. Mais si tu veux je vais les enlever.

---

<div class="post-metadata">

**Author:** ![flix09](https://avatars.discourse-cdn.com/v4/letter/f/7993a0/32.png) [@flix09](https://forum.arduino.cc/u/flix09)\
**Post date:** [February 23, 2023, 6:15pm UTC](https://forum.arduino.cc/t/systeme-alarme-bug-rfid/1093487/10 "2023-02-23T18:15:26Z")

</div>

Ah oui,oups...Bon,donc il n'y a rien sur le port serie c'est ça?  
Alors:  
1)Tu verifie ta vitesse de communications  
2)Tu essaie de bidouiller les retours chariot et tout le truc qu'il y a en bas du mioniteur  
3)Tu utilises tes balises de codes 😃  
4)Si ca ne marche pas,on verra ensuite si le 'bléme est dans ton code

---

<div class="post-metadata">

**Author:** ![Geek56](https://avatars.discourse-cdn.com/v4/letter/g/5daacb/32.png) [@Geek56](https://forum.arduino.cc/u/Geek56)\
**Post date:** [February 24, 2023, 12:18pm UTC](https://forum.arduino.cc/t/systeme-alarme-bug-rfid/1093487/11 "2023-02-24T12:18:06Z")

</div>

Bonjour,  
j'ai tout essayé ce que tu m'as dit et rien ne marche...  
Si tu veux, tu peux trouver mon projet sans le RFID ici : [Alarmy.ino - Wokwi Arduino and ESP32 Simulator](https://wokwi.com/projects/355209441636312065)  
Cordialement,  
Geek56.

---

<div class="post-metadata">

**Author:** ![hbachetti](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/hbachetti/32/238810_2.png) [@hbachetti](https://forum.arduino.cc/u/hbachetti)\
**Post date:** [February 24, 2023, 12:41pm UTC](https://forum.arduino.cc/t/systeme-alarme-bug-rfid/1093487/12 "2023-02-24T12:41:03Z")

</div>

As-tu essayé l'exemple [ReadNUID](https://github.com/miguelbalboa/rfid/blob/master/examples/ReadNUID/ReadNUID.ino) de la librairie ?

---

<div class="post-metadata">

**Author:** ![Geek56](https://avatars.discourse-cdn.com/v4/letter/g/5daacb/32.png) [@Geek56](https://forum.arduino.cc/u/Geek56)\
**Post date:** [February 24, 2023, 12:48pm UTC](https://forum.arduino.cc/t/systeme-alarme-bug-rfid/1093487/13 "2023-02-24T12:48:59Z")

</div>

Oui je l'ai essayé des dizaines de fois et ça marche.

---

<div class="post-metadata">

**Author:** ![flix09](https://avatars.discourse-cdn.com/v4/letter/f/7993a0/32.png) [@flix09](https://forum.arduino.cc/u/flix09)\
**Post date:** [February 24, 2023, 2:34pm UTC](https://forum.arduino.cc/t/systeme-alarme-bug-rfid/1093487/14 "2023-02-24T14:34:27Z")

</div>

C'est bizarre,ton code m'a l'air bon...J'ai moi meme utilise du RFID avec un code similaire et ça a bien marché...  
Peut etre que @hbachetti sera plus qualifié que moi.

---

<div class="post-metadata">

**Author:** ![jpbbricole](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jpbbricole/32/606025_2.png) [@jpbbricole](https://forum.arduino.cc/u/jpbbricole)\
**Post date:** [February 24, 2023, 3:23pm UTC](https://forum.arduino.cc/t/systeme-alarme-bug-rfid/1093487/15 "2023-02-24T15:23:57Z")

</div>

Bonjour Geek56

> [@Geek56](#):
>
> Petit problème lors de la vérification RFID qui débute ligne 199 : ça ne marche pas.

Petit problème, petite cause 😉

Tu as "zappé" le SPI:

`#include <SPI.h>`  
et dans setup()

`	SPI.begin(); // Init SPI bus`

Cordialement  
jpbbricole

---

<div class="post-metadata">

**Author:** ![hbachetti](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/hbachetti/32/238810_2.png) [@hbachetti](https://forum.arduino.cc/u/hbachetti)\
**Post date:** [February 24, 2023, 3:47pm UTC](https://forum.arduino.cc/t/systeme-alarme-bug-rfid/1093487/16 "2023-02-24T15:47:01Z")

</div>

> [@Geek56](#):
>
> ```arduino
> #define BADGE_PORT_PLAISANCE " C8 45 EB 61"
> 
> ```

Je n'avais pas regardé ton code de près.

> [@Geek56](#):
>
> ```arduino
> key.concat (String (myReader.uid.uidByte [i], HEX));
> 
> ```

Pourquoi toute cette soupe avec des Strings ?

Regarde ici, c'est aussi un code pour une alarme :  
[https://bitbucket.org/henri\_bachetti/alarm-system/src/master/arduino/alarm-system/rfid-reader.cpp](https://bitbucket.org/henri_bachetti/alarm-system/src/master/arduino/alarm-system/rfid-reader.cpp)  
Remarque comme l'UID est transformé en un uint32\_t (dernière ligne) :

```arduino
  return *(uint32_t *)rfid.uid.uidByte;

```

Ensuite l'appelant récupère la valeur en appelant la fonction readNUID() :

```arduino
  uint32_t nuid = readNUID();

```

Cette valeur est comparée avec des UID qui sont stockés sous forme de uint32\_t, par exemple :  
`#define BADGE_BLEU 0xA9B2BBB9`

Un petit détail à savoir :  
Un UID est sur 32 bits, si tu le lis à l'endroit (big endian) tu lis par exemple 0xA9B2BBB9. Mais l'AVR est little endian, donc il lira à l'envers : 0xB9BBB2A9.  
Il suffit donc de retourner tes identifiants :  
`#define BADGE_BLEU 0xB9BBB2A9`

Petite remarque supplémentaire : mis à part si tu utilises un ESP8266 ou un ESP32, évite les Strings qui fragmentent la mémoire.

---

<div class="post-metadata">

**Author:** ![flix09](https://avatars.discourse-cdn.com/v4/letter/f/7993a0/32.png) [@flix09](https://forum.arduino.cc/u/flix09)\
**Post date:** [February 24, 2023, 5:49pm UTC](https://forum.arduino.cc/t/systeme-alarme-bug-rfid/1093487/17 "2023-02-24T17:49:41Z")

</div>

> [@hbachetti](#):
>
> Pourquoi toute cette soupe avec des Strings ?

Je ne suis pas sur que ça pose probléme si?

---

<div class="post-metadata">

**Author:** ![hbachetti](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/hbachetti/32/238810_2.png) [@hbachetti](https://forum.arduino.cc/u/hbachetti)\
**Post date:** [February 24, 2023, 5:56pm UTC](https://forum.arduino.cc/t/systeme-alarme-bug-rfid/1093487/18 "2023-02-24T17:56:50Z")

</div>

C'est sans intérêt. un UID est codé sur 32 bits. Autant faire les comparaisons directement avec des valeurs 32 bits.

---

<div class="post-metadata">

**Author:** ![Geek56](https://avatars.discourse-cdn.com/v4/letter/g/5daacb/32.png) [@Geek56](https://forum.arduino.cc/u/Geek56)\
**Post date:** [February 24, 2023, 6:27pm UTC](https://forum.arduino.cc/t/systeme-alarme-bug-rfid/1093487/19 "2023-02-24T18:27:27Z")

</div>

Bonsoir,  
merci de vos réponses.

> [@jpbbricole](#):
>
> Tu as "zappé" le SPI

Effectivement, merci j'ai pu rajouter le SPI mais ça ne fonctionne pas mieux.

> [@hbachetti](#):
>
> Pourquoi toute cette soupe avec des Strings ?

> [@hbachetti](#):
>
> C'est sans intérêt. un UID est codé sur 32 bits. Autant faire les comparaisons directement avec des valeurs 32 bits.

Effectivement ça doit normalement (donc je ne vois pas l'intérêt de tout changer) rend au même et ça a très bien marché sur un autre système dont tu peux voir le code plus haut.

> [@hbachetti](#):
>
> Petite remarque supplémentaire : mis à part si tu utilises un ESP8266 ou un ESP32, évite les Strings qui fragmentent la mémoire.

J'utilise une carte arduino mega 2560 donc pas de problème. Merci quand même.

> [@flix09](#):
>
> J'ai moi meme utilise du RFID avec un code similaire et ça a bien marché...

Tu n'avais peut-être utilisé pas le même type de programmation que moi comme a expliqué @hbachetti.

Cordialement,  
Geek56.

---

<div class="post-metadata">

**Author:** ![hbachetti](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/hbachetti/32/238810_2.png) [@hbachetti](https://forum.arduino.cc/u/hbachetti)\
**Post date:** [February 24, 2023, 6:32pm UTC](https://forum.arduino.cc/t/systeme-alarme-bug-rfid/1093487/20 "2023-02-24T18:32:48Z")

</div>

> [@Geek56](#):
>
> J'utilise une carte arduino mega 2560 donc pas de problème. Merci quand même.

Ta déduction est rapide. Il n'y a aucune comparaison possible entre la RAM de 8K d'une MEGA et les 64K d'une ESP8266.  
Mais je te laisse faire, les problèmes n'apparaissent en général pas immédiatement.

Je pense que tu n'as pas compris qu'une valeur de 32 bits peut se comparer directement avec une autre valeur de 32 bits. Il n'y a aucun besoin de convertir en String.

[Next page](https://forum.arduino.cc/t/systeme-alarme-bug-rfid/1093487.md?page=2)
