Last active
December 23, 2015 16:59
-
-
Save don/6665981 to your computer and use it in GitHub Desktop.
Arduino sketch for NY Maker Faire 2013 http://makerfaire.com/makers/arduino-nfc/
This file contains hidden or 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
| /* | |
| Seeed Studio (or Adafruit) NFC Shield | |
| Adafruit 60 LED NeoPixel Strip | |
| Arduino Uno | |
| NFC tag should be TNF MIME_MEDIA and Type text/led | |
| Payload should have RGB color as 0,0,255 | |
| */ | |
| #if 1 | |
| #include <SPI.h> | |
| #include <PN532_SPI.h> | |
| #include <PN532.h> | |
| #include <NfcAdapter.h> | |
| PN532_SPI pn532spi(SPI, 10); | |
| NfcAdapter nfc = NfcAdapter(pn532spi); | |
| #else | |
| #include <Wire.h> | |
| #include <PN532_I2C.h> | |
| #include <PN532.h> | |
| #include <NfcAdapter.h> | |
| PN532_I2C pn532_i2c(Wire); | |
| NfcAdapter nfc = NfcAdapter(pn532_i2c); | |
| #endif | |
| #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); | |
| uint32_t color; | |
| int wait = 5; | |
| int lastRead = 0; | |
| void setup(void) { | |
| Serial.begin(9600); | |
| Serial.println("NFC Light Demo"); | |
| nfc.begin(); | |
| strip.begin(); | |
| strip.show(); // Initialize all pixels to 'off' | |
| } | |
| void loop(void) { | |
| for (int i = 0; i < strip.numPixels(); i++) { | |
| strip.setPixelColor(i, color); | |
| delay(wait); | |
| strip.show(); | |
| } | |
| readNfc(); | |
| } | |
| void readNfc() { | |
| // limit reads | |
| if (millis() - lastRead < 1000) { | |
| return; | |
| } | |
| if (nfc.tagPresent()) { | |
| lastRead = millis(); | |
| NfcTag tag = nfc.read(); | |
| tag.print(); | |
| NdefMessage message = tag.getNdefMessage(); | |
| // only reading the first record | |
| NdefRecord record = message[0]; | |
| if (record.getTnf() == TNF_MIME_MEDIA && record.getType() == "text/led") { | |
| String payload = getPayloadAsString(record); | |
| color = parseColor(payload); | |
| } else { | |
| color = 0; | |
| } | |
| } | |
| } | |
| // The payload is a byte array. We are expecting a string. | |
| String getPayloadAsString(NdefRecord record) { | |
| int payloadLength = record.getPayloadLength(); | |
| byte payloadBytes[payloadLength]; | |
| record.getPayload(payloadBytes); | |
| // cast because we *think* the payload is a String | |
| String payloadString = String((char*)payloadBytes); | |
| return payloadString; | |
| } | |
| // return a color from comma separated string | |
| // expecting red,green,blue | |
| // example 0,0,255 | |
| uint32_t parseColor(String s) { | |
| int firstComma = s.indexOf(','); | |
| int lastComma = s.lastIndexOf(','); | |
| int red = s.substring(0, firstComma).toInt(); | |
| int green = s.substring(firstComma+1, lastComma).toInt(); | |
| int blue = s.substring(lastComma+1).toInt(); | |
| return strip.Color(red, green, blue); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment