if(access) and else loop.

Hey! I have the following code and for some reason the if(access) and else at the bottom are looping, and I dont know why. Could someone please give me a hand as to why this isn't working?
Any help appreciated!
Using an Arduino Uno with an RC522 module if it helps!

#include <RFID.h>

/*
* Read a card using a mfrc522 reader on your SPI interface
* Pin layout should be as follows (on Arduino Uno):
* MOSI: Pin 11 / ICSP-4
* MISO: Pin 12 / ICSP-1
* SCK: Pin 13 / ISCP-3
* SS/SDA: Pin 10
* RST: Pin 9
*/

#include <SPI.h>
#include <RFID.h>

#define SS_PIN 10
#define RST_PIN 9

RFID rfid(SS_PIN,RST_PIN);


int green = 7;
int red = 8;
int power = 2;
int buzzer = 4; 
int serNum[5];
int schoolcard[][5] = {
  {180,238,165,189,66}
};

int parentcard[][5] = {
  {180,238,165,189,66}
};

int sparecard[][5] = {
  {180,238,165,189,66}
  
};

bool access = false;

void setup(){

    Serial.begin(9600);
    SPI.begin();
    rfid.init();

    pinMode(green, OUTPUT);
    pinMode(red, OUTPUT);
    pinMode(power, OUTPUT);

    digitalWrite(green, LOW);
    digitalWrite(red, LOW);
    digitalWrite(power, LOW);
    
   
}

void loop(){
    
    if(rfid.isCard()){
    
        if(rfid.readCardSerial()){
            Serial.print(rfid.serNum[0]);
            Serial.print(" ");
            Serial.print(rfid.serNum[1]);
            Serial.print(" ");
            Serial.print(rfid.serNum[2]);
            Serial.print(" ");
            Serial.print(rfid.serNum[3]);
            Serial.print(" ");
            Serial.print(rfid.serNum[4]);
            Serial.println("");
            
            for(int x = 0; x < sizeof(schoolcard); x++){
              for(int i = 0; i < sizeof(rfid.serNum); i++ ){
                  if(rfid.serNum[i] != schoolcard[x][i]) {
                      access = false;
                      break;
                  } else {
                      access = true;
                  }
                  
              }
            for(int x = 0; x < sizeof(parentcard); x++){
              for(int i = 0; i < sizeof(rfid.serNum); i++ ){
                  if(rfid.serNum[i] != parentcard[x][i]) {
                      access = false;
                      break;
                  } else {
                      access = true;
                  }
                  
              }
            for(int x = 0; x < sizeof(sparecard); x++){
              for(int i = 0; i < sizeof(rfid.serNum); i++ ){
                  if(rfid.serNum[i] != sparecard[x][i]) {
                      access = false;
                      break;
                  } else {
                      access = true;
                  }
                  
              }
              if(access) break;
            }
           
        }
        
       if(access){
           tone(buzzer, 1000);
           Serial.println("Welcome!");
           digitalWrite(green, HIGH);
           digitalWrite(power, HIGH);
           delay(1000);
           digitalWrite(green, LOW);
           noTone(buzzer);
           delay(2000);
           digitalWrite(power, LOW);
           
      } else {
           tone(buzzer, 500);
           Serial.println("Not allowed!"); 
           digitalWrite(red, HIGH);
           delay(1000);
           digitalWrite(red, LOW);
           noTone(buzzer);     
           
       }        
    }
    
    
    
    rfid.halt();

        }
    }
}

the if(access) and else at the bottom are looping,

if and else statements don't "loop"

Can you please give more details of how the problem manifests itself ?

What do you see if you print the value off access before you test it ? Is it what you expect ?

You are not iterating over your 'cards' correctly...

