Created
June 13, 2012 09:50
-
-
Save treby/2923128 to your computer and use it in GitHub Desktop.
using NFC Shield and Full Color LED
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 <PN532.h> | |
#define SCK 13 | |
#define MOSI 11 | |
#define SS 10 | |
#define MISO 12 | |
#define LED_RED_PIN 6 | |
#define LED_GREEN_PIN 5 | |
#define LED_BLUE_PIN 3 | |
#define BUTTON_RED_UP 4 | |
#define BUTTON_RED_DOWN 2 | |
#define BUTTON_GREEN_UP 8 | |
#define BUTTON_GREEN_DOWN 7 | |
#define BUTTON_BLUE_UP A0 | |
#define BUTTON_BLUE_DOWN 9 | |
#define LED_CHANGE_STEP 16 | |
PN532 pn532(SCK, MISO, MOSI, SS); | |
uint16_t r, g, b, _r, _g, _b; | |
boolean waitTagRelease = false; | |
boolean dirty = false; | |
void setup(void) { | |
pn532.begin(); | |
Serial.begin(9600); | |
uint32_t versiondata = pn532.getFirmwareVersion(); | |
if (!versiondata) { | |
Serial.println("Error - Didn't find PN53x board"); | |
while(1); | |
} | |
} | |
void loop(void) { | |
uint32_t id; | |
id = pn532.readPassiveTargetID(PN532_MIFARE_ISO14443A); | |
if (id != 0) { // Tag がある | |
if (!waitTagRelease) { // かざされたばかりだ | |
uint8_t pageBytes[16]; | |
if(pn532.readMfulPages(1, 4, pageBytes) && pageBytes[0] == 0x39) { | |
_r = r = pageBytes[1]; | |
_g = g = pageBytes[2]; | |
_b = b = pageBytes[3]; | |
waitTagRelease = true; | |
setLedValue(_r, _g, _b); | |
Serial.print("rgb:"); Serial.print(_r); Serial.print(_g); Serial.println(_b); | |
} else { | |
Serial.println("read failure"); | |
} | |
} else { // Tagが置かれているのでタクトスイッチを監視している | |
if(chkButtons()) { | |
// 値の変化あり | |
dirty = true; | |
setLedValue(_r, _g, _b); | |
} else { | |
// 値の変化なし | |
if (dirty && ( _r != r || _g != g || _b != b )) { // タグ書き込みの必要はあるか? | |
writeRGBtoTag(_r, _g, _b); | |
r = _r; g = _g; b = _b; | |
} | |
dirty = false; | |
} | |
} | |
} else { // Tag がない | |
waitTagRelease = false; | |
setLedValue(0, 0, 0); | |
} | |
delay(5); | |
} | |
boolean chkButtons() { | |
boolean flg = false; | |
if(chkButton(BUTTON_RED_UP, &_r, true)) flg = true; | |
if(chkButton(BUTTON_RED_DOWN, &_r, false)) flg = true; | |
if(chkButton(BUTTON_GREEN_UP, &_g, true)) flg = true; | |
if(chkButton(BUTTON_GREEN_DOWN, &_g, false)) flg = true; | |
if(chkButton(BUTTON_BLUE_UP, &_b, true)) flg = true; | |
if(chkButton(BUTTON_BLUE_DOWN, &_b, false)) flg = true; | |
return flg; | |
} | |
boolean chkButton(uint8_t pin, uint16_t *value, boolean isUp) { | |
if(digitalRead(pin) == HIGH) { | |
if(isUp) { // UP | |
if(*value < 0xFF) { | |
*value += LED_CHANGE_STEP; | |
if(*value > 0xFF) *value = 0xFF; | |
} | |
} else { // DOWN | |
if(*value > 0x00) { | |
*value -= LED_CHANGE_STEP; | |
if(*value > 0xFF) *value = 0x00; | |
} | |
} | |
return true; | |
} | |
return false; | |
} | |
void setLedValue(uint8_t red, uint8_t green, uint8_t blue) { | |
analogWrite(LED_RED_PIN, (int) red); | |
analogWrite(LED_GREEN_PIN, (int) green); | |
analogWrite(LED_BLUE_PIN, (int) blue); | |
} | |
void writeRGBtoTag(uint8_t red, uint8_t green, uint8_t blue) { | |
uint8_t pageBytes[4] = { 0x39, red, green, blue }; | |
if(pn532.writeMfulPage(1, 4, pageBytes)) { | |
Serial.println("write success"); | |
} else { | |
Serial.println("write failure"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment