Bonsoir max_grd
J'ai "étudié" ton programme, plus spécialement l'intégration du bouton
Le problème est ci-dessous:
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
Si ces 2 conditions ne sont pas true, on n'arrive jamais à else if ( !digitalRead (bouton)) { du fait des return;
Pour que ton bouton fonctionne, il faut presser le bouton et présenter une carte inconnue, assez acrobatique!
Donc le bloc de condition du bouton doit se trouver avant les 2 conditions avec return, donc au début de loop().
Je te mets le programme corrigé et testé,
J'ai supprimé digitalWrite(bouton, LOW); dans setup(). et remis des HIGH et LOW aux commandes de serrure.
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
String card_ID = "";
//Add as many cards you want
String Name1 = "16927176178";//carte blanche1
String Name2 = "24316222033";//bip pompier
String Name3 = "577690228";//carte blanche2
String Name4 = "90252241129";//porte clef1
String Name5 = "895181193";// porte clef2
//String Name5 = "10086395"; // Essais jpbbricole
String Name6 = "8247182210";//pass
int NumbCard[6];
int j = 0;
int statu[6];
int s = 0;
int redpin = A2; // select the pin for the red LED
int greenpin = A1; // select the pin for the green LED
int buzz = 7;
int serrure = 6;
int bouton = 2;
int bluepin = A0;
String Log;
String Name;
long Number;
int n ;
int ID = 1;
void setup() {
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
pinMode (buzz, OUTPUT);
pinMode (redpin, OUTPUT);
pinMode (greenpin, OUTPUT);
pinMode (bluepin, OUTPUT);
pinMode(serrure, OUTPUT);
pinMode (bouton, INPUT_PULLUP);
analogWrite(redpin, LOW);
analogWrite(greenpin, LOW);
analogWrite(bluepin, LOW);
digitalWrite(serrure, LOW);
delay(200);
}
void loop() {
digitalWrite(serrure, LOW);
if ( !digitalRead (bouton)) {
analogWrite(redpin, LOW);
analogWrite(greenpin, LOW);
analogWrite(bluepin, 255);
digitalWrite(serrure, HIGH);
delay(3000);
analogWrite(redpin, LOW);
analogWrite(greenpin, LOW);
analogWrite(bluepin, LOW);
digitalWrite(serrure, LOW);
}
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
for (byte i = 0; i < mfrc522.uid.size; i++) {
card_ID += mfrc522.uid.uidByte[i];
}
Serial.println(card_ID);
if (card_ID == Name1) {
Name = "First employee";
Number = "79 DD 81 2F";
analogWrite(redpin, LOW);
analogWrite(greenpin, 255);
digitalWrite(serrure, HIGH);
delay(200);
tone(buzz, 523, 50);
delay(50);
tone(buzz, 783, 50);
delay(50);
tone(buzz, 1046, 50);
delay(50);
tone(buzz, 1568, 50);
delay(50);
tone(buzz, 2093, 70);
delay(3000);
digitalWrite(serrure, LOW);
}
else if (card_ID == Name2) {
Name = "Second employee";
Number = 789101;
analogWrite(redpin, LOW);
analogWrite(greenpin, 255);
digitalWrite(serrure, HIGH);
delay(200);
tone(buzz, 523, 50);
delay(50);
tone(buzz, 783, 50);
delay(50);
tone(buzz, 1046, 50);
delay(50);
tone(buzz, 1568, 50);
delay(50);
tone(buzz, 2093, 70);
delay(3000);
digitalWrite(serrure, LOW);
}
else if (card_ID == Name3) {
Name = "Third employee";
Number = 789101;
analogWrite(redpin, LOW);
analogWrite(greenpin, 255);
digitalWrite(serrure, HIGH);
delay(200);
tone(buzz, 523, 50);
delay(50);
tone(buzz, 783, 50);
delay(50);
tone(buzz, 1046, 50);
delay(50);
tone(buzz, 1568, 50);
delay(50);
tone(buzz, 2093, 70);
delay(3000);
digitalWrite(serrure, LOW);
}
else if (card_ID == Name4) {
Name = "Fourth employee";
Number = 789101;
analogWrite(redpin, LOW);
analogWrite(greenpin, 255);
digitalWrite(serrure, HIGH);
delay(200);
tone(buzz, 523, 50);
delay(50);
tone(buzz, 783, 50);
delay(50);
tone(buzz, 1046, 50);
delay(50);
tone(buzz, 1568, 50);
delay(50);
tone(buzz, 2093, 70);
delay(3000);
digitalWrite(serrure, LOW);
}
else if (card_ID == Name5) {
Name = "Fiveth employee";
Number = 789101;
analogWrite(redpin, LOW);
analogWrite(greenpin, 255);
digitalWrite(serrure, HIGH);
delay(200);
tone(buzz, 523, 50);
delay(50);
tone(buzz, 783, 50);
delay(50);
tone(buzz, 1046, 50);
delay(50);
tone(buzz, 1568, 50);
delay(50);
tone(buzz, 2093, 70);
delay(3000);
digitalWrite(serrure, LOW);
}
else if (card_ID == Name6) {
Name = "Sixth employee";
Number = 789101;
analogWrite(redpin, LOW);
analogWrite(greenpin, 255);
digitalWrite(serrure, HIGH);
delay(200);
tone(buzz, 523, 50);
delay(50);
tone(buzz, 783, 50);
delay(50);
tone(buzz, 1046, 50);
delay(50);
tone(buzz, 1568, 50);
delay(50);
tone(buzz, 2093, 70);
delay(3000);
digitalWrite(serrure, LOW);
}
else {
analogWrite (greenpin, 0);
digitalWrite (serrure, LOW);
analogWrite(redpin, 255);
delay(200);
tone(buzz, 370, 50);
delay(100);
tone(buzz, 370, 300);
delay(3000);
goto cont;
}
Serial.print("Name \t"); Serial.println(Name);
cont:
delay(2000);
digitalWrite(greenpin, LOW);
digitalWrite(redpin, LOW);
card_ID = "";
}
Je te cache pas qu'il y a du boulot d'optimisation dans ton programme, si tu le veux, on peut s'y atteler progressivement.
Je ne suis pas fan du tout de cette façon de procéder:
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
des if() bien imbriqués sont préférables.
J'ai gardé le meilleur (ou le pire
) pour la fin, le goto cont;, je ne savait même pas que le goto existai dans ce langage, à banir!
Le programme:
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
String card_ID = "";
//Add as many cards you want
String Name1 = "16927176178";//carte blanche1
String Name2 = "24316222033";//bip pompier
String Name3 = "577690228";//carte blanche2
String Name4 = "90252241129";//porte clef1
String Name5 = "895181193";// porte clef2
//String Name5 = "10086395"; // Essais jpbbricole
String Name6 = "8247182210";//pass
int NumbCard[6];
int j = 0;
int statu[6];
int s = 0;
int redpin = A2; // select the pin for the red LED
int greenpin = A1; // select the pin for the green LED
int buzz = 7;
int serrure = 6;
int bouton = 2;
int bluepin = A0;
String Log;
String Name;
long Number;
int n ;
int ID = 1;
void setup() {
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
pinMode (buzz, OUTPUT);
pinMode (redpin, OUTPUT);
pinMode (greenpin, OUTPUT);
pinMode (bluepin, OUTPUT);
pinMode(serrure, OUTPUT);
pinMode (bouton, INPUT_PULLUP);
analogWrite(redpin, LOW);
analogWrite(greenpin, LOW);
analogWrite(bluepin, LOW);
digitalWrite(serrure, LOW);
delay(200);
}
void loop() {
digitalWrite(serrure, LOW);
if ( !digitalRead (bouton)) {
analogWrite(redpin, LOW);
analogWrite(greenpin, LOW);
analogWrite(bluepin, 255);
digitalWrite(serrure, HIGH);
delay(3000);
analogWrite(redpin, LOW);
analogWrite(greenpin, LOW);
analogWrite(bluepin, LOW);
digitalWrite(serrure, LOW);
}
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
for (byte i = 0; i < mfrc522.uid.size; i++) {
card_ID += mfrc522.uid.uidByte[i];
}
Serial.println(card_ID);
if (card_ID == Name1) {
Name = "First employee";
Number = "79 DD 81 2F";
analogWrite(redpin, LOW);
analogWrite(greenpin, 255);
digitalWrite(serrure, HIGH);
delay(200);
tone(buzz, 523, 50);
delay(50);
tone(buzz, 783, 50);
delay(50);
tone(buzz, 1046, 50);
delay(50);
tone(buzz, 1568, 50);
delay(50);
tone(buzz, 2093, 70);
delay(3000);
digitalWrite(serrure, LOW);
}
else if (card_ID == Name2) {
Name = "Second employee";
Number = 789101;
analogWrite(redpin, LOW);
analogWrite(greenpin, 255);
digitalWrite(serrure, HIGH);
delay(200);
tone(buzz, 523, 50);
delay(50);
tone(buzz, 783, 50);
delay(50);
tone(buzz, 1046, 50);
delay(50);
tone(buzz, 1568, 50);
delay(50);
tone(buzz, 2093, 70);
delay(3000);
digitalWrite(serrure, LOW);
}
else if (card_ID == Name3) {
Name = "Third employee";
Number = 789101;
analogWrite(redpin, LOW);
analogWrite(greenpin, 255);
digitalWrite(serrure, HIGH);
delay(200);
tone(buzz, 523, 50);
delay(50);
tone(buzz, 783, 50);
delay(50);
tone(buzz, 1046, 50);
delay(50);
tone(buzz, 1568, 50);
delay(50);
tone(buzz, 2093, 70);
delay(3000);
digitalWrite(serrure, LOW);
}
else if (card_ID == Name4) {
Name = "Fourth employee";
Number = 789101;
analogWrite(redpin, LOW);
analogWrite(greenpin, 255);
digitalWrite(serrure, HIGH);
delay(200);
tone(buzz, 523, 50);
delay(50);
tone(buzz, 783, 50);
delay(50);
tone(buzz, 1046, 50);
delay(50);
tone(buzz, 1568, 50);
delay(50);
tone(buzz, 2093, 70);
delay(3000);
digitalWrite(serrure, LOW);
}
else if (card_ID == Name5) {
Name = "Fiveth employee";
Number = 789101;
analogWrite(redpin, LOW);
analogWrite(greenpin, 255);
digitalWrite(serrure, HIGH);
delay(200);
tone(buzz, 523, 50);
delay(50);
tone(buzz, 783, 50);
delay(50);
tone(buzz, 1046, 50);
delay(50);
tone(buzz, 1568, 50);
delay(50);
tone(buzz, 2093, 70);
delay(3000);
digitalWrite(serrure, LOW);
}
else if (card_ID == Name6) {
Name = "Sixth employee";
Number = 789101;
analogWrite(redpin, LOW);
analogWrite(greenpin, 255);
digitalWrite(serrure, HIGH);
delay(200);
tone(buzz, 523, 50);
delay(50);
tone(buzz, 783, 50);
delay(50);
tone(buzz, 1046, 50);
delay(50);
tone(buzz, 1568, 50);
delay(50);
tone(buzz, 2093, 70);
delay(3000);
digitalWrite(serrure, LOW);
}
else {
analogWrite (greenpin, 0);
digitalWrite (serrure, LOW);
analogWrite(redpin, 255);
delay(200);
tone(buzz, 370, 50);
delay(100);
tone(buzz, 370, 300);
delay(3000);
goto cont;
}
Serial.print("Name \t"); Serial.println(Name);
cont:
delay(2000);
digitalWrite(greenpin, LOW);
digitalWrite(redpin, LOW);
card_ID = "";
}
Il est testé, il fonctionne.
A+
Cordialement
jpbbricole