Skip to content

Instantly share code, notes, and snippets.

@ariejan
Created August 22, 2012 16:03
Show Gist options
  • Save ariejan/3427025 to your computer and use it in GitHub Desktop.
Save ariejan/3427025 to your computer and use it in GitHub Desktop.
Arduino Sketch for RFID Unit 1.0
/**
* RFID Ding 1.0
* Copyright (C) 2012 Kabisa ICT
* Authors:
* Ralph Rooding <[email protected]>
* Harm de Laat <[email protected]>
* Ariejan de Vroom <[email protected]>
*/
#include <SoftwareSerial.h>
#include <SD.h>
/**
Diagnostics
1 short beep - Tag scanned and logged
3 short beeps - Ready for operations
5 short beeps - SD Card error
0 beeps - Check battery
Scanned tags are logged to the SD card into a file
name 'RFIDLOG.TXT'.
**/
const int beepDelay = 250; // ms
// Pins for the RFID RX/TX.
// TX does not have to be wired.
const int pinRfidRx = 2;
const int pinRfidTx = 3;
// Pins for the SD Unit
const int pinMosi = 11;
const int pinMiso = 12;
const int pinSck = 13;
const int pinSs = 10;
// Ananlog pin for the Biezo
const int pinBeep = 6;
// Used for reading rfid tags
byte tmpByte;
char tmpValue[13];
// Create a software serial interface for the ID12
SoftwareSerial rfidSerial(pinRfidRx, pinRfidTx);
// Beep `times` for `duration`
void beepShort(int times) {
beepWithDuration(beepDelay, times);
}
void beepLong(int times) {
beepWithDuration(beepDelay * 3, times);
}
void beepWithDuration(unsigned char duration, int times) {
for(int i = 0; i < times; i++) {
analogWrite(pinBeep, 25);
delay(duration);
analogWrite(pinBeep, 0);
delay(duration);
}
}
// Log a scanned tag
void logTag(String tag) {
// Write to serial for logging
Serial.println("Scanned tag " + tag);
// Write to SD Card for future (evil) purposes.
File dataFile = SD.open("rfidlog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(tag);
dataFile.close();
beepShort(1);
} else {
Serial.println("!! Error opening the SD card for writing");
beepShort(5);
}
}
void setup() {
// Open Serial for logging
Serial.begin(9600);
// Set Biezo pin to output
pinMode(pinBeep, OUTPUT);
// Open serial connection to ID12
rfidSerial.begin(9600);
// Check if the SD card is working
if (!SD.begin(pinSs)) {
// Something went wrong, log it and beep an error code.
Serial.println("!! SD Card initialization failed. Is the card present?");
beepShort(5);
return;
}
Serial.println("RFID Ding 1.0");
Serial.println("Waiting for scans...");
beepShort(3);
}
void loop() {
int incomingByte = 0;
// Check for available Rfid data
if (rfidSerial.available() > 0) {
incomingByte = rfidSerial.read();
if (incomingByte == 2) { // RFID Header
tmpByte = 0;
} else {
if (tmpByte < 12) {
tmpValue[tmpByte] = incomingByte;
tmpByte++;
}
if (incomingByte == 3) {
String tagValue = String(tmpValue);
logTag(tagValue);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment