Evening,
I'm having a bit of trouble using an RFID-RC522 sensor in my project. The sensor works fine when my Arduino is powered from my laptop, or from a USB 5V power supply, but when I try and use a generic 12v, 1A power supply I've used in countless other projects, I get very very odd behaviour.
The intended way for the system to work, is that 5 different RFID tags are to be passed over the sensor in order, and as they are some RGB Leds light up. This works fine while on a 5V supply, but at 12V I find that the ardunio hangs/crashes/becomes unresponsive, always consistently 3-4 seconds after I scan the 3rd RFID tag. Once in this state I can't get the arduino to recover by pressing he rest button on it, so have to cycle the power.
I've used this same equipment before in previous projects, that used multiple RFID sensors over SPI and were more about placing a specific tag on a specific sensor, and those ran fine off of my 12V 1A supply, so I really don't know what is happening. Any insight would be great.
/*
* "LUMOS" Puzzle
*
* This puzzle requires the player to place 5 runes that represnt the
* letters of lumos in the correct sequence ontop of a sensor, at which
* point the RFID tag embedded in the rune is detected by a sensor.
* When all 5 runes have be placed in the right sequence, the puzzle is solved.
*/
// LIBRARIES
// Standard SPI library comes bundled with the Arduino IDE
#include <SPI.h>
// Download and install from https://github.com/miguelbalboa/rfid
#include <MFRC522.h>
//library for controling adressable RGB LED strips via PWM
#include <FastLED.h>
// CONSTANTS
// Reader's Slave Select pin
const byte ssPin = 10;
// Reset pin
const byte resetPin = 9;
// The sequence of NFC tag IDs required to solve the puzzle
const String correctIDs[] = {"704a6d59","30af6d59","10d16d59", "0336e59", "705a6e59"};
// The number of LEDs in the Strip
#define NUM_LEDS 25
// The data pin for the RBG LEDS
#define DATA_PIN 6
// Define the array of leds
CRGB leds[NUM_LEDS];
// GLOBALS
// Initialise a MFRC522 instance representing the reader
MFRC522 mfrc522;
// The tag ID currently detected by the reader
String currentID;
// Running total of how many letters have been solved
int score = 0;
//sawtooth array
int saw[20] = {-2,-1,0,1,2,3,4,5,4,3,2,1,0,-1,-2,-3};
//places in saw array
int sawCount1 = 0;
int sawCount2 = 9;
int sawCount3 = 3;
int sawCount4 = 12;
int sawCount5 = 6;
void setup() {
// Initialise serial communications channel with the PC
Serial.begin(9600);
Serial.println(F("Serial communication started"));
// Set the lock pin as output and secure the lock
pinMode(3, OUTPUT);
digitalWrite(3,LOW);
// Initialise the SPI bus
SPI.begin();
// Initialise the reader
mfrc522.PCD_Init(ssPin, resetPin);
// Dump some debug information to the serial monitor
Serial.print(F("Reader #1"));
Serial.print(F(" initialised on pin "));
Serial.print(String(ssPin));
Serial.print(F(". Antenna strength: "));
Serial.print(mfrc522.PCD_GetAntennaGain());
Serial.print(F(". Version : "));
mfrc522.PCD_DumpVersionToSerial();
delay(100);
FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS); // GRB ordering is typical
mfrc522.PCD_Init();
Serial.println(F("--- END SETUP ---"));
}
/**
* Main loop
*/
void loop() {
// Assume that the puzzle has not been solved
boolean puzzleSolved = false;
// Assume that the tags have not changed since last reading
boolean changedValue = false;
// String to hold the ID detected by the sensor
String readRFID = "";
// If the sensor detects a tag and is able to read it
if(mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial())
{
// Extract the ID from the tag
readRFID = dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
}
// If the current reading is different from the last known reading
if(readRFID != currentID)
{
// Set the flag to show that the puzzle state has changed
changedValue = true;
// Update the stored value for the sensor
currentID = readRFID;
}
// If the changedValue flag has been set the sensor has changed
if(changedValue)
{
// Dump to serial the current state of the sensor
Serial.print(F("Reader #1"));
Serial.print(F(" on Pin #"));
Serial.print(String(ssPin));
Serial.print(F(" detected tag: "));
Serial.println(currentID);
Serial.println(F("---"));
if (currentID == correctIDs[score])
{
Serial.println("correct rune!, please enter next rune");
score++;
if (score == 5)
{
puzzleSolved = true;
}
}
else if (currentID != NULL && currentID != correctIDs[score - 1])
{
Serial.println("invalid rune, reseting");
score = 0;
}
}
// If the puzzleSolved flag is set, all the tags have been detected
if(puzzleSolved)
{
Serial.print("Puzzle solved!");
digitalWrite(3,HIGH); //release the maglock
while(1)
{
twinkle(); //twinkle forever
}
}
twinkle(); //give the current lit letters a twinkle
//delay(100);
}
/**
* Helper function to return a string ID from byte array
*/
String dump_byte_array(byte *buffer, byte bufferSize) {
String read_rfid = "";
for (byte i=0; i<bufferSize; i++) {
read_rfid = read_rfid + String(buffer[i], HEX);
}
return read_rfid;
}
//function to light letters that have been solved, and then twinkle them for a set period of time
void twinkle()
{
for (int i = 0; i < 25; ++i) // blank LED values first incase of resets
{
leds[i] = 0;
}
for (int i = 0 ; i < 5 ; i++)
{
if (score >= 1)
{
leds[i] = CHSV(saw[sawCount1]+89, 255, (saw[sawCount5]*10)+200);
}
if (score >= 2)
{
leds[i+5] = CHSV(saw[sawCount2]+89, 255, (saw[sawCount3]*10)+200);
}
if (score >= 3)
{
leds[i+10] = CHSV(saw[sawCount3]+89, 255, (saw[sawCount2]*10)+200);
}
if (score >= 4)
{
leds[i+15] = CHSV(saw[sawCount4]+89, 255, (saw[sawCount1]*10)+200);
}
if (score >= 5)
{
leds[i+20] = CHSV(saw[sawCount5]+89, 255, (saw[sawCount4]*10)+200);
}
}
sawCount1++;
sawCount2++;
sawCount3++;
sawCount4++;
sawCount5++;
if (sawCount1 >= 16) { sawCount1 = 0; }
if (sawCount2 >= 16) { sawCount2 = 0; }
if (sawCount3 >= 16) { sawCount3 = 0; }
if (sawCount4 >= 16) { sawCount4 = 0; }
if (sawCount5 >= 16) { sawCount5 = 0; }
FastLED.show();
delay(100);
}