Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // VIA CHAINE YOUTUBE EMAKERZ, https://www.youtube.com/channel/UCnNRQGz1SSgMGgFZzZRi8PA?view_as=subscriber
- #include <SPI.h>
- #include <MFRC522.h>librairie à ajouter (module RFID)
- #include <Password.h> //http://playground.arduino.cc/uploads/Code/Password.zip //librairie à ajouter (mot de passe)
- #include <Keypad.h> //http://www.arduino.cc/playground/uploads/Code/Keypad.zip //librairie à ajouter (pad)
- #include <Servo.h> //librairie à ajouter (servomoteur)
- #define SS_PIN 10
- #define RST_PIN 9
- MFRC522 mfrc522(SS_PIN, RST_PIN);
- Servo myservo; //declare le servo
- Password password = Password( "0000" ); //mot de passe pour ouvrir (à changer)
- const byte ROWS = 4; // lignes
- const byte COLS = 3; // colonnes
- char keys[ROWS][COLS] = {
- {'1','2','3'},
- {'4','5','6'},
- {'7','8','9'},
- {'*','0','#'}
- };
- // Connecte le keypad ROW0, ROW1, ROW2 et ROW3 aux pins arduino
- byte rowPins[ROWS] = { 8, 7, 6, 5 };// Connecte le keypad COL0, COL1 et COL2 aux pins arduino
- byte colPins[COLS] = { 4, 3, 2 };
- // Crée le Keypad
- Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
- //Défini les pins
- int esp=A1;
- int servo=A0;
- int button=A4;
- int ledrouge=A2;
- int ledverte=A3;
- void setup(){
- Serial.begin(9600);
- SPI.begin();
- mfrc522.PCD_Init();
- Serial.write(254);
- Serial.write(0x01);
- delay(200);
- pinMode(esp, INPUT);
- pinMode(button, INPUT);
- pinMode(ledrouge, OUTPUT);
- pinMode(ledverte, OUTPUT);
- myservo.attach(servo);
- keypad.addEventListener(keypadEvent);
- myservo.detach();
- }
- void loop(){
- keypad.getKey();
- /* ----Active la fonction de l'ESP si on signal est detecté---- */
- if (analogRead(esp)>600){
- espservo();
- delay(2000);
- }
- /* ----Active la fonction du bouton si il est pressé---- */
- if (analogRead(button)>900){
- boutton();
- }
- //fonction pour le lecteur RFID
- if ( ! mfrc522.PICC_IsNewCardPresent()) {
- //Serial.println("newcardpresent");
- return;
- }
- if ( ! mfrc522.PICC_ReadCardSerial()) {
- return;
- }
- //Montre l'UID de la carte RFID sur le moniteur série
- Serial.print("UID tag :");
- String content= "";
- byte letter;
- for (byte i = 0; i < mfrc522.uid.size; i++) {
- Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
- Serial.print(mfrc522.uid.uidByte[i], HEX);
- content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
- content.concat(String(mfrc522.uid.uidByte[i], HEX));
- }
- Serial.println();
- Serial.print("Message : ");
- content.toUpperCase();
- /* ----Active la fonction pour ouvrir la porte si un badge RFID est detecté---- */
- 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{
- Serial.println("Authorized access");
- delay(10);
- rfid();
- }
- //fait clignoter la led en rouge si un mauvais badge est detecté
- else {
- Serial.println(" Access denied");
- for (int i=0;i<4;i++){
- analogWrite(ledrouge, 700);
- delay(250);
- analogWrite(ledrouge, 0);
- delay(250);
- }
- }
- }
- /* ----Fonctions pour faire fonctionner le pad---- */
- void keypadEvent(KeypadEvent eKey){
- switch (keypad.getState()){
- case PRESSED:
- Serial.print("Enter:");
- Serial.println(eKey);
- analogWrite(ledrouge,300);
- delay(50);
- analogWrite(ledrouge,0);
- Serial.write(254);
- switch (eKey){
- case '*': checkPassword(); delay(1); password.reset(); break;
- case '#': password.reset(); delay(1); break;
- default: password.append(eKey); delay(1);
- }
- }
- }
- void checkPassword(){
- myservo.attach(servo);
- /* ----Actions à réaliser si le mdp est correct---- */
- //ouvrir & faire clignoter led verte
- if (password.evaluate() && myservo.read()>=0 && myservo.read()<90){
- Serial.println("Accepted");
- Serial.write(254);
- delay(10);
- myservo.write(180);
- for (int i=0;i<4;i++){
- analogWrite(ledverte, 700);
- delay(250);
- analogWrite(ledverte, 0);
- delay(250);
- }
- myservo.detach();
- }
- //Fermer & faire clignoter led verte
- else if (password.evaluate() && myservo.read()>90 && myservo.read()<=185){ //if password is right open
- Serial.println("Accepted");
- Serial.write(254);delay(10);
- myservo.write(0);
- for (int i=0;i<4;i++){
- analogWrite(ledverte, 700);
- delay(250);
- analogWrite(ledverte, 0);
- delay(250);
- }
- myservo.detach();
- }
- //mdp incorrect donc ne rien faire & faire clignoter led rouge
- else{
- Serial.println("Denied"); //if passwords wrong keep locked
- Serial.write(254);delay(10);
- for (int i=0;i<4;i++){
- analogWrite(ledrouge, 700);
- delay(250);
- analogWrite(ledrouge, 0);
- delay(250);
- }
- }
- }
- /* ----Fonctions pour activer le servomoteur avec l'ESP---- */
- void espservo(){
- myservo.attach(servo);
- if(myservo.read()>=0 && myservo.read()<90){
- analogWrite(ledrouge, 700);
- myservo.write(180);
- delay(2000);
- analogWrite(ledrouge, 0);
- }
- else if (myservo.read()>90 && myservo.read()<=185){
- analogWrite(ledverte, 700);
- myservo.write(0);
- delay(2000);
- analogWrite(ledverte, 0);
- }
- myservo.detach();
- }
- /* ----Fonctions pour activer le servomoteur avec le RFID---- */
- void rfid(){
- myservo.attach(servo);
- if(myservo.read()>=0 && myservo.read()<90){
- myservo.write(180);
- for (int i=0;i<4;i++){
- analogWrite(ledverte, 700);
- delay(250);
- analogWrite(ledverte, 0);
- delay(250);
- }
- }
- else if (myservo.read()>90 && myservo.read()<=185){
- myservo.write(0);
- for (int i=0;i<4;i++){
- analogWrite(ledverte, 700);
- delay(250);
- analogWrite(ledverte, 0);
- delay(250);
- }
- }
- myservo.detach();
- password.reset();
- }
- /* ----Fonctions pour activer le servomoteur avec le bouton---- */
- void boutton(){
- myservo.attach(servo);
- if(myservo.read()>=0 && myservo.read()<90){
- analogWrite(ledrouge, 700);
- myservo.write(180);
- delay(2000);
- analogWrite(ledrouge, 0);
- }
- else if (myservo.read()>90 && myservo.read()<=185){
- analogWrite(ledverte, 700);
- myservo.write(0);
- delay(2000);
- analogWrite(ledverte, 0);
- }
- myservo.detach();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement