Last active
December 17, 2015 23:39
-
-
Save don/5691236 to your computer and use it in GitHub Desktop.
Arduino NFC Demo from Maker Faire Bay Area 2013 http://don.github.io/slides/2013-05-18-arduino-nfc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include <Adafruit_NFCShield_I2C.h> | |
#include <NfcAdapter.h> | |
NfcAdapter nfc = NfcAdapter(); | |
#include <Adafruit_NeoPixel.h> | |
// Parameter 1 = number of pixels in strip | |
// Parameter 2 = pin number (most are valid) | |
// Parameter 3 = pixel type flags, add together as needed: | |
// NEO_RGB Pixels are wired for RGB bitstream | |
// NEO_GRB Pixels are wired for GRB bitstream | |
// NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels) | |
// NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip) | |
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, 6, NEO_GRB + NEO_KHZ800); | |
void setup(void) { | |
Serial.begin(115200); | |
Serial.println("NFC Light Demo"); | |
nfc.begin(); | |
strip.begin(); | |
strip.show(); // Initialize all pixels to 'off' | |
} | |
void loop(void) { | |
Serial.println("\nScan a NFC tag\n"); | |
if (nfc.tagPresent()) | |
{ | |
NfcTag tag = nfc.read(); | |
tag.print(); | |
NdefMessage message = tag.getNdefMessage(); | |
NdefRecord record = message[0]; | |
// Note we really should only handle TNF_MIME_MEDIA | |
// and ensure that the mime type is "text/led" | |
// payload is a byte array, could be binary | |
String payload = getPayloadAsString(record); | |
if (payload.startsWith("red")) | |
{ | |
setColor(255,0,0); | |
} | |
else if (payload.startsWith("green")) | |
{ | |
setColor(0,255,0); | |
} | |
else if (payload.startsWith("blue")) | |
{ | |
setColor(0,0,255); | |
} | |
else if (payload.startsWith("bounce")) | |
{ | |
bounce(); | |
} | |
else if (payload.startsWith("rainbow")) | |
{ | |
rainbowCycle(5); | |
} | |
else if (payload.startsWith("off")) | |
{ | |
setColor(0,0,0); | |
} | |
else | |
{ | |
blink(); | |
} | |
} | |
delay(2000); | |
} | |
// set the whole strip to one color | |
void setColor(uint8_t red, uint8_t green, uint8_t blue) { | |
uint8_t i; | |
for (i = 0; i < strip.numPixels(); i++) { | |
strip.setPixelColor(i, red, green, blue); | |
} | |
strip.show(); | |
} | |
// blink the strip on and off | |
void blink() { | |
uint8_t i; | |
uint8_t times = 3; | |
for (i = 0; i < times; i++) { | |
setColor(255,255,255); | |
delay(200); | |
setColor(0,0,0); | |
delay(200); | |
} | |
} | |
// make a light bounce up and down the strip | |
void bounce() { | |
uint8_t i, x; | |
uint8_t times = 3; | |
uint8_t numPixels = strip.numPixels(); | |
uint8_t wait = 20; | |
uint32_t color = strip.Color(255,0,255); | |
uint32_t off = strip.Color(0,0,0); | |
for (x = 0; x < times; x++) { | |
for (i = 0; i < numPixels; i++) { | |
strip.setPixelColor(i, color); | |
if (i > 0) { | |
strip.setPixelColor(i-1, off); | |
} | |
strip.show(); | |
delay(wait); | |
} | |
for (i = numPixels; i > 0; i--) { // note, this skips 0 | |
strip.setPixelColor(i, color); | |
if (i < numPixels) { | |
strip.setPixelColor(i+1, off); | |
} | |
strip.show(); | |
delay(wait); | |
} | |
// loop left light #1 on | |
strip.setPixelColor(1, off); | |
strip.show(); | |
} | |
} | |
// The payload is a byte array. We are expecting a string, but that is | |
// not always the case. NdefRecord needs more helper methods | |
String getPayloadAsString(NdefRecord record) { | |
int payloadLength = record.getPayloadLength(); | |
uint8_t* payloadBytes = (uint8_t*) malloc(payloadLength); | |
// optionally zero out array | |
memset(payloadBytes, 0, sizeof(payloadBytes)); | |
record.getPayload(payloadBytes); | |
// cast because we *think* the payload is a String | |
String payloadString = String((char*)payloadBytes); | |
free(payloadBytes); | |
return payloadString; | |
} | |
// rainbow methods are from Adafruit's strandtest sketch | |
void rainbow(uint8_t wait) { | |
uint16_t i, j; | |
for(j=0; j<256; j++) { | |
for(i=0; i<strip.numPixels(); i++) { | |
strip.setPixelColor(i, Wheel((i+j) & 255)); | |
} | |
strip.show(); | |
delay(wait); | |
} | |
} | |
// Slightly different, this makes the rainbow equally distributed throughout | |
void rainbowCycle(uint8_t wait) { | |
uint16_t i, j; | |
for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel | |
for(i=0; i< strip.numPixels(); i++) { | |
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255)); | |
} | |
strip.show(); | |
delay(wait); | |
} | |
} | |
// Input a value 0 to 255 to get a color value. | |
// The colours are a transition r - g - b - back to r. | |
uint32_t Wheel(byte WheelPos) { | |
if(WheelPos < 85) { | |
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); | |
} else if(WheelPos < 170) { | |
WheelPos -= 85; | |
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); | |
} else { | |
WheelPos -= 170; | |
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment