Hello to all,
As the title says, i have the following setup:
Arduino nano -> RFID-RC522 -> 2 relays.
I want to use a single RFID tag to switch on and off relay 1 and then relay 2.
Something like:
touch the rfid tag -> switch relay1 on -> delay 1000ms -> switch relay1 off
touch the SAME rfid tag -> switch relay2 on -> delay 1000ms -> switch relay2 off
My rfid tag key is CB:C3:F1:AB
Can anyone help me with a code/sketch for my problem?
Thank you all very much in advance for any help
@spycatcher2k - i said i need a code to do what i need, as i have explained.
P.S. I have searched on google but i haven't found a code at least similar with what i need. That is why i asked help here. I thought that is someone here who did this already and can help me.
Thank you...
Here is the code i have:
#include "SPI.h"
#include "MFRC522.h"
int LED_PIN = A0;
int LED_PIN1 = A1;
int itsONled[] = {0,0};
#define SS_PIN 10
#define RST_PIN 9
#define SP_PIN 8
#define LED_PIN A0
#define LED_PIN1 A1
MFRC522 rfid(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
boolean pinState;
void setup() {
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
int state; //adaugat
pinMode(LED_PIN, OUTPUT);
pinMode(LED_PIN1, OUTPUT);
digitalWrite(LED_PIN, HIGH);
digitalWrite(LED_PIN1, HIGH);
}
void loop() {
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial())
return;
// Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
// Serial.println(rfid.PICC_GetTypeName(piccType));
// Check is the PICC of Classic MIFARE type
if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println(F("Your tag is not of type MIFARE Classic."));
return;
}
String strID = "";
for (byte i = 0; i < 4; i++) {
strID +=
(rfid.uid.uidByte[i] < 0x10 ? "0" : "") +
String(rfid.uid.uidByte[i], HEX) +
(i!=3 ? ":" : "");
}
strID.toUpperCase();
// LED ติดสว่าง หรือดับได้
Serial.print("Tap card key: ");
Serial.println(strID);
if (strID.indexOf("CB:C3:F1:AB") >= 0) {
unsigned int value = strID.indexOf("CB:C3:F1:AB");
switch(value) {
if(itsONled[A0] == 0) { // usa e incuiata si o descuiem
digitalWrite(LED_PIN, LOW); // descuie usa si opreste releul
delay(1000);
digitalWrite(LED_PIN, HIGH);
itsONled[A0] = 1; // releu oprit si use descuiata
} else { //
digitalWrite(LED_PIN1, LOW); // daca usa e descuiata si o incuiem
delay(1000);
digitalWrite(LED_PIN1, HIGH);
itsONled[A1] = 1; // am incuiat usa si am oprit releul
}
break;
}
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}
}
I is supposed to open relay1 for 1s then close it on first rfid tag read and at the second rfid tag it should open then close relay2 after 1 second.
Why have you done this?
int LED_PIN = A0;
int LED_PIN1 = A1;
.......
#define LED_PIN A0
#define LED_PIN1 A1
That has my head in a spin as to what it will actually do. Do one or the other but not both.
You need to count the number of times the card of the correct RFID card has been presented, then do the switching on the least significant bit ( LSB ) of that count. You return this by using a bitwise and operation:-
lsb = count & 1;
Just figured it out. I have added a LED input and i read it's status.
If the LED is ON when i swipe the RFID tag then the relay 1 will be switched on and then off after 500ms.
If the LED is OFF when i swipe the RFID tag then the relay2 will be switched on and then off after 500ms.
I want to use it into a car to open/close the doors.
The code is (and i am NOT asking money for it like others):
#include "SPI.h"
#include "MFRC522.h"
#define SS_PIN 10
#define RST_PIN 9
#define SP_PIN 8
#define led1 5
#define rel1 A0
#define rel2 A1
MFRC522 rfid(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
boolean pinState;
void setup() {
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
int state; //adaugat
pinMode(rel1, OUTPUT);
pinMode(rel2, OUTPUT);
pinMode(led1, OUTPUT);
digitalWrite(rel1, HIGH);
digitalWrite(rel2, HIGH);
digitalWrite(led1, HIGH);
}
void loop() {
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial())
return;
// Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
// Serial.println(rfid.PICC_GetTypeName(piccType));
// Check is the PICC of Classic MIFARE type
if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println(F("Your tag is not of type MIFARE Classic."));
return;
}
String strID = "";
for (byte i = 0; i < 4; i++) {
strID +=
(rfid.uid.uidByte[i] < 0x10 ? "0" : "") +
String(rfid.uid.uidByte[i], HEX) +
(i!=3 ? ":" : "");
}
strID.toUpperCase();
// LED ติดสว่าง หรือดับได้
Serial.print("Tap card key: ");
Serial.println(strID);
if (strID.indexOf("CB:C3:F1:AB") >= 0) {
if (digitalRead(led1) == HIGH) {
digitalWrite(rel1, LOW);
delay(500);
digitalWrite(rel1, HIGH);
digitalWrite(led1, LOW);
}
// if (digitalRead(led1) == LOW)
else{
digitalWrite(rel2, LOW);
delay(500);
digitalWrite(rel2, HIGH);
digitalWrite(led1, HIGH);
}
}
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}
Enjoy and have a great day all
@spycatcher2k - I asked for help on more places and aprox. 70% of the responses was something like: "if you pay X EUR i will make you the code", but that's not a problem. I am glad i have figured it out and i hope the code will be useful for other people.
Best regards to all