Hello Everyone,
I have little project to automated my door lock. its RFID Reader plugged to arduino and it opens relay. I manage to find code online to do this. But now I wanna add LEDs and buzzer and push button.
I need activate buzzer when lock is unlocked. (relay not active)
I need push button to open lock for certain amount of time. (activate relay for 10s).
I need Green LED to blink when RFID card is scanned and card accepted and lock is opened , else LED off.
I need Red LED to blink when RFID card is scanned and card is not accepted, else LED stays on.
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 //
#define SS_PIN 10 //
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
String read_rfid;
String ok_rfid_1="9445aba7";
int lock = 7;
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
pinMode(lock, OUTPUT);
}
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(){
digitalWrite(lock, HIGH);
delay(10000);
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) {
Serial.println(ok_rfid_1);
open_lock();
}
}
Do you have this code doing what is required? No point going further if not.
Then try adding one extra bit and get that working, then the next.
We are not here to write your program but are more than happy to help with problems. You have not told us of any problem, just that you want to add more inputs.
thanks for reply. i'm not expecting anyone to write code for me just need some help with making code more simple it's getting confusing. for instance can i make this lines shorter? because later I will add buzzer so it will be more digitalwrite. it does not look right to me. thanks
weedpharma:
You will need to get rid of the delay. Once in the delay nothing else happens. This means you cannot read the card or button or anything else.
Read the first post in this forum to learn how to do several things at once.
Weedpharma
it's do the job on delay relay is open after delay it's go back to lock state but yes when is active it can't read cards