Hi,
I am making a project where I use 4 MFC522 card readers to send an OSC command over ethernet. Everything works perfectly as long as the Arduino is connected to the USB cable and Mac. As soon as I connect an external power supply (12v, 2A), the system refuses to start. Anyone have any idea?
- Arduino Uno R3
- Ethernet shield with W5100
- 4x MFC522 card readers
// DEFINES
// Provides debugging information over serial connection if defined
#define DEBUG
// 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>
// Ethernet Library
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <OSCBundle.h>
/////////////////////////ETHERNET INITIALIZE ///////////////////
// you can find this written on the board of some Arduino Ethernets or shields
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
// NOTE: Alternatively, you can assign a fixed IP to configure your
// Ethernet shield.
/////********** SET IP ADDRESSES AND SERVER PORTS **********////////
byte ip[] = { 192, 168, 0, 10 };
int serverPort = 8000; //TouchOSC (incoming port)
int destPort = 53535; //TouchOSC (outgoing port)
IPAddress outIp(192, 168, 0, 108);
//Create UDP message object
EthernetUDP Udp;
/////////////////////////////////////
// CONSTANTS
// The number of RFID readers
const byte numReaders = 4;
// Each reader has a unique Slave Select pin
const byte ssPins[] = {2, 3, 4, 5};
// They'll share the same reset pin
const byte resetPin = 8;
// This pin will be driven LOW to release a lock when puzzle is solved
const byte lockPin = A0;
// The sequence of NFC tag IDs required to solve the puzzle
const String correctIDs[] = {"4776a9af44880", "43b6a9af44880", "416649af44880", "42b689af44880"};
// GLOBALS
// Initialise an array of MFRC522 instances representing each reader
MFRC522 mfrc522[numReaders];
// The tag IDs currently detected by each reader
String currentIDs[numReaders];
/**
* Initialisation
*/
void setup() {
#ifdef DEBUG
// Initialise serial communications channel with the PC
Serial.begin(9600);
#endif
// Avoid conflict with ethernet shield
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);
pinMode(3, OUTPUT);
digitalWrite(3, HIGH);
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
pinMode(5, OUTPUT);
digitalWrite(5, HIGH);
// start the Ethernet connection:
// NOTE: Alternatively, you can assign a fixed IP to configure your
// Ethernet shield.
Ethernet.begin(mac, ip);
// if (Ethernet.begin(ip) == 0) {
// Serial.println("Failed to configure Ethernet using DHCP");
// // no point in carrying on, so do nothing forevermore:
// while (true);
// }
#ifdef DEBUG
// print your local IP address:
Serial.println("Arduino IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println(" ");
Serial.println(F("Serial communication started"));
Serial.println(" ");
#endif
Udp.begin(serverPort);
// Set the lock pin as output and secure the lock
pinMode(lockPin, OUTPUT);
digitalWrite(lockPin, HIGH);
// Initialise the SPI bus
SPI.begin();
cardResetFunction();
}
void cardResetFunction(){
for (uint8_t i=0; i<numReaders; i++) {
mfrc522[i].PCD_Init(ssPins[i], resetPin);
#ifdef DEBUG
// Dump some debug information to the serial monitor
Serial.print(F("Reader #"));
Serial.print(i);
Serial.print(F(" initialised on pin "));
Serial.print(String(ssPins[i]));
Serial.print(F(". Antenna strength: "));
Serial.print(mfrc522[i].PCD_GetAntennaGain());
Serial.print(F(". Version : "));
mfrc522[i].PCD_DumpVersionToSerial();
#endif
// delay before activating next reader
delay(100);
}
#ifdef DEBUG
Serial.println(F("--- END SETUP ---"));
#endif
}
/**
* Main loop
*/
void loop() {
mainloop();
}
void mainloop(){
while(true){
OSCMsgReceive();
// Assume that the puzzle has been solved
boolean puzzleSolved = true;
// Assume that the tags have not changed since last reading
boolean changedValue = false;
// Loop through each reader
for (uint8_t i=0; i<numReaders; i++) {
// Initialise the sensor
mfrc522[i].PCD_Init();
// String to hold the ID detected by each sensor
String readRFID = "";
// If the sensor detects a tag and is able to read it
if(mfrc522[i].PICC_IsNewCardPresent() && mfrc522[i].PICC_ReadCardSerial()) {
// Extract the ID from the tag
readRFID = dump_byte_array(mfrc522[i].uid.uidByte, mfrc522[i].uid.size);
}
// If the current reading is different from the last known reading
if(readRFID != currentIDs[i]){
// Set the flag to show that the puzzle state has changed
changedValue = true;
// Update the stored value for this sensor
currentIDs[i] = readRFID;
}
// If the reading fails to match the correct ID for this sensor
if(currentIDs[i] != correctIDs[i]) {
// The puzzle has not been solved
puzzleSolved = false;
}
// Halt PICC
mfrc522[i].PICC_HaltA();
// Stop encryption on PCD
mfrc522[i].PCD_StopCrypto1();
}
#ifdef DEBUG
// If the changedValue flag has been set, at least one sensor has changed
if(changedValue){
// Dump to serial the current state of all sensors
for (uint8_t i=0; i<numReaders; i++) {
Serial.print(F("Reader #"));
Serial.print(String(i));
Serial.print(F(" on Pin #"));
Serial.print(String((ssPins[i])));
Serial.print(F(" detected tag: "));
Serial.println(currentIDs[i]);
}
Serial.println(F("---"));
}
#endif
// If the puzzleSolved flag is set, all sensors detected the correct ID
if(puzzleSolved){
onSolve();
}
// Add a short delay before next polling sensors
delay(100);
}
}
/**
* Called when correct puzzle solution has been entered
*/
void onSolve(){
#ifdef DEBUG
// Print debugging message
Serial.println(F("Puzzle Solved!"));
#endif
// Release the lock
digitalWrite(lockPin, LOW);
startQlabTrigger("solved");
delay(1000);
while(true) {
OSCMsgReceive();
}
}
/**
* 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;
}
//////// OSC CONTROLS ///////
void OSCMsgReceive() {
OSCMessage msgIN;
int size;
if ((size = Udp.parsePacket()) > 0) {
while (size--)
msgIN.fill(Udp.read());
if (!msgIN.hasError())
{
printMessage(msgIN);
msgIN.route("/lock", lockLock);
msgIN.route("/release", releaseLock);
msgIN.route("/reset", resetpuzzle);
}
}
}
void releaseLock() {
digitalWrite(lockPin, LOW);
}
void lockLock() {
digitalWrite(lockPin, HIGH);
}
void resetpuzzle(){
Serial.println("reset");//print reset to know the program has been reset
for( int i = 0; i < numReaders; ++i ) {
currentIDs[i] = "0000000000000";
Serial.println(currentIDs[i]);
}
//cardResetFunction();
digitalWrite(lockPin, HIGH);
delay(2000);
mainloop();
}
//////////////// SEND OSC MESSAGE ////////////////////////////////
void startQlabTrigger(String trigger) {
String completeaddress = "/cue/" + trigger + "/start" ;
const char * addr = completeaddress.c_str();
Serial.println(completeaddress);
OSCMessage msgOUT(addr);
Udp.beginPacket(outIp, destPort);
msgOUT.send(Udp); // send the bytes
Udp.endPacket(); // mark the end of the OSC Packet
msgOUT.empty(); // free space occupied by message
}
void printMessage(OSCMessage &msg) {
char address[255];
int len = msg.getAddress(address, 0);
for (int i = 0; i < len; i++)
Serial.print(address[i]);
Serial.print(" with data ");
float data = msg.getFloat(0);
Serial.print(data);
Serial.println(" ");
}