Freetronics Cube + NFC Tag + Arduino Leonardo

I bought RFID-RC522 NFC reader online. You can get them from Ebay or DX for <$10. The board is based on NXP  MFRC522  and has on pcb RFID antenna. Arduino website has online material on how to interface MFRC522 to Arduino board.

In this setup, I connected the Leonardo board to RC522 board to read the NFC tag id. The Leonardo board would then verify if the tag is a known id and if so, it will forward the colour information to the Freetronics Cube to display the colour on LEDs.

Below is the Arduino Leonardo code.

#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9
#define RED_PIN 5
#define GREEN_PIN 6
#define BLUE_PIN 7

MFRC522 mfrc522(SS_PIN, RST_PIN);	

// Edit the Tag IDs below to suit your NFC tag.
byte redTag[] = {0x3D, 0x55, 0x64, 0x1A};
byte blueTag[] = {0xEA, 0xED, 0xEF, 0xE5};
byte greenTag[] = {0x8D, 0x08, 0x67, 0x1A};
byte rainbowTag[] = {0xD4, 0x31, 0x71, 0x1A};

void setup() {
	Serial.begin(9600);	
	SPI.begin();			
	mfrc522.PCD_Init();	
	Serial.println("Scan PICC to see UID and type...");
        pinMode(RED_PIN, OUTPUT);
        pinMode(GREEN_PIN, OUTPUT);
        pinMode(BLUE_PIN, OUTPUT);
}

void loop() {
	// Look for new cards
	if ( ! mfrc522.PICC_IsNewCardPresent()) {
		return;
	}

	// Select one of the cards
	if ( ! mfrc522.PICC_ReadCardSerial()) {
		return;
	}
       
        if((mfrc522.uid.uidByte[0] == redTag[0]) && 
          (mfrc522.uid.uidByte[1] == redTag[1]) && 
          (mfrc522.uid.uidByte[2] == redTag[2]) && 
          (mfrc522.uid.uidByte[3] == redTag[3]))
          {
            Serial.println("Red");
            digitalWrite(RED_PIN, 1);
            digitalWrite(GREEN_PIN, 0);
            digitalWrite(BLUE_PIN, 0);
          }
        else if((mfrc522.uid.uidByte[0] == blueTag[0]) && 
          (mfrc522.uid.uidByte[1] == blueTag[1]) && 
          (mfrc522.uid.uidByte[2] == blueTag[2]) && 
          (mfrc522.uid.uidByte[3] == blueTag[3]))
          {
            Serial.println("Blue");
            digitalWrite(RED_PIN, 0);
            digitalWrite(GREEN_PIN, 0);
            digitalWrite(BLUE_PIN, 1);
          }
        else if((mfrc522.uid.uidByte[0] == greenTag[0]) && 
          (mfrc522.uid.uidByte[1] == greenTag[1]) && 
          (mfrc522.uid.uidByte[2] == greenTag[2]) && 
          (mfrc522.uid.uidByte[3] == greenTag[3]))
          {
            Serial.println("Green");
            digitalWrite(RED_PIN, 0);
            digitalWrite(GREEN_PIN, 1);
            digitalWrite(BLUE_PIN, 0);
          }
         else if((mfrc522.uid.uidByte[0]== rainbowTag[0]) && 
          (mfrc522.uid.uidByte[1] == rainbowTag[1]) && 
          (mfrc522.uid.uidByte[2] == rainbowTag[2]) && 
          (mfrc522.uid.uidByte[3] == rainbowTag[3]))
          {
            Serial.println("Rainbow");
            digitalWrite(RED_PIN, 0);
            digitalWrite(GREEN_PIN, 0);
            digitalWrite(BLUE_PIN, 0);
          }
         else
         {
           mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
         }
      delay(200);
}

Below is the code for the Freetronics Cube.

#include <SPI.h>
#include <Cube.h>

rgb_t colours[3] = {
  RED, GREEN, BLUE};

int hueValue;
Cube cube;

void setup(void) {
  cube.begin(0, 115200); 
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);  
}

byte communicatedData;
rgb_t colourToFill;

void loop() {
  for (byte filled = 0 ; filled < 64 ; ) {
    char x = random(4);
    char y = random(4);
    char z = random(4);
    rgb_t ledvalue = led[x][y][z];
    communicatedData = (digitalRead(A2)<<2) + (digitalRead(A1) <<1) + digitalRead(A0);
    Serial.println(communicatedData);
    delay(200);
    switch(communicatedData) {
    case 1:
      colourToFill = RGB(0xff, 0x00, 0x00);
      break;
    case 2:
      colourToFill = RGB(0x00, 0xff, 0x00);
      break;
    case 4:
      colourToFill = RGB(0x40, 0x00, 0xff);
      break;
    default:
      colourToFill = RGB(0x00, 0x00, 0x00);
    }
    if (ledvalue.color[0] != colourToFill.color[0] ||
      ledvalue.color[1] != colourToFill.color[1] ||
      ledvalue.color[2] != colourToFill.color[2]) {
      cube.set(x, y, z, colourToFill);
      filled++;
      delay(20);
    }
  }
}
This entry was posted in Arduino, Cube, My Projects, NFC, rfid and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *