Togling a value with RFID-card [SOLVED]

Hi,
I am trying to enable/disable the operating panel of an instrument, with the aid of a RFID-card.
Undermentioned program is a tryout and shall be integrated i a larger program.

The problem is that my parameter “blnOperation” changes several times after each. It looks like bouncing. I want to toggle between true or false.
I don’t prefer to use the delay-function, because it disturbs my other (extended) program.

Who as a tip?

/*
WHAT DOES THIS PROGRAM:
- It generates a Lock-signal (true or false) by striking a RFID-card along a RFID-sensor

HARDWARE:
- Adrduino Nano V3
- RFID-sensor
RFID___Arduino Nani
SDA    D10
SCK    D13
MOSI   D11  
MISO   D12
IRQ    not connected
GND    GND
RST    D9
3,3 V  3,3 V

TESTED ON:
IDE 1.8.13 and Arduino Nano V3

 */
//========================================================================
//Libraries
#include <SPI.h>
#include <MFRC522.h>
 
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

bool blnOperation;
bool lastButtonState;
bool currentButtonState;

//========================================================================
void setup() 
{
  Serial.begin(9600);   // Initiate a serial communication
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  Serial.println("Approximate your card to the reader...");
  Serial.println();  
  currentButtonState = LOW;
}

//========================================================================
void loop() 
{
   //delay(200);
   lastButtonState    = currentButtonState;  // save the last state
   currentButtonState = Check_RFID();        // read new state
   
   //Serial.println(String(lastButtonState) + "   " + currentButtonState);
   if(lastButtonState == HIGH && currentButtonState == LOW) {
      blnOperation= !blnOperation;
      Serial.println(String("blnOperation = ") + blnOperation);
      if (blnOperation == true){Serial.println ("Vrijgegeven");} else {Serial.println("Locked");}
      }  //End change buttonState
    
} 


//==============================================================
bool Check_RFID(){

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

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {return false; return;}
  
 //Show UID on serial monitor
  //Serial.print("UID tag :");
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++) 
     {
     //Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     //Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
     }
  
  content.toUpperCase();
  if (content.substring(1) == ("3C 95 45 49") or ("D3 CC 8C 16")) //change here the UID of the card/cards that you want to give access
    {Serial.println("Authorized access");
    return true;
    Serial.println("Access");
    }
    else
    {return false;
    Serial.println("Locked"); }
    
    //delay(100);
    mfrc522.PICC_HaltA();
    
}  //End Check_RFID

I am surprised that the compiler doesn't warn you about unreachable code - this will never be executed:

  mfrc522.PICC_HaltA();

Similarly, what's going on here?

    return false;
    return;
mfrc522.PICC_HaltA();

This is the code to stop reading (it a part of MFRC522-1.4.8.zip)
With "return false" I return false to the function.
With "return" I stop and exit the function.

Not exactly. Return false exits the function, which is why the return after it is unnecessary.

The way this line of code is written it will always be true:

  if (content.substring(1) == ("3C 95 45 49") or ("D3 CC 8C 16"))

I believe what you intended is:

  if (content.substring(1) == "3C 95 45 49" or content.substring(1) == "D3 CC 8C 16")

Note that the extra parens are not necessary.

Besides my previous reply regarding the conditional, note that the Serial.println("Access"); and Serial.println("Locked"); lines will never be executed because they follow a return statement.

  if (content.substring(1) == ("3C 95 45 49") or ("D3 CC 8C 16")) //change here the UID of the card/cards that you want to give access
    {Serial.println("Authorized access");
    return true;
    Serial.println("Access");
    }
    else
    {return false;
    Serial.println("Locked"); }

Thanx WildBill and ToddL1962 for your comments; I changed the code.

The function Check_RFID() seems to be executed a number of times.
I think to make a timeslot to prohibit that the function Check_RFID() will be executed for a second time in a short while (in stead of using DELAY).

content.toUpperCase();
  if (content.substring(1) == "3C 95 45 49" or content.substring(1) == "D3 CC 8C 16") 
    {Serial.println("Authorized access"); return true; }
    else
    {Serial.println("Not authorized"); return false; }

I found a solution to execute the subroutine Check_RFID() only once within a loop with the aid of blnRFIDenabled.
After 1 second it is possible to read the RFID again.

After calling Check_RFID() I set blnRFIDenabled to false.
I use a 20 Hz timer, set the counts (cnt2) to 20 and count back to zero (i.e. 1 second); when the second is passed blnRFIDenabled is set to true.

void loop() { 

//disables pushbuttons by setting blnOperation ON and OFF
   if (blnRFIDenabled ==true) {
      intButtonState = Check_RFID();      //Read new state
      if(intButtonState==1){
          cnt2= 20;               //Counts back until zero
          blnRFIDenabled = false;
          blnOperation= !blnOperation;
          if (blnOperation == true){Serial.println ("Vrijgegeven");} else {Serial.println("Locked");}
          }  
      }   //End try to read RFID
      //And so on

The timer looks like

//Interrupt every 50 ms (20 Hz) with Timer1
ISR(TIMER1_COMPA_vect){
  cnt1++;                     //Increment 50 ms counter
  if (cnt1 == 80) {cnt1 = 1;}  //runs from 1 to 80
  cnt2--;
  if (cnt2 <= 0) {cnt2 = 0; blnRFIDenabled = true;}  //runs from 20 to 0
  }    //End interrupt service request

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.