Arduino - RFID Tarda o no lee tarjeta

 
Vista:

RFID Tarda o no lee tarjeta

Publicado por danielFTV (1 intervención) el 08/08/2019 23:50:30
Buenas. Estoy realizando un proyecto en el que uso un módulo de RFID RC522 con el que quiero hacer un estilo de automatización. Le tengo incorporado un relé, una pantalla LCD y un LED RGB.
La cuestión es que el módulo tarda mucho en reconocer la tarjeta, haciendo que ésta sea totalmente inútil puesto que se busca la comodidad y la rapidez y con lo que tarda no tiene sentido usarlo.
Otras veces directamente no lo lee.
Adjunto el código utilizado, es sacado de internet también.
Adelanto que habrá muchos fallos puesto que soy nuevo.
Gracias de antemano.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <LiquidCrystal_I2C.h> 
#include <SPI.h>    
#include <MFRC522.h>  
 
#define greenLed 7      
#define redLed   5      
#define yellowLed 6     
 
byte myCards[] = {0x87,0x24,0x43,0x14,   //Metropolitando
                  0x87,0x24,0xA7,0x33};  //Llavero azul
 
int successRead;
 
byte dummy = 0x00;
 
byte readCard[4];
 
const int rele = 2;
 
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
LiquidCrystal_I2C lcd(0x27,16,2);
void setup() {
  Serial.begin(9600);
  pinMode(greenLed,OUTPUT);
  pinMode(redLed,OUTPUT);
  pinMode(yellowLed,OUTPUT);
  lcd.init();
  lcd.backlight();
  lcd.clear();
 lcd.setCursor(1,0);
  lcd.setCursor (4,1);
  lcd.print("");
  pinMode(rele,OUTPUT);
  Serial.begin(9600);
  SPI.begin();                                  //Iniciamos protocolo SPI
  mfrc522.PCD_Init();
  mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
   Serial.println("IDENTIFIQUESE...");
     for (int i =9 ; i<12 ; i++)
            pinMode(i, OUTPUT);
}
 
void success() {
 
 
}
void Color(int R, int G, int B)
    {
        analogWrite(9 , R) ;   // Red    - Rojo
        analogWrite(10, G) ;   // Green - Verde
        analogWrite(11, B) ;   // Blue - Azul
    }
void loop () {
 
  do{
    Color(255 ,0 ,0) ;
        delay(1000);
        Color(0 ,0 ,0) ;
        delay(1000) ;
    successRead = getID();
    lcd.clear();
    lcd.print("ESCANEANDO...");
    digitalWrite(rele, HIGH);
  }
 
  while (!successRead);    //Esperando que haya una comunicación con la tarjeta
 
  if (readCard[0] == myCards[4] && readCard[1] == myCards[5]
  && readCard[2] == myCards[6] && readCard[3] == myCards[7])              //checking for blue card
  {
 
    Serial.println("Bienvenido a casa");
 
   Success();
 
   for(int i = 0; i<1; i++) dummy = readCard[i];   // removing previous stored value from the readCard variable
 
   successRead = 0;
  }else if(readCard[0] == myCards[0] && readCard[1] == myCards[1]
  && readCard[2] == myCards[2] && readCard[3] == myCards[3])            //checking for white card
  {
    Serial.println("Bienvenido al Metropolitano");
 
    Success();
 
    for(int i = 0; i<4; i++) dummy = readCard[i];
  }
  else {
 
    Error();      //calling the error function
  }
}
 
int getID() {
  // Getting ready for Reading PICCs
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return 0;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return 0;
  }
 
  Serial.println("");
  for (int i = 0; i < 4; i++) {  // 
    readCard[i] = mfrc522.uid.uidByte[i];
    Serial.print(readCard[i], HEX);
  }
 
  Serial.println("");
  mfrc522.PICC_HaltA();
  return 1;
 
}
 
void Success(){
 Color(0 ,250 ,0) ;
        delay(1000);
        Color(0 ,0 ,0) ;
        delay(1000) ;
  digitalWrite(rele, LOW);
  lcd.clear();
  lcd.print("BIENVENIDO");
  delay(15000);
 
}
 
void Error(){
  Serial.println("USUARIO NO IDENTIFICADO");
 
    digitalWrite(redLed,HIGH);
    delay(2000);
    digitalWrite(redLed,LOW);
    delay(500);
 
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder