Not Quite Normal

CATaLOG | Programming the Arduino

Once I had tested the reader I attempted to port the test code over to the Arduino.  Unfortunately I was unable to talk to the reader reliably using the SoftwareSerial library and so decided to purchase another reader which I knew would work hassle free - a Parallax serial RFID reader.  Once this reader was working it was discovered the previous reader had also been reading tag codes incorrectly.

The setup looks as follows:


Here you can see the Arduino microcontroller on the left which is sitting underneath the ethernet shield, and on the right is the RFID reader.

The programming of the arduino was relatively straightforward once I'd remembered C :)  There were a couple of gotchas though, for anyone attempting something similar.

The RFID reader serial output is linked to digital pin 2 of the Arduino which can be serviced by a hardware interrupt.  This means the CPU doesn't have to keep polling the RFID reader to see if there is a tag there - the reader can simply raise an interrupt to the CPU to let it know when one appears.  To ensure a tag didn't get logged many times as the cat walked past the reader I added a simple debounce routine which logged the time it first saw the tag and then, if subsequent tags were seen sooner than the debounce interval allowed, it would ignore them.

The first problem arose as I was putting this debounce code in the interrupt handler routine  and the AVR documentation states that the timer doesn't increment while in the interrupt handler;  Effectively time stops and the debounce interval never expires.  The solution was to move all non essential code out of the interrupt handle - a good programming strategy anyway, set a globally accessible flag (a volatile) saying a tag had been seen and let the main arduino loop code handle the rest.

The second problem I encountered was the interrupt handler routine not returning to the main program loop after triggering.  After reading up on some more example code I discovered changing the interrupt trigger from CHANGE to LOW solved the problem.

Here is the log output from the Arduino showing it deciding whether or not to act upon seeing a tag:


The same debounce code is also applied to the push buttons which are used to manually set the cats status should they evade the reader somehow - running through an open door for example.

← Return to CATaLOG project page