305 lines
7.5 KiB
Arduino
305 lines
7.5 KiB
Arduino
//multiple RFID readers with SPI
|
||
/**************************************************************************/
|
||
#include <Wire.h>
|
||
#include <SPI.h>
|
||
#include <Adafruit_PN532.h>
|
||
|
||
// If using the breakout with SPI, define the pins for SPI communication.
|
||
#define PN532_SCK (2) //yellow
|
||
#define PN532_MOSI (3) //red
|
||
#define PN532_SS (4) //orange
|
||
#define PN532_MISO (5) //green
|
||
|
||
|
||
enum UIDResult {
|
||
UID_NONE,
|
||
UID_RIGHT,
|
||
UID_WRONG
|
||
};
|
||
|
||
// Use this line for a breakout with a SPI connection:
|
||
Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, 4); //orange
|
||
Adafruit_PN532 nfc2(PN532_SCK, PN532_MISO, PN532_MOSI, 6); //brown
|
||
Adafruit_PN532 nfc3(PN532_SCK, PN532_MISO, PN532_MOSI, 7); //purple
|
||
|
||
//LED strip baseline
|
||
/**************************************************************************/
|
||
#include <FastLED.h>
|
||
|
||
#define LED_PIN 8
|
||
#define NUM_LEDS 60
|
||
#define LED_TYPE WS2812B
|
||
#define COLOR_ORDER GRB
|
||
|
||
CRGB leds[NUM_LEDS];
|
||
|
||
// Base colors (pure RGB, no green component)
|
||
CRGB blue = CRGB(0, 0, 180);
|
||
CRGB red = CRGB(180, 0, 0);
|
||
|
||
// Brightness levels
|
||
const uint8_t DIM_SCALE = 2;
|
||
const uint8_t BRIGHT_SCALE = 100;
|
||
|
||
void setupNFC(Adafruit_PN532 nfc){
|
||
nfc.begin();
|
||
|
||
uint32_t versiondata = nfc.getFirmwareVersion();
|
||
if (! versiondata) {
|
||
Serial.print("Didn't find PN53x board on");
|
||
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");
|
||
|
||
}
|
||
|
||
unsigned long uid1HoldStart = 0; // When reader 1 first sees a card
|
||
const unsigned long UID1_HOLD_MS = 5000; // 5 seconds to trigger win
|
||
bool winAnimationPlayed = false; // To prevent multiple triggers
|
||
|
||
|
||
/**************************************************************************/
|
||
void setup(void) {
|
||
|
||
//LED set-up
|
||
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
|
||
FastLED.setBrightness(255); // Control brightness manually
|
||
|
||
Serial.begin(115200);
|
||
while (!Serial) delay(10); // for Leonardo/Micro/Zero
|
||
Serial.println("Hello!");
|
||
setupNFC(nfc);
|
||
setupNFC(nfc2);
|
||
setupNFC(nfc3);
|
||
}
|
||
|
||
boolean getCardID(Adafruit_PN532 &nfc, uint8_t *uid, uint8_t &uidLength) {
|
||
|
||
// Try to read a card (timeout = 100 ms)
|
||
boolean success = nfc.readPassiveTargetID(
|
||
PN532_MIFARE_ISO14443A,
|
||
uid,
|
||
&uidLength,
|
||
100
|
||
);
|
||
|
||
return success;
|
||
}
|
||
|
||
const unsigned long CARD_TIMEOUT_MS = 2000;
|
||
|
||
unsigned long lastSeen1 = 0;
|
||
unsigned long lastSeen2 = 0;
|
||
unsigned long lastSeen3 = 0;
|
||
|
||
|
||
void loop(void) {
|
||
|
||
delay(50);
|
||
|
||
const int8_t DEFAULT_TEMP = 24;
|
||
int8_t temperature = DEFAULT_TEMP;
|
||
|
||
unsigned long now = millis();
|
||
|
||
uint8_t uid1[7], uid2[7], uid3[7];
|
||
uint8_t uidLength1 = 0, uidLength2 = 0, uidLength3 = 0;
|
||
|
||
// -------- Reader 1 --------
|
||
if (getCardID(nfc, uid1, uidLength1)) {
|
||
lastSeen1 = millis();
|
||
|
||
// Start hold timer if not already started
|
||
if (uid1HoldStart == 0) {
|
||
uid1HoldStart = millis();
|
||
}
|
||
|
||
// Check if card has been held for 5 seconds
|
||
if (!winAnimationPlayed && (millis() - uid1HoldStart >= UID1_HOLD_MS)) {
|
||
Serial.println("WIN Animation Triggered!");
|
||
playWinAnimation(); // Call our function
|
||
winAnimationPlayed = true; // prevent repeat until card is removed
|
||
}
|
||
} else {
|
||
// No card detected, reset hold timer and animation flag
|
||
uid1HoldStart = 0;
|
||
winAnimationPlayed = false;
|
||
}
|
||
|
||
|
||
// -------- Reader 2 --------
|
||
if (getCardID(nfc2, uid2, uidLength2)) {
|
||
lastSeen2 = now;
|
||
}
|
||
|
||
// -------- Reader 3 --------
|
||
if (getCardID(nfc3, uid3, uidLength3)) {
|
||
lastSeen3 = now;
|
||
}
|
||
|
||
// -------- Presence check (2s timeout) --------
|
||
bool reader1Active = (now - lastSeen1) < CARD_TIMEOUT_MS;
|
||
bool reader2Active = (now - lastSeen2) < CARD_TIMEOUT_MS;
|
||
bool reader3Active = (now - lastSeen3) < CARD_TIMEOUT_MS;
|
||
|
||
if (reader1Active) {
|
||
Serial.println("Reader 1 active");
|
||
temperature -= 6;
|
||
}
|
||
|
||
if (reader2Active) {
|
||
Serial.println("Reader 2 active");
|
||
temperature += 6;
|
||
}
|
||
|
||
if (reader3Active) {
|
||
Serial.println("Reader 3 active");
|
||
temperature += 6;
|
||
}
|
||
|
||
temperature = constrain(temperature, 0, NUM_LEDS - 12);
|
||
displayTemp(temperature);
|
||
}
|
||
|
||
|
||
void displayTemp(uint8_t temp) {
|
||
|
||
// Clamp temp so we don’t overflow the strip
|
||
uint8_t maxTempLeds = NUM_LEDS - 12;
|
||
if (temp > maxTempLeds) temp = maxTempLeds;
|
||
|
||
uint16_t brightEnd = 12 + temp;
|
||
|
||
// --------------------------------------------------
|
||
// First 12 LEDs OFF
|
||
for (int i = 0; i < 12; i++) {
|
||
leds[i] = CRGB::Black;
|
||
}
|
||
|
||
// Bottom 6 LEDs solid BLUE
|
||
for (int i = 12; i < 18; i++) {
|
||
CRGB color = blue;
|
||
uint8_t scale = (i < brightEnd) ? BRIGHT_SCALE : DIM_SCALE;
|
||
color.nscale8_video(scale);
|
||
leds[i] = color;
|
||
}
|
||
|
||
// Gradient section (between solid colors)
|
||
for (int i = 18; i < 54; i++) {
|
||
|
||
uint8_t pos = map(i, 18, 53, 0, 255);
|
||
uint8_t eased = ease8InOutQuad(pos);
|
||
CRGB color = blend(blue, red, eased);
|
||
|
||
uint8_t scale = (i < brightEnd) ? BRIGHT_SCALE : DIM_SCALE;
|
||
color.nscale8_video(scale);
|
||
|
||
leds[i] = color;
|
||
}
|
||
|
||
// Top 6 LEDs solid RED
|
||
for (int i = 54; i < NUM_LEDS; i++) {
|
||
CRGB color = red;
|
||
uint8_t scale = (i < brightEnd) ? BRIGHT_SCALE : DIM_SCALE;
|
||
color.nscale8_video(scale);
|
||
leds[i] = color;
|
||
}
|
||
|
||
FastLED.show();
|
||
}
|
||
|
||
|
||
// ---- Known UIDs ----
|
||
|
||
// Right answers (4-byte UIDs)
|
||
const uint8_t RIGHT_UIDS[][4] = {
|
||
{0x4E, 0xBF, 0xCA, 0x06},
|
||
{0xF6, 0x4D, 0x1C, 0x06},
|
||
{0xA3, 0xBC, 0xCA, 0x06},
|
||
{0xC3, 0xDD, 0xCA, 0x06}
|
||
};
|
||
const uint8_t NUM_RIGHT_UIDS = 4;
|
||
|
||
// Wrong answers (7-byte UIDs)
|
||
const uint8_t WRONG_UIDS[][7] = {
|
||
{0x04, 0x97, 0xCF, 0x1F, 0x3F, 0x61, 0x80},
|
||
{0x04, 0x7C, 0xA9, 0x00, 0xA0, 0x61, 0x80},
|
||
{0x04, 0x6D, 0xC2, 0x2E, 0x5F, 0x61, 0x80},
|
||
{0x04, 0x22, 0x85, 0xFF, 0x9F, 0x61, 0x80}
|
||
};
|
||
const uint8_t NUM_WRONG_UIDS = 4;
|
||
|
||
|
||
bool compareUID(const uint8_t *uidA, const uint8_t *uidB, uint8_t length) {
|
||
for (uint8_t i = 0; i < length; i++) {
|
||
if (uidA[i] != uidB[i]) {
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
UIDResult checkUID(const uint8_t *uid, uint8_t uidLength) {
|
||
|
||
// Check RIGHT UIDs (4 bytes)
|
||
if (uidLength == 4) {
|
||
for (uint8_t i = 0; i < NUM_RIGHT_UIDS; i++) {
|
||
if (compareUID(uid, RIGHT_UIDS[i], 4)) {
|
||
Serial.println("correct");
|
||
return UID_RIGHT;
|
||
}
|
||
}
|
||
}
|
||
|
||
// Check WRONG UIDs (7 bytes)
|
||
if (uidLength == 7) {
|
||
for (uint8_t i = 0; i < NUM_WRONG_UIDS; i++) {
|
||
if (compareUID(uid, WRONG_UIDS[i], 7)) {
|
||
Serial.println("wrong");
|
||
return UID_WRONG;
|
||
}
|
||
}
|
||
}
|
||
Serial.println("unknown");
|
||
return UID_NONE;
|
||
}
|
||
|
||
void playWinAnimation() {
|
||
const int waitMs = 10; // delay between each LED step
|
||
static uint8_t hue = 0; // hue counter
|
||
|
||
// Forward sweep
|
||
for (int i = 0; i < NUM_LEDS; i++) {
|
||
leds[i] = CHSV(hue++, 255, 255); // light up the LED with current hue
|
||
FastLED.show();
|
||
fadeAllSweep(); // fade all LEDs slightly
|
||
delay(waitMs);
|
||
}
|
||
|
||
// Backward sweep
|
||
for (int i = NUM_LEDS - 1; i >= 0; i--) {
|
||
leds[i] = CHSV(hue++, 255, 255); // light up the LED with current hue
|
||
FastLED.show();
|
||
fadeAllSweep(); // fade all LEDs slightly
|
||
delay(waitMs);
|
||
}
|
||
}
|
||
|
||
// exactly like your previous 'fadeall()'
|
||
void fadeAllSweep() {
|
||
for (int i = 0; i < NUM_LEDS; i++) {
|
||
leds[i].nscale8(250); // dim LEDs gradually
|
||
}
|
||
}
|
||
|