Strange behavior of funtions and serial connections

I am running the sketch, which I attached, on an Arduino Leonardo. It reads the ID of RFID cards from an antenna via Serial1 and compares them to the IDs saved on an external EEPROM ( connected via I²C.
The problem is that when I call the getCards() function manually, that is by sending ‘c’ via the Serial connection, it works just fine, but when it’s called by the function which is invoked after an RFID card enters the antenna’s field, the Serial connection to my PC freezes as the first address should be printd, even though I can’t see a big difference considering the arguments passed.

The sketch on the Leonardo, meanwhile, seems to keep on running, but when I scan the card once more, only zeroes are received by Serial1. It has already worked perfectly eralier, but I can’t remember having applied any changes to most of the already existing functions. What am I looking over the head?

If I can provide any additional information, let me know. Thanks in advance!

RFID_EEPROM.ino (7.54 KB)

I got this far:

  if (!Serial.available())
  }else if (Serial.available()){

Serial.available() does not return a boolean. The value that it returns should not be used that way. Even if you decide to do that, what else could you determine other than that there is, or is not, data to be read? If there is not data, then there must be data. There are NO other choices, and it is not necessary to test again.


Thanks for your reply, but I doubt that's the issue. It is right that the double checking is unnecessary, I'm going to remove that. But the !Serial.available() is just another way to check whether there's no data available, because as far as I know false is equivalent to 0.
Nevertheless I've changed your code according to your suggestions, but sadly, it did not resolve the issue. Probably it is some assignment error concerning some array, but I can't find a difference between the two scenarios where the function is called. Maybe it is very obvious and I just cannot manage to find it.


Would you suggest that I insert more line feeds in the future or shorter function names or did I misunderstand you there?

Would you suggest that I insert more line feeds in the future or shorter function names or did I misunderstand you there?

White space of some kind makes code more readable. Personally, I think every curly brace belongs on its own line.

Okay, I will keep that in mind. But do you have an idea how it is possible, that the Serial transmission freezes mid-line if the programm wants the list to check for a certain ID, but works fine over and over again if containing function is called in the loop()?

I wonder if it is wise to create this as a local variable due to its total size - almost half the available SRAM

    case 'c':
        byte buffer[255][5];

I don't know how the compiler deals with that sort of thing in successive calls to a function and I would create it as a global variable

I also wonder if you may be running out of memory.


Thank you for answer.
I also think that this is the right approach to the problem, though in this case, if I send a 'c' to the Leonardo, it works. I have already tried earlier to reduce the size of those variables, a few times it helped. But I'm gonna try out your advice on creating it globally

Okay, I’ve done what you recommended. The result was, that my Leonardo quit crashing but produced address errors acessing the I²C bus, until I noticed that I meanwhile stupidly had pulled the clock and data line out of the breadbord.
So, you were correct, problem solved. Thanks a lot!!