Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ---------------------------------------------------------------------------------
- // Système de code d'entrée avec
- // Mini-clavier 16 boutons (matriciel 16 BP)
- // https://www.gotronic.fr/pj2-sbc-buttonmatrix-fr-1461.pdf
- // Ecranc LCD 16 x 2 Grove
- // http://wiki.seeedstudio.com/Grove-16x2_LCD_Series/
- // Buzzer
- // https://wiki.dfrobot.com/Digital_Buzzer_Module__SKU__DFR0032_
- // Je compte y rajouter un servo linéaire pour contrôler un verrou,
- // mais je n'en ai pas. Les livraisons ne sont plus assurées.
- // ---------------------------------------------------------------------------------
- // on charge la bibliothèque de cablage simplifié
- #include <Wire.h>
- // on charge la bibliothèque de gestion de l'écran LCD
- #include <rgb_lcd.h>
- // Ecran
- rgb_lcd ecran;
- // Mini-Clavier
- int rangee[] = {9, 8, 7, 6};
- int colonne[] = {10, 11, 12, 5};
- int col_scan;
- // Code Saisi / Secret
- // Code à afficher pour la saisie
- int code[4] = { 0, 0, 0, 0};
- // Compteur ("curseur" : position du caractère dans le code à saisir)
- int compteurCode = 0;
- // Code rempli totalement (4 chiffres saisis)
- bool codeComplet = 0;
- // Définition du code secret qui ouvre le "verrou"
- int codeSecret [4] = {8, 1, 7, 4};
- // Variable qui détermine si on est en mode "Changement de mot de passe"
- bool change = 0;
- // Variable qui détermine si on vérifie le mot de passe actuel avant de le changer
- bool verifie = 0;
- // Variable qui indique si on entre le nouveau code
- bool nouveau = 0;
- // Buzzer
- const int buzzerPin = 3;
- // Notes
- const int Note_a = 440;
- const int Note_c = 261;
- const int Note_f = 349;
- const int Note_cH = 523;
- const int Note_dH = 587;
- const int Note_eH = 659;
- const int Note_fH = 698;
- const int Note_gH = 784;
- const int Note_aH = 880;
- const int Note_bH = 988;
- const int Note_cVH = 1046;
- const int Note_dVH = 1174;
- const int Note_eVH = 1318;
- const int Note_fVH = 1397;
- void setup() {
- // on initialise et synchronise la communication avec le PC (dev)
- Serial.begin(9600);
- // on déclare les pins du mini-clavier
- for (int i = 0; i <= 3; i++)
- {
- pinMode(rangee[i], OUTPUT);
- pinMode(colonne[i], INPUT);
- // on initialise l'état les broches "colonne"
- digitalWrite(colonne[i], HIGH);
- }
- // on initialise l'écran LCD (16 2)
- ecran.begin(16, 2);
- // on déclare de laboche du buzzer
- pinMode(buzzerPin, OUTPUT);
- // Affichage de l'"écran d'accueil"
- ecran.clear();
- ecran.setCursor (0, 0);
- ecran.print (" ");
- ecran.print (code [0]);
- ecran.print (" | ");
- ecran.print (code [1]);
- ecran.print (" | ");
- ecran.print (code [2]);
- ecran.print (" | ");
- ecran.print (code [3]);
- ecran.setCursor(1, 2);
- ecran.print ("Entrer code ");
- ecran.print (compteurCode + 1);
- ecran.display();
- }
- void loop() {
- // Regarde si un bouton est enfoncé
- for (int i = 0; i <= 3; i++)
- {
- digitalWrite(rangee[0], HIGH);
- digitalWrite(rangee[1], HIGH);
- digitalWrite(rangee[2], HIGH);
- digitalWrite(rangee[3], HIGH);
- digitalWrite(rangee[i], LOW);
- for (int j = 0; j <= 3; j++)
- {
- col_scan = digitalRead(colonne[j]);
- if (col_scan == LOW)
- {
- // Lorsqu'un bouton est enfoncé, appel de la fonction toucherBouton
- // pour savoir quel bouton est enfoncé
- toucherBouton(i, j);
- delay(300);
- }
- }
- }
- }
- // -----------------------------------------------------------
- void toucherBouton(int i, int j) {
- if (i == 0 && j == 0) {
- saisieCode(1, Note_cH);
- }
- if (i == 0 && j == 1) {
- saisieCode(2, Note_dH);
- }
- if (i == 0 && j == 2) {
- saisieCode(3, Note_eH);
- }
- if (i == 0 && j == 3) {
- effaceDel ();
- }
- if (i == 1 && j == 0) {
- saisieCode(4, Note_fH);
- }
- if (i == 1 && j == 1) {
- saisieCode(5, Note_gH);
- }
- if (i == 1 && j == 2) {
- saisieCode(6, Note_aH);
- }
- if (i == 1 && j == 3) {
- remiseAZero ();
- }
- if (i == 2 && j == 0) {
- saisieCode(7, Note_bH);
- }
- if (i == 2 && j == 1) {
- saisieCode(8, Note_cVH);
- }
- if (i == 2 && j == 2) {
- saisieCode(9, Note_dVH);
- }
- if (i == 2 && j == 3) {
- changerCode();
- }
- if (i == 3 && j == 0) {
- aGauche();
- }
- if (i == 3 && j == 1) {
- saisieCode(0, Note_eVH);
- }
- if (i == 3 && j == 2) {
- aDroite();
- }
- if (i == 3 && j == 3) {
- Serial.println(codeComplet);
- valideCode ();
- delay (2500);
- initCode();
- }
- }
- // -----------------------------------------------------------
- void saisieCode (int bout, int note) {
- // si le 4ème caractère n'est pas encore saisi
- if (codeComplet != 1) {
- // la valeur du code affiché / saisi prend la valeur attribuée à ce bouton (bout)
- code [compteurCode] = bout;
- // si le compteur (position du "curseur") n'est pas sur le dernier chiffre
- if (compteurCode < 3) {
- // on fait passer le curseur sur le chiffre suivant
- compteurCode += 1;
- afficheCode ();
- ecran.setCursor(1, 2);
- ecran.print ("Entrer code ");
- ecran.print (compteurCode + 1);
- tone(buzzerPin, note, 250);
- } else {
- // on indique que la saisie est complète
- codeComplet = 1;
- afficheCode ();
- ecran.setCursor(1, 2);
- ecran.print (" Valider");
- tone(buzzerPin, note, 250);
- }
- // si le 4ème caractère est déjà saisi
- } else {
- tone(buzzerPin, Note_c, 500);
- }
- ecran.display();
- }
- // -----------------------------------------------------------
- void afficheCode () {
- ecran.clear();
- ecran.setCursor (0, 0);
- ecran.print (" ");
- ecran.print (code [0]);
- ecran.print (" | ");
- ecran.print (code [1]);
- ecran.print (" | ");
- ecran.print (code [2]);
- ecran.print (" | ");
- ecran.print (code [3]);
- }
- // -----------------------------------------------------------
- void valideCode () {
- if (nouveau != 1) {
- if (change != 1 && verifie == 0) {
- if (codeComplet == 1) {
- codeComplet = 0;
- if (code [0] == codeSecret [0] && code [1] == codeSecret [1] && code [2] == codeSecret [2] && code [3] == codeSecret [3]) {
- ecran.clear();
- ecran.setCursor(0, 0);
- ecran.print(" Code OK !");
- ecran.setCursor(1, 2);
- ecran.print(" Entrez !");
- ecran.display();
- for (int i = 0; i < 3; i++) {
- tone(buzzerPin, Note_fVH, 100);
- delay (150);
- }
- for (int i = 0; i < 4; i++) {
- code [i] = 0;
- }
- compteurCode = 0;
- } else {
- erreurCode ();
- }
- } else {
- erreurCode ();
- }
- } else if (change == 1 && verifie == 1) {
- if (codeComplet == 1) {
- codeComplet = 0;
- if (code [0] == codeSecret [0] && code [1] == codeSecret [1] && code [2] == codeSecret [2] && code [3] == codeSecret [3]) {
- ecran.clear();
- ecran.setCursor(0, 0);
- ecran.print(" Code OK !");
- ecran.setCursor(1, 2);
- ecran.print(" Vérifié");
- ecran.display();
- nouveau = 1;
- Serial.println (nouveau);
- for (int i = 0; i < 3; i++) {
- tone(buzzerPin, Note_fVH, 100);
- delay (150);
- }
- for (int i = 0; i < 4; i++) {
- code [i] = 0;
- }
- compteurCode = 0;
- } else {
- erreurCode ();
- }
- } else {
- erreurCode ();
- }
- } else if (change == 1 && verifie == 0) {
- initCode();
- verifie = 1;
- tone(buzzerPin, Note_aH, 250);
- } else {
- erreurCode ();
- }
- } else {
- if (codeComplet == 1) {
- for (int i = 0; i < 4; i++) {
- codeSecret [i] = code [i];
- code [i] = 0;
- Serial.println (codeSecret [i]);
- }
- ecran.clear();
- ecran.setCursor(0, 0);
- ecran.print(" Code ");
- ecran.setCursor(1, 2);
- ecran.print(" enregistré");
- ecran.display();
- for (int i = 0; i < 3; i++) {
- tone(buzzerPin, Note_fVH, 100);
- delay (150);
- }
- verifie = 0;
- change = 0;
- nouveau = 0;
- codeComplet = 0;
- compteurCode = 0;
- initCode();
- delay (2000);
- } else {
- erreurCode ();
- }
- }
- }
- // -----------------------------------------------------------
- void erreurCode () {
- ecran.clear();
- ecran.setCursor(0, 0);
- ecran.print(" ERREUR !");
- ecran.setCursor(1, 2);
- ecran.print(" Recommencez");
- ecran.display();
- tone(buzzerPin, Note_c, 500);
- for (int i = 0; i < 4; i++) {
- code [i] = 0;
- }
- compteurCode = 0;
- }
- // -----------------------------------------------------------
- void initCode () {
- afficheCode ();
- ecran.setCursor(1, 2);
- ecran.print ("Entrer code ");
- ecran.print (compteurCode + 1);
- ecran.display();
- }
- void effaceDel () {
- if (compteurCode >= 1) {
- compteurCode -= 1;
- if (codeComplet != 1) {
- code [compteurCode] = 0;
- } else {
- codeComplet = 0;
- code [3] = 0;
- compteurCode = 3;
- }
- initCode();
- tone(buzzerPin, Note_f, 250);
- } else {
- initCode();
- tone(buzzerPin, Note_a, 100);
- delay (125);
- tone(buzzerPin, Note_a, 100);
- }
- }
- void remiseAZero () {
- for (int i = 0; i < 4; i++) {
- code [i] = 0;
- }
- compteurCode = 0;
- codeComplet = 0;
- tone(buzzerPin, Note_cH, 250);
- initCode ();
- }
- void aGauche () {
- if (compteurCode >= 1) {
- if (codeComplet == 1) {
- compteurCode = 3;
- } else {
- compteurCode -= 1;
- }
- initCode();
- tone(buzzerPin, Note_aH, 250);
- } else {
- tone(buzzerPin, Note_a, 100);
- delay (125);
- tone(buzzerPin, Note_a, 100);
- }
- }
- void aDroite () {
- if (compteurCode < 3) {
- if (codeComplet != 1) {
- compteurCode += 1;
- initCode();
- tone(buzzerPin, Note_aH, 250);
- } else {
- tone(buzzerPin, Note_a, 100);
- delay (125);
- tone(buzzerPin, Note_a, 100);
- }
- } else {
- tone(buzzerPin, Note_a, 100);
- delay (125);
- tone(buzzerPin, Note_a, 100);
- }
- }
- void changerCode () {
- if (change != 1) {
- change = 1;
- ecran.clear();
- ecran.print(" Changer le");
- ecran.setCursor(1, 2);
- ecran.print(" code secret ?");
- ecran.display();
- tone(buzzerPin, Note_aH, 250);
- } else {
- change = 0;
- initCode();
- tone(buzzerPin, Note_aH, 250);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement