Advertisement
Ollivier

Serrure à code clavier 16 touches ecran LCD 16 x 2 (servo)

Apr 7th, 2020
489
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.42 KB | None | 0 0
  1. // ---------------------------------------------------------------------------------
  2. // Système de code d'entrée avec
  3. // Mini-clavier 16 boutons (matriciel 16 BP)
  4. // https://www.gotronic.fr/pj2-sbc-buttonmatrix-fr-1461.pdf
  5. // Ecranc LCD 16 x 2 Grove
  6. // http://wiki.seeedstudio.com/Grove-16x2_LCD_Series/
  7. // Buzzer
  8. // https://wiki.dfrobot.com/Digital_Buzzer_Module__SKU__DFR0032_
  9. // Je compte y rajouter un servo linéaire pour contrôler un verrou,
  10. // mais je n'en ai pas. Les livraisons ne sont plus assurées.
  11. // ---------------------------------------------------------------------------------
  12.  
  13. // on charge la bibliothèque de cablage simplifié
  14. #include <Wire.h>
  15. // on charge la bibliothèque de gestion de l'écran LCD
  16. #include <rgb_lcd.h>
  17.  
  18. // Ecran
  19.  
  20. rgb_lcd ecran;
  21.  
  22. //  Mini-Clavier
  23.  
  24. int rangee[] = {9, 8, 7, 6};
  25. int colonne[] = {10, 11, 12, 5};
  26. int col_scan;
  27.  
  28. // Code Saisi / Secret
  29.  
  30. // Code à afficher pour la saisie
  31. int code[4] = { 0, 0, 0, 0};
  32. // Compteur ("curseur" : position du caractère dans le code à saisir)
  33. int compteurCode = 0;
  34. // Code rempli totalement (4 chiffres saisis)
  35. bool codeComplet = 0;
  36. // Définition du code secret qui ouvre le "verrou"
  37. int codeSecret [4] = {8, 1, 7, 4};
  38. // Variable qui détermine si on est en mode "Changement de mot de passe"
  39. bool change = 0;
  40. // Variable qui détermine si on vérifie le mot de passe actuel avant de le changer
  41. bool verifie = 0;
  42. // Variable qui indique si on entre le nouveau code
  43. bool nouveau = 0;
  44.  
  45. // Buzzer
  46.  
  47. const int buzzerPin = 3;
  48.  
  49. // Notes
  50.  
  51.  
  52. const int Note_a = 440;
  53. const int Note_c = 261;
  54. const int Note_f = 349;
  55. const int Note_cH = 523;
  56. const int Note_dH = 587;
  57. const int Note_eH = 659;
  58. const int Note_fH = 698;
  59. const int Note_gH = 784;
  60. const int Note_aH = 880;
  61. const int Note_bH = 988;
  62. const int Note_cVH = 1046;
  63. const int Note_dVH = 1174;
  64. const int Note_eVH = 1318;
  65. const int Note_fVH = 1397;
  66.  
  67. void setup() {
  68.  
  69.   // on initialise et synchronise la communication avec le PC (dev)
  70.   Serial.begin(9600);
  71.  
  72.   // on déclare les pins du mini-clavier
  73.   for (int i = 0; i <= 3; i++)
  74.   {
  75.     pinMode(rangee[i], OUTPUT);
  76.     pinMode(colonne[i], INPUT);
  77.     // on initialise l'état les broches "colonne"
  78.     digitalWrite(colonne[i], HIGH);
  79.   }
  80.  
  81.   // on initialise l'écran LCD (16 2)
  82.   ecran.begin(16, 2);
  83.  
  84.   // on déclare de laboche du buzzer
  85.  
  86.   pinMode(buzzerPin, OUTPUT);
  87.  
  88.   // Affichage de l'"écran d'accueil"
  89.   ecran.clear();
  90.   ecran.setCursor (0, 0);
  91.   ecran.print ("  ");
  92.   ecran.print (code [0]);
  93.   ecran.print (" | ");
  94.   ecran.print (code [1]);
  95.   ecran.print (" | ");
  96.   ecran.print (code [2]);
  97.   ecran.print (" | ");
  98.   ecran.print (code [3]);
  99.   ecran.setCursor(1, 2);
  100.   ecran.print ("Entrer code ");
  101.   ecran.print (compteurCode + 1);
  102.   ecran.display();
  103.  
  104. }
  105. void loop() {
  106.   // Regarde si un bouton est enfoncé
  107.   for (int i = 0; i <= 3; i++)
  108.   {
  109.     digitalWrite(rangee[0], HIGH);
  110.     digitalWrite(rangee[1], HIGH);
  111.     digitalWrite(rangee[2], HIGH);
  112.     digitalWrite(rangee[3], HIGH);
  113.     digitalWrite(rangee[i], LOW);
  114.     for (int j = 0; j <= 3; j++)
  115.     {
  116.       col_scan = digitalRead(colonne[j]);
  117.       if (col_scan == LOW)
  118.       {
  119.         // Lorsqu'un bouton est enfoncé, appel de la fonction toucherBouton
  120.         // pour savoir quel bouton est enfoncé
  121.         toucherBouton(i, j);
  122.         delay(300);
  123.       }
  124.     }
  125.   }
  126. }
  127.  
  128. // -----------------------------------------------------------
  129.  
  130. void toucherBouton(int i, int j) {
  131.  
  132.   if (i == 0 && j == 0) {
  133.     saisieCode(1, Note_cH);
  134.   }
  135.   if (i == 0 && j == 1) {
  136.     saisieCode(2, Note_dH);
  137.   }
  138.   if (i == 0 && j == 2) {
  139.     saisieCode(3, Note_eH);
  140.   }
  141.   if (i == 0 && j == 3) {
  142.     effaceDel ();
  143.   }
  144.   if (i == 1 && j == 0) {
  145.     saisieCode(4, Note_fH);
  146.   }
  147.   if (i == 1 && j == 1) {
  148.     saisieCode(5, Note_gH);
  149.   }
  150.   if (i == 1 && j == 2) {
  151.     saisieCode(6, Note_aH);
  152.   }
  153.   if (i == 1 && j == 3) {
  154.     remiseAZero ();
  155.   }
  156.   if (i == 2 && j == 0) {
  157.     saisieCode(7, Note_bH);
  158.   }
  159.   if (i == 2 && j == 1) {
  160.     saisieCode(8, Note_cVH);
  161.   }
  162.   if (i == 2 && j == 2) {
  163.     saisieCode(9, Note_dVH);
  164.   }
  165.   if (i == 2 && j == 3) {
  166.     changerCode();
  167.   }
  168.   if (i == 3 && j == 0) {
  169.     aGauche();
  170.   }
  171.   if (i == 3 && j == 1) {
  172.     saisieCode(0, Note_eVH);
  173.   }
  174.   if (i == 3 && j == 2) {
  175.     aDroite();
  176.   }
  177.   if (i == 3 && j == 3) {
  178.     Serial.println(codeComplet);
  179.  
  180.     valideCode ();
  181.  
  182.     delay (2500);
  183.     initCode();
  184.   }
  185. }
  186.  
  187. // -----------------------------------------------------------
  188.  
  189. void saisieCode (int bout, int note) {
  190.   // si le 4ème caractère n'est pas encore saisi
  191.   if (codeComplet != 1) {
  192.     // la valeur du code affiché / saisi prend la valeur attribuée à ce bouton (bout)
  193.     code [compteurCode] = bout;
  194.  
  195.     // si le compteur (position du "curseur") n'est pas sur le dernier chiffre
  196.     if (compteurCode < 3) {
  197.       // on fait passer le curseur sur le chiffre suivant
  198.       compteurCode += 1;
  199.  
  200.       afficheCode ();
  201.       ecran.setCursor(1, 2);
  202.       ecran.print ("Entrer code ");
  203.       ecran.print (compteurCode + 1);
  204.       tone(buzzerPin, note, 250);
  205.  
  206.     } else {
  207.       // on indique que la saisie est complète
  208.       codeComplet = 1;
  209.       afficheCode ();
  210.       ecran.setCursor(1, 2);
  211.       ecran.print ("   Valider");
  212.       tone(buzzerPin, note, 250);
  213.     }
  214.     // si le 4ème caractère est déjà saisi
  215.   } else {
  216.     tone(buzzerPin, Note_c, 500);
  217.   }
  218.  
  219.   ecran.display();
  220. }
  221.  
  222. // -----------------------------------------------------------
  223.  
  224. void afficheCode () {
  225.  
  226.   ecran.clear();
  227.   ecran.setCursor (0, 0);
  228.   ecran.print ("  ");
  229.   ecran.print (code [0]);
  230.   ecran.print (" | ");
  231.   ecran.print (code [1]);
  232.   ecran.print (" | ");
  233.   ecran.print (code [2]);
  234.   ecran.print (" | ");
  235.   ecran.print (code [3]);
  236.  
  237. }
  238.  
  239. // -----------------------------------------------------------
  240.  
  241. void valideCode () {
  242.   if (nouveau != 1) {
  243.     if (change != 1 && verifie == 0) {
  244.       if (codeComplet == 1) {
  245.         codeComplet = 0;
  246.         if (code [0] == codeSecret [0] && code [1] == codeSecret [1] && code [2] == codeSecret [2] && code [3] == codeSecret [3]) {
  247.           ecran.clear();
  248.           ecran.setCursor(0, 0);
  249.           ecran.print("    Code OK !");
  250.           ecran.setCursor(1, 2);
  251.           ecran.print("   Entrez !");
  252.           ecran.display();
  253.           for (int i = 0; i < 3; i++) {
  254.             tone(buzzerPin, Note_fVH, 100);
  255.             delay (150);
  256.           }
  257.           for (int i = 0; i < 4; i++) {
  258.             code [i] = 0;
  259.           }
  260.           compteurCode = 0;
  261.         } else {
  262.           erreurCode ();
  263.         }
  264.       } else {
  265.         erreurCode ();
  266.       }
  267.     } else if (change == 1 && verifie == 1) {
  268.       if (codeComplet == 1) {
  269.         codeComplet = 0;
  270.         if (code [0] == codeSecret [0] && code [1] == codeSecret [1] && code [2] == codeSecret [2] && code [3] == codeSecret [3]) {
  271.           ecran.clear();
  272.           ecran.setCursor(0, 0);
  273.           ecran.print("    Code OK !");
  274.           ecran.setCursor(1, 2);
  275.           ecran.print("  Vérifié");
  276.           ecran.display();
  277.           nouveau = 1;
  278.           Serial.println (nouveau);
  279.           for (int i = 0; i < 3; i++) {
  280.             tone(buzzerPin, Note_fVH, 100);
  281.             delay (150);
  282.           }
  283.           for (int i = 0; i < 4; i++) {
  284.             code [i] = 0;
  285.           }
  286.           compteurCode = 0;
  287.         } else {
  288.           erreurCode ();
  289.         }
  290.       } else {
  291.         erreurCode ();
  292.       }
  293.     } else if (change == 1 && verifie == 0) {
  294.       initCode();
  295.       verifie = 1;
  296.       tone(buzzerPin, Note_aH, 250);
  297.     } else {
  298.       erreurCode ();
  299.     }
  300.   } else {
  301.     if (codeComplet == 1) {
  302.       for (int i = 0; i < 4; i++) {
  303.         codeSecret [i] = code [i];
  304.         code [i] = 0;
  305.         Serial.println (codeSecret [i]);
  306.       }
  307.       ecran.clear();
  308.       ecran.setCursor(0, 0);
  309.       ecran.print("      Code ");
  310.       ecran.setCursor(1, 2);
  311.       ecran.print("  enregistré");
  312.       ecran.display();
  313.       for (int i = 0; i < 3; i++) {
  314.         tone(buzzerPin, Note_fVH, 100);
  315.         delay (150);
  316.       }
  317.       verifie = 0;
  318.       change = 0;
  319.       nouveau = 0;
  320.       codeComplet = 0;
  321.       compteurCode = 0;
  322.       initCode();
  323.       delay (2000);
  324.  
  325.     } else {
  326.       erreurCode ();
  327.     }
  328.   }
  329. }
  330.  
  331.  
  332.  
  333. // -----------------------------------------------------------
  334.  
  335. void erreurCode () {
  336.  
  337.   ecran.clear();
  338.   ecran.setCursor(0, 0);
  339.   ecran.print("    ERREUR !");
  340.   ecran.setCursor(1, 2);
  341.   ecran.print("  Recommencez");
  342.   ecran.display();
  343.   tone(buzzerPin, Note_c, 500);
  344.   for (int i = 0; i < 4; i++) {
  345.     code [i] = 0;
  346.   }
  347.   compteurCode = 0;
  348.  
  349. }
  350.  
  351. // -----------------------------------------------------------
  352.  
  353. void initCode () {
  354.  
  355.   afficheCode ();
  356.   ecran.setCursor(1, 2);
  357.   ecran.print ("Entrer code ");
  358.   ecran.print (compteurCode + 1);
  359.   ecran.display();
  360.  
  361. }
  362.  
  363.  
  364. void effaceDel () {
  365.   if (compteurCode >= 1) {
  366.     compteurCode -= 1;
  367.     if (codeComplet != 1) {
  368.       code [compteurCode] = 0;
  369.     } else {
  370.       codeComplet = 0;
  371.       code [3] = 0;
  372.       compteurCode = 3;
  373.     }
  374.     initCode();
  375.     tone(buzzerPin, Note_f, 250);
  376.   } else {
  377.     initCode();
  378.     tone(buzzerPin, Note_a, 100);
  379.     delay (125);
  380.     tone(buzzerPin, Note_a, 100);
  381.  
  382.   }
  383. }
  384.  
  385.  
  386. void remiseAZero () {
  387.   for (int i = 0; i < 4; i++) {
  388.     code [i] = 0;
  389.   }
  390.   compteurCode = 0;
  391.   codeComplet = 0;
  392.   tone(buzzerPin, Note_cH, 250);
  393.   initCode ();
  394. }
  395.  
  396.  
  397. void aGauche () {
  398.   if (compteurCode >= 1) {
  399.     if (codeComplet == 1) {
  400.       compteurCode = 3;
  401.     } else {
  402.       compteurCode -= 1;
  403.     }
  404.     initCode();
  405.     tone(buzzerPin, Note_aH, 250);
  406.   } else {
  407.     tone(buzzerPin, Note_a, 100);
  408.     delay (125);
  409.     tone(buzzerPin, Note_a, 100);
  410.   }
  411. }
  412.  
  413.  
  414. void aDroite () {
  415.   if (compteurCode < 3) {
  416.     if (codeComplet != 1) {
  417.       compteurCode += 1;
  418.       initCode();
  419.       tone(buzzerPin, Note_aH, 250);
  420.     } else {
  421.       tone(buzzerPin, Note_a, 100);
  422.       delay (125);
  423.       tone(buzzerPin, Note_a, 100);
  424.     }
  425.   } else {
  426.     tone(buzzerPin, Note_a, 100);
  427.     delay (125);
  428.     tone(buzzerPin, Note_a, 100);
  429.   }
  430. }
  431.  
  432.  
  433. void changerCode () {
  434.   if (change != 1) {
  435.     change = 1;
  436.     ecran.clear();
  437.     ecran.print("   Changer le");
  438.     ecran.setCursor(1, 2);
  439.     ecran.print(" code secret ?");
  440.     ecran.display();
  441.     tone(buzzerPin, Note_aH, 250);
  442.   } else {
  443.     change = 0;
  444.     initCode();
  445.     tone(buzzerPin, Note_aH, 250);
  446.   }
  447. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement