Advertisement
Emakerz

Untitled

Apr 10th, 2020
4,516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.67 KB | None | 0 0
  1. // VIA CHAINE YOUTUBE EMAKERZ, https://www.youtube.com/channel/UCnNRQGz1SSgMGgFZzZRi8PA?view_as=subscriber
  2. #include <SPI.h>
  3. #include <MFRC522.h>librairie à ajouter (module RFID)
  4. #include <Password.h> //http://playground.arduino.cc/uploads/Code/Password.zip //librairie à ajouter (mot de passe)
  5. #include <Keypad.h> //http://www.arduino.cc/playground/uploads/Code/Keypad.zip //librairie à ajouter (pad)
  6. #include <Servo.h> //librairie à ajouter (servomoteur)
  7.  
  8. #define SS_PIN 10
  9. #define RST_PIN 9
  10.  
  11. MFRC522 mfrc522(SS_PIN, RST_PIN);
  12. Servo myservo; //declare le servo
  13. Password password = Password( "0000" ); //mot de passe pour ouvrir (à changer)
  14.  
  15. const byte ROWS = 4; // lignes
  16. const byte COLS = 3; // colonnes
  17. char keys[ROWS][COLS] = {
  18. {'1','2','3'},
  19. {'4','5','6'},
  20. {'7','8','9'},
  21. {'*','0','#'}
  22. };
  23. // Connecte le keypad ROW0, ROW1, ROW2 et ROW3 aux pins arduino
  24. byte rowPins[ROWS] = { 8, 7, 6, 5 };// Connecte le keypad COL0, COL1 et COL2 aux pins arduino
  25. byte colPins[COLS] = { 4, 3, 2 };
  26.  
  27. // Crée le Keypad
  28. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  29.  
  30. //Défini les pins
  31. int esp=A1;
  32. int servo=A0;
  33. int button=A4;
  34. int ledrouge=A2;
  35. int ledverte=A3;
  36.  
  37. void setup(){
  38. Serial.begin(9600);
  39. SPI.begin();
  40. mfrc522.PCD_Init();
  41. Serial.write(254);
  42. Serial.write(0x01);
  43. delay(200);
  44. pinMode(esp, INPUT);
  45. pinMode(button, INPUT);
  46. pinMode(ledrouge, OUTPUT);
  47. pinMode(ledverte, OUTPUT);
  48. myservo.attach(servo);
  49. keypad.addEventListener(keypadEvent);
  50. myservo.detach();
  51.  
  52. }
  53.  
  54. void loop(){
  55. keypad.getKey();
  56. /* ----Active la fonction de l'ESP si on signal est detecté---- */
  57.  
  58. if (analogRead(esp)>600){
  59. espservo();
  60. delay(2000);
  61. }
  62.  
  63. /* ----Active la fonction du bouton si il est pressé---- */
  64. if (analogRead(button)>900){
  65. boutton();
  66. }
  67.  
  68. //fonction pour le lecteur RFID
  69. if ( ! mfrc522.PICC_IsNewCardPresent()) {
  70. //Serial.println("newcardpresent");
  71. return;
  72. }
  73. if ( ! mfrc522.PICC_ReadCardSerial()) {
  74. return;
  75. }
  76.  
  77. //Montre l'UID de la carte RFID sur le moniteur série
  78. Serial.print("UID tag :");
  79. String content= "";
  80. byte letter;
  81. for (byte i = 0; i < mfrc522.uid.size; i++) {
  82. Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
  83. Serial.print(mfrc522.uid.uidByte[i], HEX);
  84. content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
  85. content.concat(String(mfrc522.uid.uidByte[i], HEX));
  86. }
  87. Serial.println();
  88. Serial.print("Message : ");
  89. content.toUpperCase();
  90.  
  91. /* ----Active la fonction pour ouvrir la porte si un badge RFID est detecté---- */
  92. if ((content.substring(1) == "XX XX XX XX")||(content.substring(1) == "XX XX XX XX")||(content.substring(1) == "XX XX XX XX")||(content.substring(1) == "XX XX XX XX")){ //remplacer les "X" par votre UID de Badge{
  93. Serial.println("Authorized access");
  94. delay(10);
  95. rfid();
  96. }
  97. //fait clignoter la led en rouge si un mauvais badge est detecté
  98. else {
  99. Serial.println(" Access denied");
  100. for (int i=0;i<4;i++){
  101. analogWrite(ledrouge, 700);
  102. delay(250);
  103. analogWrite(ledrouge, 0);
  104. delay(250);
  105. }
  106. }
  107. }
  108.  
  109. /* ----Fonctions pour faire fonctionner le pad---- */
  110. void keypadEvent(KeypadEvent eKey){
  111. switch (keypad.getState()){
  112. case PRESSED:
  113. Serial.print("Enter:");
  114. Serial.println(eKey);
  115. analogWrite(ledrouge,300);
  116. delay(50);
  117. analogWrite(ledrouge,0);
  118. Serial.write(254);
  119.  
  120. switch (eKey){
  121. case '*': checkPassword(); delay(1); password.reset(); break;
  122.  
  123. case '#': password.reset(); delay(1); break;
  124.  
  125. default: password.append(eKey); delay(1);
  126. }
  127. }
  128. }
  129.  
  130. void checkPassword(){
  131. myservo.attach(servo);
  132.  
  133. /* ----Actions à réaliser si le mdp est correct---- */
  134.  
  135. //ouvrir & faire clignoter led verte
  136. if (password.evaluate() && myservo.read()>=0 && myservo.read()<90){
  137. Serial.println("Accepted");
  138. Serial.write(254);
  139. delay(10);
  140. myservo.write(180);
  141. for (int i=0;i<4;i++){
  142. analogWrite(ledverte, 700);
  143. delay(250);
  144. analogWrite(ledverte, 0);
  145. delay(250);
  146. }
  147. myservo.detach();
  148. }
  149.  
  150. //Fermer & faire clignoter led verte
  151. else if (password.evaluate() && myservo.read()>90 && myservo.read()<=185){ //if password is right open
  152. Serial.println("Accepted");
  153. Serial.write(254);delay(10);
  154. myservo.write(0);
  155. for (int i=0;i<4;i++){
  156. analogWrite(ledverte, 700);
  157. delay(250);
  158. analogWrite(ledverte, 0);
  159. delay(250);
  160. }
  161. myservo.detach();
  162. }
  163.  
  164. //mdp incorrect donc ne rien faire & faire clignoter led rouge
  165. else{
  166. Serial.println("Denied"); //if passwords wrong keep locked
  167. Serial.write(254);delay(10);
  168. for (int i=0;i<4;i++){
  169. analogWrite(ledrouge, 700);
  170. delay(250);
  171. analogWrite(ledrouge, 0);
  172. delay(250);
  173. }
  174. }
  175. }
  176.  
  177. /* ----Fonctions pour activer le servomoteur avec l'ESP---- */
  178. void espservo(){
  179. myservo.attach(servo);
  180. if(myservo.read()>=0 && myservo.read()<90){
  181. analogWrite(ledrouge, 700);
  182. myservo.write(180);
  183. delay(2000);
  184. analogWrite(ledrouge, 0);
  185. }
  186. else if (myservo.read()>90 && myservo.read()<=185){
  187. analogWrite(ledverte, 700);
  188. myservo.write(0);
  189. delay(2000);
  190. analogWrite(ledverte, 0);
  191. }
  192. myservo.detach();
  193. }
  194.  
  195. /* ----Fonctions pour activer le servomoteur avec le RFID---- */
  196. void rfid(){
  197. myservo.attach(servo);
  198. if(myservo.read()>=0 && myservo.read()<90){
  199. myservo.write(180);
  200. for (int i=0;i<4;i++){
  201. analogWrite(ledverte, 700);
  202. delay(250);
  203. analogWrite(ledverte, 0);
  204. delay(250);
  205. }
  206. }
  207. else if (myservo.read()>90 && myservo.read()<=185){
  208. myservo.write(0);
  209. for (int i=0;i<4;i++){
  210. analogWrite(ledverte, 700);
  211. delay(250);
  212. analogWrite(ledverte, 0);
  213. delay(250);
  214. }
  215. }
  216. myservo.detach();
  217. password.reset();
  218. }
  219.  
  220. /* ----Fonctions pour activer le servomoteur avec le bouton---- */
  221. void boutton(){
  222. myservo.attach(servo);
  223. if(myservo.read()>=0 && myservo.read()<90){
  224. analogWrite(ledrouge, 700);
  225. myservo.write(180);
  226. delay(2000);
  227. analogWrite(ledrouge, 0);
  228. }
  229. else if (myservo.read()>90 && myservo.read()<=185){
  230. analogWrite(ledverte, 700);
  231. myservo.write(0);
  232. delay(2000);
  233. analogWrite(ledverte, 0);
  234. }
  235. myservo.detach();
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement