Files
ExploringUncertainties/ExploringUncertanties.ino
T

294 lines
7.6 KiB
Arduino
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//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");
}
/**************************************************************************/
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;
}
UIDResult result1 = UID_NONE;
UIDResult result2 = UID_NONE;
UIDResult result3 = UID_NONE;
/**************************************************************************/
void loop(void) {
delay(50); // Static
//RFID readers
boolean success1;
boolean success2;
boolean success3;
uint8_t uid1[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uid2[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uid3[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength1 = 7; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
uint8_t uidLength2 = 7; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
uint8_t uidLength3 = 7; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
//Reader 01--------------
success1 = getCardID(nfc, uid1, uidLength1);
success2 = getCardID(nfc2, uid2, uidLength2);
success3 = getCardID(nfc3, uid3, uidLength3);
int8_t temperature = 24;
if(result1 == UID_RIGHT){
temperature = temperature - 6;
}else if (result1 == UID_WRONG) {
temperature = temperature + 6;
}
else {
}
if(result2 == UID_RIGHT){
temperature = temperature - 6;
}else if (result2 == UID_WRONG) {
temperature = temperature + 6;
}
else {
}
if(result3 == UID_RIGHT){
temperature = temperature - 6;
}else if (result3 == UID_WRONG) {
temperature = temperature + 6;
}
else {
}
if (success1) {
Serial.println("First reader found a card!");
Serial.print("UID Length: ");Serial.print(uidLength1, DEC);Serial.println(" bytes");
Serial.print("UID Value: ");
for (uint8_t i=0; i < uidLength1; i++)
{
Serial.print(" 0x");Serial.print(uid1[i], HEX);
}
Serial.println("");
result1 = checkUID(uid1, uidLength1);
}
if (success2) {
Serial.println("Second reader found a card!");
Serial.print("UID Length: ");Serial.print(uidLength2, DEC);Serial.println(" bytes");
Serial.print("UID Value: ");
for (uint8_t i=0; i < uidLength2; i++)
{
Serial.print(" 0x");Serial.print(uid2[i], HEX);
}
Serial.println("");
result2 = checkUID(uid2, uidLength2);
}
if (success3) {
Serial.println("Third reader found a card!");
Serial.print("UID Length: ");Serial.print(uidLength3, DEC);Serial.println(" bytes");
Serial.print("UID Value: ");
for (uint8_t i=0; i < uidLength3; i++)
{
Serial.print(" 0x");Serial.print(uid3[i], HEX);
}
Serial.println("");
result3 = checkUID(uid3, uidLength3);
}
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;
if (i < brightEnd) {
scale = BRIGHT_SCALE; // temp-controlled brightness
} else {
scale = (i < 36) ? BRIGHT_SCALE : DIM_SCALE; // original behavior
}
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;
}