#include <SPI.h>
#include <PN532_SPI.h>
#include "PN532.h"
#include <LiquidCrystal.h>
PN532_SPI pn532spi(SPI, 10);
PN532 nfc(pn532spi);
LiquidCrystal lcd(4,5,0,6,7,8);
static uint8_t selected_option = 0;
#define LED0 17
#define LED1 18
#define LED2 19
boolean TurnDetected;
boolean up;
const int PinCLK=2; // Used for generating interrupts using CLK signal
const int PinDT=3; // Used for reading DT signal
//const int PinSW=4; // Used for the push button switch
void initEncoder(void) {
/* Setup encoder pins as inputs */
pinMode(PinCLK,INPUT);
pinMode(PinDT,INPUT);
attachInterrupt (0,isr,FALLING); // interrupt 0 is always connected to pin 2 on Arduino UNO
Serial.begin (9600);
Serial.println("Start");
}
/********************************
Initialize pins for LEDs
********************************/
void initLED(void) {
pinMode(LED0, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
digitalWrite(LED0, LOW);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
syncLED();
}
void initNFC(void) {
nfc.begin();
lcd.clear();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print("Didn't find PN53x board");
while (1); // halt
}
// Got ok data, print it out!
lcd.print("found board");
Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
// Set the max number of retry attempts to read from a card
// This prevents us from waiting forever for a card, which is
// the default behaviour of the PN532.
nfc.setPassiveActivationRetries(0xFF);
// configure board to read RFID tags
nfc.SAMConfig();
Serial.println("Waiting for an ISO14443A card");
}
void isr () { // Interrupt service routine is executed when a HIGH to LOW transition is detected on CLK
if (digitalRead(PinCLK))
up = digitalRead(PinDT);
else
up = !digitalRead(PinDT);
TurnDetected = true;
}
void uid_output(void) {
uint8_t uidReadCount=0;
boolean success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
// Wait for an ISO14443A type cards (Mifare, etc.). When one is found
// 'uid' will be populated with the UID, and uidLength will indicate
// if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
lcd.clear();
if (success) {
//Serial.println("Found a card!");
//Serial.print("UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
if (uidLength == 7) {
Serial.print(" ");
for (uint8_t i=0; i < uidLength; i++)
{
if (uid <= 0x0F) { Serial.print(" 0x0");lcd.print(" 0x0"); }
else { Serial.print(" 0x"); lcd.print(" 0x");}Serial.print(uid_, HEX);lcd.print(uid*,HEX);Serial.print(",");lcd.print(",");
}
Serial.println(""); lcd.print("");
}
//Beep to indicate tag was read*
* // tone(PIEZO_PIN, NOTE_G5, 180);*
// lcd.setCursor(0,1);
* // lcd.clear();
// lcd.print(++uidReadCount);*_
* //Serial.print("UID Value: ");*
* //for (uint8_t i=0; i < uidLength; i++)
_ //{
// Serial.print(" 0x");Serial.print(uid, HEX);
//}
//Serial.println("");
// Wait 1 second before continuing*
* delay(1000);
}
else*
* {
// PN532 probably timed out waiting for a card*
* //Serial.println("Timed out waiting for a card");
}
}
void syncLED(void) {
digitalWrite(LED0, LOW);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);_
switch(selected_option) {
_ case 0:
digitalWrite(LED0, HIGH);
break;
case 1:
digitalWrite(LED1, HIGH);
break;
case 2:
digitalWrite(LED2, HIGH);
break;
}
}
/********************************
Increments the voting selection*
/
void incSelOpt(void) {
* if (++selected_option > 2) { selected_option = 0; }*
* syncLED();
}*
/
* Decrements the voting selection*
********************************/
void decSelOpt(void) {
* //Postfix checks before decrement*
* if (selected_option-- == 0 ) { selected_option = 2; }*
* syncLED();
}
void setup() {
lcd.begin(20,4); // put your setup code here, to run once:
initEncoder();
initLED();
initNFC();
}
void loop() {
if (TurnDetected) { // do this only if rotation was detected*
* if (up)
incSelOpt();
else*
* decSelOpt();
TurnDetected = false; // do NOT repeat IF loop until new rotation detected*
* Serial.print ("Count = "); _
//lcd.print(selected_option,DEC);
Serial.println (selected_option);
_}
uid_output();
// put your main code here, to run repeatedly:
}*_