int schoolcard[][5] = {
  {180, 238, 165, 189, 66}
};
....
      for (int x = 0; x < sizeof(schoolcard); x++) {

You code would seem to indicate you think sizeof(schoolcard) would be 1 but that is not true, it is 10 (int is 2 bytes and you have 5 of them). The typical way to know how many elements you have is

const int numElements = sizeof(schoolcard) / sizeof(schoolcard[0]);

Your 'card' variables should be of type 'byte' (i.e. uint8_t) as well

UKHeliBob:
if and else statements don't "loop"

Can you please give more details of how the problem manifests itself ?

What do you see if you print the value off access before you test it ? Is it what you expect ?

So the problem occurs when I scan my RFID card. I scan the card once and then depending on the card it either repeats the if statement or the else statement without scanning for another card. In the serial monitor, I just get "Welcome!" or "Not allowed!" without printing the card serial number.

Ok, so I managed to make the code not loop, however, only schoolcard will scan. Any ideas as to why?

#include <RFID.h>

/*
* Read a card using a mfrc522 reader on your SPI interface
* Pin layout should be as follows (on Arduino Uno):
* MOSI: Pin 11 / ICSP-4
* MISO: Pin 12 / ICSP-1
* SCK: Pin 13 / ISCP-3
* SS/SDA: Pin 10
* RST: Pin 9
*/

#include <SPI.h>
#include <RFID.h>

#define SS_PIN 10
#define RST_PIN 9

RFID rfid(SS_PIN,RST_PIN);

int green = 7;
int red = 8;
int power = 2;
int buzzer = 4; 
int serNum[5];
int schoolcard[][5] = {
  {180,238,165,189,66}
  
};

int parentcard[][5] = {
  {198,54,161,43,122}
  
};

int sparecard[][5] = {
  {196,224,187,189,34}
  
 
};

bool access = false;

void setup(){

    Serial.begin(9600);
    SPI.begin();
    rfid.init();

    pinMode(green, OUTPUT);
    pinMode(red, OUTPUT);
    pinMode(power, OUTPUT);

    digitalWrite(green, LOW);
    digitalWrite(red, LOW);
    digitalWrite(power, LOW);
    
   
}

void loop(){
    
    if(rfid.isCard()){
    
        if(rfid.readCardSerial()){
            Serial.print(rfid.serNum[0]);
            Serial.print(",");
            Serial.print(rfid.serNum[1]);
            Serial.print(",");
            Serial.print(rfid.serNum[2]);
            Serial.print(",");
            Serial.print(rfid.serNum[3]);
            Serial.print(",");
            Serial.print(rfid.serNum[4]);
            Serial.println("");
            
            for(int x = 0; x < sizeof(schoolcard); x++){
              for(int i = 0; i < sizeof(rfid.serNum); i++ ){
                  if(rfid.serNum[i] != schoolcard[x][i]) {
                      access = false;
                      break;
                  } else {
                      access = true;
                  }
              }
              if(access) break;
            }

            for(int x = 0; x < sizeof(parentcard); x++){
              for(int i = 0; i < sizeof(rfid.serNum); i++ ){
                  if(rfid.serNum[i] != parentcard[x][i]) {
                      access = false;
                      break;
                  } else {
                      access = true;
                  }
              }
              if(access) break;
            }

            for(int x = 0; x < sizeof(sparecard); x++){
              for(int i = 0; i < sizeof(rfid.serNum); i++ ){
                  if(rfid.serNum[i] != sparecard[x][i]) {
                      access = false;
                      break;
                  } else {
                      access = true;
                  }
              }
              if(access) break;
            }
           
        }
        
       if(access){
           tone(buzzer, 1000);
           Serial.println("Welcome!");
           digitalWrite(green, HIGH);
           digitalWrite(power, HIGH);
           delay(1000);
           digitalWrite(green, LOW);
           noTone(buzzer);
           delay(1500);
           digitalWrite(power, LOW);
           
      } else {
           tone(buzzer, 500);
           Serial.println("Unauthorised!"); 
           digitalWrite(red, HIGH);
           delay(1000);
           digitalWrite(red, LOW);
           noTone(buzzer);     
           
       }        
    }
    
    
    
    rfid.halt();

}

Why are the card number arrays 2 dimensional when they hold only 1 level of data ?

Another observation
schoolcard is an array of ints. Each in takes 2 bytes
sizeof(schoolcard)returns the number of bytes used by schoolcard, not the number of entries in it, which is not the same thing

reggaeboi:
Ok, so I managed to make the code not loop, however, only schoolcard will scan. Any ideas as to why?

It should be only 'sparecard' that works. When you compare against 'schoolcard' and find a match you set 'access' to 'true' but then you go on to compare against 'parentcard' and the first mismatch you find you set 'access' to false.
You are still indexing WAY off the end of the arrays of cards. You should create named constants for the number of elements in each list. For example:

int schoolcard[][5] =
{
  {180, 238, 165, 189, 66}
};
const int schoolcards = sizeof schoolcard / sizeof schoolcard[0];
      for (int x = 0; x < schoolcards; x++)
      {

You use this code in three different places. You should make it a function.

        for (int i = 0; i < sizeof(rfid.serNum); i++ )
        {
          if (rfid.serNum[i] != schoolcard[x][i])
          {
            access = false;
            break;
          }
          else
          {
            access = true;
          }
        }

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