/*
Coffre à combinaison dérivé du colis radioactif.
Tous les tableaux ont été écrit en char pour permettre l'affichage en Serial.print,
LCD.print et comparaison des tableaux. Des tableaux en int posent des problèmes
La ligne du bas affiche la combinaison insérée au fur et à mesure.
*/
#include <Servo.h>
Servo Serrure;
#include "Wire.h"
#include "LiquidCrystal_I2C.h"
LiquidCrystal_I2C LCD(0x27,20,4);
int const Bp=6; //Bp Rouge
int const Hall=7; //Bp Jaune. L'effet Hall n'est pas utilisé.
int const Switch=5;
int const LedR=2;
int const LedJ=3;
int const Buzzer=12;
int Code;
int ZaN=0; //Défilement des chiffres de zéro à neuf.
int Digit=0; //Digit 0= 1er chiffre de combinaison inseré.
char TabloNum[10] {'0','1','2','3','4','5','6','7','8','9'};
char array_CCCombinaison[6] {'0','1','2','3','4','5'};
char array_CCCodeInsere[6] {'0','0','0','0','0','0'};
bool MatcH=false; //Si après avoir inséré 6 digits la combinaison est fausse il faudra recommencer.
void setup() {
Serial.begin(9600);
Serrure.attach(11);
Serrure.write(5); //Serrure verrouillée.
pinMode(LedR, OUTPUT);
pinMode(LedJ, OUTPUT);
pinMode(Switch, INPUT_PULLUP);
pinMode(Bp, INPUT_PULLUP);
pinMode(Hall, INPUT_PULLUP);
pinMode(Buzzer, OUTPUT);
pinMode(Switch, INPUT_PULLUP);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
LCD.init(); // initialisation de l'afficheur
LCD.backlight();
LCD.setCursor(3, 1);
LCD.print("-COFFRE CODE-");
LCD.setCursor(5,2);
LCD.print("A 6 DIGITS");
delay(2500);
LCD.clear();
}
void loop() {
Serial.print("TabloNum ");
for(int i=0; i<10; i++){Serial.print(TabloNum [i]);}
Serial.println("");
Serial.println("CCCodeInsere ");
for(int i=0; i<6; i++){Serial.print(array_CCCodeInsere[i]);}
Serial.println("");
Blink();
ZERO:
LCD.clear();
LCD.setCursor(2,0);
LCD.print("POUR INSERER LA");
LCD.setCursor(1,1);
LCD.print("COMBINAISON APPUYEZ");
LCD.setCursor(0,2);
LCD.print("SUR LE BOUTON ROUGE");
LCD.setCursor(1,3);
LCD.print("POUR CHAQUE DIGIT");
delay(1500);
if(digitalRead(Bp)==LOW)
{LCD.clear();Bip();delay(1500); goto ONE;}
LCD.clear();
LCD.setCursor(1,0);
LCD.print("LA VALIDATION EST");
LCD.setCursor(1,1);
LCD.print("CONFIRMEE PAR BIP");
LCD.setCursor(4,2);
LCD.print("APPUYEZ POUR");
LCD.setCursor(5,3);
LCD.print("COMMANCER");
delay(1500);
if(digitalRead(Bp)==LOW)
{LCD.clear();Blink();Bip();delay(1500); goto ONE;}
else{goto ZERO;}
ONE:
LCD.setCursor(0,0); //<===
LCD.print("DIGIT ");
LCD.print(Digit+1);
Code=TabloNum[ZaN];
LCD.setCursor(0,1);
LCD.print(TabloNum[ZaN]);
++ZaN; //Faire défiler le chiffre de 0 à 9.
if(ZaN==10) {ZaN=0;}
delay(1500);
if(digitalRead(Bp)==LOW)
{array_CCCodeInsere[Digit]={Code};
ZaN=0;++Digit;Bip();delay(450);}
delay(20);
Serial.println("CCCodeInsere ");
for(int i=0; i<6; i++){Serial.print(array_CCCodeInsere[i]);}
Serial.println("");
delay(20);
if (!memcmp(array_CCCodeInsere, array_CCCombinaison, 6)) //comparaison des 2 tableaux.
{MatcH=true;LCD.clear();LCD.setCursor(6,0);LCD.print("-MATCH-");
Serrure.write(95);LCD.setCursor(3,1);LCD.print("SAFE UNLOCKED!");Unlocked();goto TWO;}
else{LCD.print(" NO-MATCH !!!");}
delay(500);
LCD.setCursor(Digit,3); //<===
LCD.print(TabloNum[ZaN]); //<===
LCD.setCursor(6,3);
LCD.print(" "); //Pour éffacer l'affichage du 7 ème digit avant de passer à Wrong Code.
if(Digit==6 && MatcH==false) {Wrong();}
goto ONE;
TWO:
delay(3000);
delay((2000));
while(digitalRead(Bp)==HIGH)
{if(digitalRead(Switch)==LOW)
{LCD.setCursor(3,0);LCD.print(" -SAFE OPEN- ");delay(1500);}
else
{LCD.setCursor(3,0);LCD.print(" -SAFE CLOSED-");
LCD.setCursor(0,2);LCD.print("TO LOCK PRESS BOUTON");delay(1500);}
}
LCD.clear();
Digit=0;
ZaN=0;
MatcH=false;
Serrure.write(5);
LCD.setCursor(4,1);
LCD.print("SAFE LOCKED!");
Blink();
Bip();
delay(2500);
LCD.clear();
for (int i=0;i<6;i++) {array_CCCodeInsere[i]='0';} //Remise à zéro du tableau de comparaison.
goto ONE;
}
//Sous-programmes.
void Wrong() //Si 6 Digits insérés mais combinaison fausse.
{
Blink();
tone(Buzzer,50,500);
delay(650);
tone(Buzzer,50,500);
LCD.setCursor(4,1);
LCD.print("WRONG CODE");
delay(1500);
LCD.setCursor(4,2);
LCD.print("-TRY AGAIN-");
Digit=0;
ZaN=0;
MatcH=false;
for (int i=0;i<6;i++) {array_CCCodeInsere[i]='0';} //Remise à zéro du tableau
delay(1500);
LCD.clear();
}
void Bip() //Buzzer.
{
tone(Buzzer,100,400);
}
void Unlocked() //Sonnerie coffre déverouillé.
{
Blink();
tone(Buzzer,200,500);
delay(550);
tone(Buzzer,100,500);
delay(550);
}
void Blink() //Clignotement des Leds.
{
digitalWrite(LedR, HIGH);
delay(400);
digitalWrite(LedJ, HIGH);
delay(400);
digitalWrite(LedR, LOW);
delay(400);
digitalWrite(LedJ, LOW);
delay(400);
}