I document my experiment code here. So I will find it next time when I need it.
Code
/*
Copyright 2023 Tauno Erik
Started: 07.10.2023
RC522 module
*/
#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>
#include <Adafruit_NeoPixel.h>
// pins for SPI communication.
#define PN532_SCK SCK
#define PN532_MOSI MOSI
#define PN532_SS SS
#define PN532_MISO MISO
#define PN532_IRQ 22
#define PN532_RESET 21
Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
const int RGB_PIN = 2;
const int NUMPIXELS = 6;
Adafruit_NeoPixel pixels(NUMPIXELS, RGB_PIN, NEO_GRB + NEO_KHZ800);
int counter = 0;
uint8_t green_tag[7] = { 0x1D, 0xDE, 0xBC, 0xDC, 0x93, 0x00, 0x00 };
uint8_t red_tag[7] = { 0x1D, 0x26, 0xBD, 0xDC, 0x93, 0x00, 0x00 };
const uint32_t OFF = pixels.Color(0, 0, 0);
const uint32_t GREEN = pixels.Color(0, 150, 0);
const uint32_t BLUE = pixels.Color(0, 0, 150);
const uint32_t RED = pixels.Color(150, 0, 0);
uint32_t pixels_colors[NUMPIXELS] = {OFF};
/*
Compare the two arrays
*/
bool is_equal(uint8_t array1[], uint8_t array2[]) {
bool result = true;
for (uint8_t i = 0; i < sizeof(array1); i++) {
if (array1[i] != array2[i]) {
result = false;
break;
}
}
return result;
}
void add_color(uint32_t new_color) {
// move elemnts to right one step
for (int i = NUMPIXELS; i > 0; i--) {
pixels_colors[i] = pixels_colors[i-1];
}
pixels_colors[0] = new_color;
}
void setup(void) {
Serial.begin(115200);
/*
while (!Serial) {
delay(10);
}
*/
Serial.println("Hello!");
pixels.begin();
pixels.show(); // Turn OFF all pixels ASAP
pixels.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (!versiondata) {
Serial.print("Didn't find PN53x board");
while (1) {
// halt
}
}
// Got ok data, print it out!
Serial.print("Found chip PN5");
Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print("Firmware ver. ");
Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print('.');
Serial.println((versiondata>>8) & 0xFF, DEC);
// Set the max number of retry attempts to read from a card
// This prevents us from waiting forever for a card, which is
// the default behaviour of the PN532.
nfc.setPassiveActivationRetries(0xFF);
Serial.println("Waiting for an ISO14443A card");
}
void loop(void) {
pixels.clear(); // Set all pixel colors to 'off'
boolean success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
// 4 or 7 bytes depending on ISO14443A card type
// ntag 7 bytes
uint8_t uid_length;
// Wait for an ISO14443A type cards (Mifare, etc.). When one is found
// 'uid' will be populated with the UID, and uidLength will indicate
// if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uid_length);
if (counter > NUMPIXELS) {
counter = 0;
}
if (success) {
Serial.println("Found a card!");
Serial.print("UID Length: ");
Serial.print(uid_length, DEC);
Serial.println(" bytes");
Serial.print("UID Value: ");
for (uint8_t i=0; i < uid_length; i++) {
Serial.print(" 0x");
Serial.print(uid[i], HEX);
}
Serial.println("");
if (is_equal(green_tag, uid) && uid_length == 7) {
Serial.println("green tag");
add_color(GREEN);
} else if (is_equal(red_tag, uid) && uid_length == 7) {
Serial.println("red tag");
add_color(RED);
} else {
Serial.println("unknown tag");
add_color(OFF);
}
counter++;
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels_colors[i]);
}
pixels.show();
delay(500); // slowdown
} else {
// PN532 probably timed out waiting for a card
// Serial.println("Timed out waiting for a card");
Serial.println("---");
}
}







































