Hi guys im trying to make a secret compartment using an rfid and a relay module, i have an objegt wich ill be tagging with an rfid tag or card, what i want to do is basically this: get the object near the reader and the relay activates, opens up the compartment and keeps on high lighting it untill i remove the object , so, i have come up to this code at this point, but after five minutes the relay clics to low and them high in a millisecond, what i need it to do is that if the rfid reader its reading the code the relay should be on high untill i remove the object from its place, i hope you get me im no engineer anonly have the basic knowledge on arduino, i hope one of you guys can help me out.
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
#define CH1 2
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
String read_rfid;
String ok_rfid_1="5665367";
//String ok_rfid_2="ffffffff"; //add as many as you need.
int lock=3; //Which pin the lock will be on if using a relay or solenoid or similar
/*
* Initialize.
*/
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
//Choose which lock below:
pinMode(lock, OUTPUT);
pinMode(CH1, OUTPUT);
}
/*
* Helper routine to dump a byte array as hex values to Serial.
*/
void dump_byte_array(byte *buffer, byte bufferSize) {
read_rfid="";
for (byte i = 0; i < bufferSize; i++) {
read_rfid=read_rfid + String(buffer[i], HEX);
}
}
void open_lock() {
//Use this routine when working with Relays and Solenoids etc.
digitalWrite(lock, HIGH);
delay(6000);
digitalWrite(lock,LOW);
}
void loop() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
return;
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
return;
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println(read_rfid);
if (read_rfid==ok_rfid_1) {
//ok, open the door.
open_lock();
}
//Add below as many "keys" as you want
//if (read_rfid==ok_rfid_2) {
//also ok, open the door
// open_lock();
//}
}
You have to edit your code and insert code tags. Some of your code e.g. [ i ] is being misinterpreted and has got lost.
The UPPERCASE is horrible to read. Use this tool to convert to sentence case: http://caseconverter.com/
If you are looking for strange behaviour after 5 Minutes, this could explain it:
Im sorry for the uppercase, no im trying to keep it open as long as the object is close to the reader, as soon as i remove it, i want it to be on low, that sketch i made so after five minutes reads the card again, but i need it to go on low if there isnt a card or tag there, not to blink, yeah i know my coding is bad, but im just learning how to, i hope you can help me out, i also appreciate your time
Here is the sentence case version of your text (automatically translated) . . .
Hi guys im trying to make a secret compartment using an rfid and a relay module, i have an objegt wich ill be tagging with an rfid tag or card, what i want to do is basically this: get the object near the reader and the relay activates, opens up the compartment and keeps on high lighting it untill i remove the object , so, i have come up to this code at this point, but after five minutes the relay clics to low and them high in a millisecond, what i need it to do is that if the rfid reader its reading the code the relay should be on high untill i remove the object from its place, i hope you get me im no engineer anonly have the basic knowledge on arduino, i hope one of you guys can help me out.
Can you post your code between code tags because it has been it has been damaged with missing subscripts and unwanted italic text.
thanks for your help, i have just re done my original post
that didnt worked out what i need to do is to turn the relay to low when the rfid tag isnt present
rfid tag on reader_relay, high
rfid tag not on reader_relay, low
it seems really simple in my mind, but its harder than i thought
You can try this and see how far you get.
I am assuming that on each pass of the loop, (read_rfid == ok_rfid_1) is true if the tag is present and false if not.
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
#define CH1 2
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
String read_rfid;
String ok_rfid_1 = "5665367";
//String ok_rfid_2="ffffffff"; //add as many as you need.
int lock = 3; //Which pin the lock will be on if using a relay or solenoid or similar
boolean isOpen ;
/*
Initialize.
*/
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
//Choose which lock below:
pinMode(lock, OUTPUT);
pinMode(CH1, OUTPUT);
}
/*
Helper routine to dump a byte array as hex values to Serial.
*/
void dump_byte_array(byte *buffer, byte bufferSize) {
read_rfid = "";
for (byte i = 0; i < bufferSize; i++) {
read_rfid = read_rfid + String(buffer[i], HEX);
}
}
void open_lock() {
//Use this routine when working with Relays and Solenoids etc.
digitalWrite(lock, HIGH);
// delay(6000);
// digitalWrite(lock,LOW);
}
void close_lock() {
digitalWrite(lock, LOW);
}
void loop() {
isOpen = false ; // assume locked
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
return;
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
return;
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println(read_rfid);
if (read_rfid == ok_rfid_1) {
//ok, open the door.
isOpen = true ;
}
//Add below as many "keys" as you want
//if (read_rfid==ok_rfid_2) {
//also ok, open the door
// open_lock();
//}
(isOpen) ? ( open_lock() ) : ( close_lock() ) ;
}
6v6gt:
You have to edit your code and insert code tags. Some of your code e.g. [ i ] is being misinterpreted and has got lost.
The UPPERCASE is horrible to read. Use this tool to convert to sentence case: http://caseconverter.com/