RFID Light Switch

I'm trying to make a simple RFID light switch with a UNO and a cheep Funduino RC522. I want to be able to scan a tag to turn the lights on then scan it again to turn them off and i simply just can't figure the code out. I got it working good with a delay but thats not what I want. (relay in pin 7) and RFID reader in the common RC522 pins.
Currently it only turns on when scanned.

Thank you!

#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 red_led         6
MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance
#define RELAY           4
String read_rfid;
String ok_rfid_1="RFID";
String ok_rfid_2="DDFC333B";
 int lock = 7; // lock = light
 int mylight = LOW;
 
 
void setup() {
	{    
// Initialise the Arduino data pins for OUTPUT
  pinMode(RELAY, OUTPUT); 
	}
	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(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); 
  
}
void close_lock()  {
  
  digitalWrite(lock,LOW);
}

void loop() {
	// Look for new cards
	if ( ! mfrc522.PICC_IsNewCardPresent()) {
		return;
	}

	// Select one of the cards                           
	if ( ! mfrc522.PICC_ReadCardSerial()) {
		return;
	}
     Serial.println(read_rfid);
     if (read_rfid==ok_rfid_1); {
    //ok, open the door.
    Serial.println(ok_rfid_1);              //I think this is my prblem area
    if (lock == LOW);
    open_lock();
     }
 Serial.println(read_rfid);       
     if (read_rfid==ok_rfid_1); {     
    //ok, close the door.
    Serial.println(ok_rfid_1);
    if (lock == HIGH) 
    close_lock();
    
     }
// if (not_ok);
 //do nothing;
}
	//  if not okay
    //do nothing...
    //}
	// Dump debug info about the card; PICC_HaltA() is automatically called
	//mfrc522.PICC_DumpToSerial(&(mfrc522.uid));

light.ino (1.84 KB)

You'll get more answers when you include your code in immediately readable form, using code tags (click </>).

Thank You!

The turn on and turn off of the lock is triggered by the same conditions. Include the state of the lock variable in the if statement.

  Serial.println(read_rfid);
     if (read_rfid==ok_rfid_1 && lock == HIGH) {
    //ok, open the door.
    Serial.println(ok_rfid_1);              //I think this is my prblem area
    if (lock == LOW);
    open_lock();
     }
 Serial.println(read_rfid);       
     if (read_rfid==ok_rfid_1 && lock == LOW) {     
    //ok, close the door.
    Serial.println(ok_rfid_1);
    if (lock == HIGH)
    close_lock();
   
     }
     Serial.println(read_rfid);
     if (read_rfid==ok_rfid_1); {
    //ok, open the door.
    Serial.println(ok_rfid_1);              //I think this is my prblem area
    if (lock == LOW);
    open_lock();
     }

Some problems:

How do you fill read_rfid with information?

Remove the ';' from your if's, they will make the if inoperative.

To toggle the lock, use

if (lock==LOW)
  open_lock();
else
  close_lock();

Thank you so much for both of your help but I can't seem to get either "advice" to "work".
With the original code the light would turn on and stay on but now it won't react at all.
I'm probably doing something silly but again thank you so much for your help!

void loop() {
	// Look for new cards
	if ( ! mfrc522.PICC_IsNewCardPresent()) {
		return;
	}

	// Select one of the cards                           
	if ( ! mfrc522.PICC_ReadCardSerial()) {
		return;
	}
     

  Serial.println(read_rfid);
     if (read_rfid==ok_rfid_1 && lock == HIGH) {
    //ok, open the door.
    Serial.println(ok_rfid_1);              //I think this is my prblem area
    if (lock == LOW);
    open_lock();
     }
 Serial.println(read_rfid);       
     if (read_rfid==ok_rfid_1 && lock == LOW) {     
    //ok, close the door.
    Serial.println(ok_rfid_1);
    if (lock == HIGH)
    close_lock();
   
     }

}

and this..

void loop() {
	// Look for new cards
	if ( ! mfrc522.PICC_IsNewCardPresent()) {
		return;
	}

	// Select one of the cards                           
	if ( ! mfrc522.PICC_ReadCardSerial()) {
		return;
	}
     

  Serial.println(read_rfid);
     if (read_rfid==ok_rfid_1 && lock == HIGH) {
    //ok, open the door.
    Serial.println(ok_rfid_1);              //I think this is my prblem area
    if (lock == LOW);
    open_lock();
     }
 Serial.println(read_rfid);       
     if (read_rfid==ok_rfid_1 && lock == LOW) {     
    //ok, close the door.
    Serial.println(ok_rfid_1);
    if (lock == HIGH)
    close_lock();
   
     }
     if (lock==LOW)
  open_lock();
else
  close_lock();
}

I'm probably doing something silly

What is your printout telling you? Add more printout statements to see where the program is getting to.

Nothing in printing out in the serial monitor.

The add some print statements to the setup function to check that it is working.
Add MORE print statements in the code to see where the code gets to.
Post the code of this and tell us what happens.

Where did you get that code from, it looks most odd.

I'm sorry I don't know how to add some print statements.
I'm really new to coding so this code is from a lot of copying and pasting.

I'm sorry I don't know how to add some print statements.

Serial.print("The code is here");

I put that at the end of the loop and it printed "The code is here" over and over again.

void loop() {
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }

  // Select one of the cards                           
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return;
  }
     

  Serial.println(read_rfid);
     if (read_rfid==ok_rfid_1 && lock == HIGH) {
    //ok, open the door.
    Serial.println(ok_rfid_1);              //I think this is my problem area
    if (lock == LOW)
    open_lock();
     }
 Serial.println(read_rfid);       
     if (read_rfid==ok_rfid_1 && lock == LOW) {     
    //ok, close the door.
    Serial.println(ok_rfid_1);
    if (lock == HIGH)
    close_lock();
   
     }
    Serial.print("The code is here");


}

Sorry I don't think you are ready for this project. You should learn some code first. Try some tutorials, and try to understand how code works. Make some modification and get them working.

Come back to this project in about a month and maybe you will have more of an idea about how code works.

Got it to work! 8)
So much for not being ready
Thanks for the help!

houstonm:
Got it to work! 8)
So much for not being ready
Thanks for the help!

Houtonm would you mind posting your sketch here on the forum?

Would be a big help.

Hello bro.. can u send me the codes and the diagram fke it ?