Skip to content

Instantly share code, notes, and snippets.

@kmicheli
Created April 20, 2019 21:52
Show Gist options
  • Save kmicheli/42adcfb6f72c13e284643a199f3b7468 to your computer and use it in GitHub Desktop.
Save kmicheli/42adcfb6f72c13e284643a199f3b7468 to your computer and use it in GitHub Desktop.
#include <SdFatConfig.h>
#include <sdios.h>
#include <FreeStack.h>
#include <MinimumSerial.h>
#include <SdFat.h>
#include <BlockDriver.h>
#include <SdFatUtil.h>
#include <SysCall.h>
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>
#include <SPI.h>
#include <SoftwareSerial.h>
SdFat sd;
SFEMP3Shield MP3player;
// Choose two pins for SoftwareSerial
SoftwareSerial rSerial(10, 1); // RX, TX
// For SparkFun's tags, we will receive 16 bytes on every
// tag read, but throw four away. The 13th space will always
// be 0, since proper strings in Arduino end with 0
// These constants hold the total tag length (tagLen) and
// the length of the part we want to keep (idLen),
// plus the total number of tags we want to check against (kTags)
const int tagLen = 16;
const int idLen = 13;
const int kTags = 5;
// Put your known tags here!
char knownTags[kTags][idLen] = {
"7F00444EF782",
"7F001CF0FF6C",
"0E00598DE832",
"7F00444EFC89",
"0E0059A50EFC"
};
// Empty array to hold a freshly scanned tag
char newTag[idLen];
void setup() {
// Starts the hardware and software serial ports
Serial.begin(9600);
rSerial.begin(9600);
//start the shield
sd.begin(SD_SEL, SPI_HALF_SPEED);
MP3player.begin();
}
void loop() {
// Counter for the newTag array
int i = 0;
// Variable to hold each byte read from the serial buffer
int readByte;
// Flag so we know when a tag is over
boolean tag = false;
// This makes sure the whole tag is in the serial buffer before
// reading, the Arduino can read faster than the ID module can deliver!
if (rSerial.available() == tagLen) {
tag = true;
}
if (tag == true) {
while (rSerial.available()) {
// Take each byte out of the serial buffer, one at a time
readByte = rSerial.read();
/* This will skip the first byte (2, STX, start of text) and the last three,
ASCII 13, CR/carriage return, ASCII 10, LF/linefeed, and ASCII 3, ETX/end of
text, leaving only the unique part of the tag string. It puts the byte into
the first space in the array, then steps ahead one spot */
if (readByte != 2 && readByte!= 13 && readByte != 10 && readByte != 3) {
newTag[i] = readByte;
i++;
}
// If we see ASCII 3, ETX, the tag is over
if (readByte == 3) {
tag = false;
}
}
}
// don't do anything if the newTag array is full of zeroes
if (strlen(newTag)== 0) {
return;
}
else {
int total = 0;
for (int ct=0; ct < kTags; ct++){
total += checkTag(newTag, knownTags[ct]);
}
// If newTag matched any of the tags
// we checked against, total will be 1
if (total > 0) {
// Put the action of your choice here!
// TRIGGER AUDIO HERE
if (strcmp(newTag, "7F00444EF782") == 0){
Serial.println("TAG 1");
delay(5000);
MP3player.playMP3("track001.mp3");
}
if (strcmp(newTag, "7F001CF0FF6C") == 0){
Serial.println("TAG 2");
}
if (strcmp(newTag, "0E00598DE832") == 0){
Serial.println("TAG 3");
}
if (strcmp(newTag, "7F00444EFC89") == 0){
Serial.println("TAG 4");
}
if (strcmp(newTag, "0E0059A50EFC") == 0){
Serial.println("TAG 5");
}
}
}
// Once newTag has been checked, fill it with zeroes
// to get ready for the next tag read
for (int c=0; c < idLen; c++) {
newTag[c] = 0;
}
}
// This function steps through both newTag and one of the known
// tags. If there is a mismatch anywhere in the tag, it will return 0,
// but if every character in the tag is the same, it returns 1
int checkTag(char nTag[], char oTag[]) {
for (int i = 0; i < idLen; i++) {
if (nTag[i] != oTag[i]) {
return 0;
}
}
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment