Pages: [1] 2   Go Down
Author Topic: RFID Charlie Bear Project Software Snag from MAKEvol28  (Read 1298 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So I've tried to get help from the adafruit forums but none of the suggestions have helped. Anything you can come up with to help will be greatly appreciated by three very happy kids (hopefully) receiving this functioning project tomorrow.
If you're unfamiliar with the project listed in the subject, http://makeprojects.com/Project/Charlie-s-RFID-Teddy-Bear/1411/, meet my nemesis. Currently he has taken the form of an oversized black lab retriever with stuffing pouring out of his poorly unstitched sides and has a messy necklace of dangling wires, boards, and speakers. However, as far as I can tell, the problem I'm facing is not hardware (and yes, I bought everything on that parts list directly from MakerShed and Jameco- no dodgy 3rd party stuff)...
Last night I successfully got sound in the form of the "daphc.pde" sketch. My RFID reader prints the tags magnificently. However, trying to unite sound and RFID seem to be a bit beyond my grasp. Below is the code for "bear:"
Code:
// An interactive bear for Charlie -- RFID triggered sound responses

#include <FatReader.h>
#include <SdReader.h>
#include "WaveHC.h"
#include "WaveUtil.h"

SdReader memcard;
FatVolume vol;
FatReader root;
FatReader file;
WaveHC wave;

#define ENABLE 7      // Set the pin number for enabling the RFID reader. The Audio Shield uses pins 2-5.
#define NUMTAGS 22

int  val = 0;
char code[10];
int bytesread = 0;

char knowntags[NUMTAGS][11] = {"2100DFB002", "2100E0B6ED", "2100DFF660", "2100DFD586", "2100DFB1E5", "2100E0CBAA", "2100E07B21", "2100E0B9D6", "2100D82331", "2100DFA060", "2100DFBC77", "2100DFA69A", "2100D7EFF7", "2100DFE251", "2100E02F09", "2100DFBF83", "2100DFD9EA", "2100E0ABA6", "2100DFDFC4", "2100DFF0DF", "36005B7B50", "36005B7C05"};
char soundfiles[NUMTAGS][9] = {"1000.WAV", "1001.WAV", "1002.WAV", "1003.WAV", "1004.WAV", "1005.WAV", "1006.WAV", "1007.WAV", "1008.WAV", "1009.WAV", "1010.WAV", "1011.WAV", "1012.WAV", "1013.WAV", "1014.WAV", "1015.WAV", "1016.WAV", "1017.WAV", "1018.WAV", "1019.WAV", "1020.WAV", "1021.WAV"};
char filename[1][13];

void setup() {
  //////// Set up RFID reader to collect tag information /////////////////////
  Serial.begin(2400);         // RFID reader SOUT pin connected to Serial RX pin at 2400bps
  pinMode(ENABLE,OUTPUT);     // Set digital pin 7 as OUTPUT to connect it to the RFID /ENABLE pin
  digitalWrite(ENABLE, LOW);  // Activate the RFID reader
 
  //////// Set the pins to output for driving the Audio Shield
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
 
  ////////// Set up the Audio Shield by initializing the memory card for reading ////////
  if (!memcard.init()) {
    // If something went wrong sdErrorCheck prints out the error check
    putstring_nl("Card init. failed!");
    cardErrorCheck();
    return;
  }
 
  //This will optimize the reading of the memory card -- remove it if it times out
  memcard.partialBlockRead(true);
 
  // Find a FAT formatted partition by looking in teh first five slots. Remember your memory card should be FAT16 or FAT32 formatted
  uint8_t partition;
  for (partition = 0; partition < 5; partition++) {
    if (vol.init(memcard, partition))
      break;
  }
  if (partition == 5)
  {
    putstring_nl("No valid FAT partition");
    cardErrorCheck();
    while(1); // This is a point of no return. Format your memory card properly and try again.
  }

  // Open the root directory for reading the files
  if (!root.openRoot(vol))
  {
    putstring_nl("Can't open root directory");
    while(1); // Something went wrong here so investigate the file system on your memory card.
  }
 
  // If you got this far then the card is ready to read
  putstring_nl("Ready to go");
}

// If we find an error, check what the error is and show it on the serial terminal
void cardErrorCheck(void)
{
  if(!memcard.errorCode()) return;
  putstring("\n\rSD I/O error:");
  Serial.print(memcard.errorCode());
  putstring(", ");
  Serial.print(memcard.errorData());
  while(1); // Stick here if there is an error
}

void loop() {

  if(Serial.available() > 0) {          // if data available from reader 
    if((val = Serial.read()) == 10) {   // check for header
      bytesread = 0;
      while(bytesread<10) {              // read 10 digit code
        if( Serial.available() > 0) {
          val = Serial.read();
          if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading
            break;                       // stop reading
          }
          code[bytesread] = val;         // add the digit           
          bytesread++;                   // ready to read next digit 
        }
      }
      if(bytesread == 10) {              // if 10 digit read is complete
        playsound(code);
        Serial.print("TAG code is: ");   // possibly a good TAG
        Serial.println(code);            // print the TAG code
        Serial.flush();                  // Flush the serial buffer before trying to read a new code
      }
      bytesread = 0;
    }
  }

}

void playsound(char codetoplay[]) {
  for(int i = 0; i<8; i++) {    // Make a filename from the first 8 characters of the RFID tag number
    filename[0][i]=codetoplay[i];
  }
  filename[0][8]='.';
  filename[0][9]='w';
  filename[0][10]='a';
  filename[0][11]='v';
  silence(); //shut down anything that is currently playing and close that file
  playfile(filename[0]);
 
}

void playfile(char *name) {
  if (!file.open(root, name)) {
      putstring_nl("Couldn't open file");
      return;
   }
   if (!wave.create(file)) {
     putstring_nl("Not a valid WAV");
     return;
   }
  wave.play();
}

void silence() {
  if(wave.isplaying) {
    wave.stop();
  }
}

So what does Serial Monitor say when I put a RFID tag near the reader?
Code:
Ready to go
Couldn't open file
TAG code is: 3D001ED8B7

Near as I can tell, my primary issue here is confusion in the CharlieBear sketch where there's a software disconnect between reading the RFID tag, truncating the first 8 characters, and summoning the corresponding .wav file. I've run tests (dapHC, for those familiar) and have yielded my audio file on loop, playing as expected. I don't think it's a hardware issue.

Any ideas?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 602
Posts: 33371
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It looks like you are not getting the file name right. I would add some serial print debug lines to see the name of the file you are trying to open. Then double check that this file is actually on the SD card and is a valid file.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are passing the RFID tag to the playsound() function. That function is then overwriting the characters in positions 8 to 11, but not appending a NULL.

So, when you scan the tag 3D001ED8B7, the file to be played becomes 3D001ED8.wav. Does that file really exist?

Or, are you supposed to be looking that tag up in the knowntags array, and playing the corresponding file from the soundfiles array?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First of all, thank you for the speedy responses!

It looks like you are not getting the file name right. I would add some serial print debug lines to see the name of the file you are trying to open.
Bear with me here, this is my first major project. I've seen this as common advice but I don't know the first thing about where to start. What exactly should I put and here?

You are passing the RFID tag to the playsound() function. That function is then overwriting the characters in positions 8 to 11, but not appending a NULL.

So, when you scan the tag 3D001ED8B7, the file to be played becomes 3D001ED8.wav. Does that file really exist?

Or, are you supposed to be looking that tag up in the knowntags array, and playing the corresponding file from the soundfiles array?
The physical file exists on the root of the SD card in the waveShield, yes. In terms of the code of the sketch, the project instructions mention no where that the builder should alter the code to get the project to function. That said, they provide very little in the way of troubleshooting as it is. Should I add the known tag "3D001ED8B7" and sound file "3D001ED8.wav" to  the sketch and remove the ones that came with it?
Logged

Ontario, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've read the code and I'm certain that you don't need those arrays defined at the top
ie.
Code:
char knowntags[NUMTAGS][11] = {"2100DFB002", "2100E0B6ED", "2100DFF660", "2100DFD586", "2100DFB1E5", "2100E0CBAA", "2100E07B21", "2100E0B9D6", "2100D82331", "2100DFA060", "2100DFBC77", "2100DFA69A", "2100D7EFF7", "2100DFE251", "2100E02F09", "2100DFBF83", "2100DFD9EA", "2100E0ABA6", "2100DFDFC4", "2100DFF0DF", "36005B7B50", "36005B7C05"};
char soundfiles[NUMTAGS][9] = {"1000.WAV", "1001.WAV", "1002.WAV", "1003.WAV", "1004.WAV", "1005.WAV", "1006.WAV", "1007.WAV", "1008.WAV", "1009.WAV", "1010.WAV", "1011.WAV", "1012.WAV", "1013.WAV", "1014.WAV", "1015.WAV", "1016.WAV", "1017.WAV", "1018.WAV", "1019.WAV", "1020.WAV", "1021.WAV"};
It looks like code that was never used in the final product.

I'm not 100% sure if this'll work, but can you try adding this line
Code:
Serial.print(name);
right before
Code:
putstring_nl("Couldn't open file");

end result should be
Code:
void playfile(char *name) {
  if (!file.open(root, name)) {
      Serial.print(name);
      putstring_nl("Couldn't open file");
      return;
   }
   if (!wave.create(file)) {
     putstring_nl("Not a valid WAV");
     return;
   }
  wave.play();
}

Then post what the result is?
Logged

Ontario, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Also, this should fix it as well (taking into account what PaulS said)
change the playsound function to
Code:
void playsound(char codetoplay[]) {
  for(int i = 0; i<8; i++) {    // Make a filename from the first 8 characters of the RFID tag number
    filename[0][i]=codetoplay[i];
  }
  filename[0][8]='.';
  filename[0][9]='w';
  filename[0][10]='a';
  filename[0][11]='v';
  [b]filename[0][12]='0';[/b]
  silence(); //shut down anything that is currently playing and close that file
  playfile(filename[0]);

}
"should" fix it
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Also, this should fix it as well (taking into account what PaulS said)
change the playsound function to
Code:
void playsound(char codetoplay[]) {
  for(int i = 0; i<8; i++) {    // Make a filename from the first 8 characters of the RFID tag number
    filename[0][i]=codetoplay[i];
  }
  filename[0][8]='.';
  filename[0][9]='w';
  filename[0][10]='a';
  filename[0][11]='v';
  [b]filename[0][12]='0';[/b]
  silence(); //shut down anything that is currently playing and close that file
  playfile(filename[0]);

}
"should" fix it

I made the changes you suggested but before I could upload it, I verified it and this came up. Do you know what I need to do to fix it?
Logged

Ontario, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

add a ; after the filename[0][12] = '0';
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks- that was silly of me! The code compiled beautifully afterwards.
However, not all was swell in the serial monitor when I passed the corresponding tag infront of the reader:


It still won't open the file!
Logged

Ontario, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok, let's change just this line to:
Code:
[b]filename[0][12]='\0';[/b]
and try running again.
Also, does this file exist on the root of your sd card?
3D001ED8.wav ??
exactly as it is written there (case sensitive)?

EDIT:
You could also try removing that line altogether as it seems to add the null anyway...
ie. remove this line from the code
Code:
[b]filename[0][12]='\0';[/b]

or try both..i dunno smiley
« Last Edit: December 13, 2011, 03:21:38 pm by carlito » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
filename[0][12]='0';
is wrong. It should be:
Code:
filename[0][12]='\0';
Make sure that you changed the size of filename to allow it to hold 13 values.

There is a function to list the names of the files on the card. Add a call to that function, in setup(), and show us what names really exist.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok, let's change just this line to:
Code:
[b]filename[0][12]='\0';[/b]
and try running again.
Also, does this file exist on the root of your sd card?
3D001ED8.wav ??
exactly as it is written there (case sensitive)?

EDIT:
You could also try removing that line altogether as it seems to add the null anyway...
ie. remove this line from the code
Code:
[b]filename[0][12]='\0';[/b]

or try both..i dunno smiley


I tried both versions of that line and neither worked. "3D001ED8.wav" exists on the root of the SD card. (The dapHC sketch prints the name on the Serial Monitor when executed.)

"Make sure that you changed the size of filename to allow it to hold 13 values.

There is a function to list the names of the files on the card. Add a call to that function, in setup(), and show us what names really exist."
^^ A bit of google researching has yielded no answers to what such function you've mentioned- plenty of sketches though. Did you have one in mind? (I'm sorry, I'm terribly new to all this.)


Logged

Ontario, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you post the output that you are receiving now please?

Again, have you made sure that the files on the sd card are EXACTLY what is displayed in the output? making sure to check for case-sensitivity?

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Absolutely!
Output:


Unless I'm missing something (and I very could be), it's identical- even casing:




And, just for kicks, here's the most recent iteration of the code:
Code:
// An interactive bear for Charlie -- RFID triggered sound responses

#include <FatReader.h>
#include <SdReader.h>
#include "WaveHC.h"
#include "WaveUtil.h"

SdReader memcard;
FatVolume vol;
FatReader root;
FatReader file;
WaveHC wave;

#define ENABLE 7      // Set the pin number for enabling the RFID reader. The Audio Shield uses pins 2-5.
#define NUMTAGS 22

int  val = 0;
char code[10];
int bytesread = 0;

char filename[1][13];

void setup() {
  //////// Set up RFID reader to collect tag information /////////////////////
  Serial.begin(2400);         // RFID reader SOUT pin connected to Serial RX pin at 2400bps
  pinMode(ENABLE,OUTPUT);     // Set digital pin 7 as OUTPUT to connect it to the RFID /ENABLE pin
  digitalWrite(ENABLE, LOW);  // Activate the RFID reader
 
  //////// Set the pins to output for driving the Audio Shield
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
 
  ////////// Set up the Audio Shield by initializing the memory card for reading ////////
  if (!memcard.init()) {
    // If something went wrong sdErrorCheck prints out the error check
    putstring_nl("Card init. failed!");
    cardErrorCheck();
    return;
  }
 
  //This will optimize the reading of the memory card -- remove it if it times out
  memcard.partialBlockRead(true);
 
  // Find a FAT formatted partition by looking in teh first five slots. Remember your memory card should be FAT16 or FAT32 formatted
  uint8_t partition;
  for (partition = 0; partition < 5; partition++) {
    if (vol.init(memcard, partition))
      break;
  }
  if (partition == 5)
  {
    putstring_nl("No valid FAT partition");
    cardErrorCheck();
    while(1); // This is a point of no return. Format your memory card properly and try again.
  }

  // Open the root directory for reading the files
  if (!root.openRoot(vol))
  {
    putstring_nl("Can't open root directory");
    while(1); // Something went wrong here so investigate the file system on your memory card.
  }
 
  // If you got this far then the card is ready to read
  putstring_nl("Ready to go");
}

// If we find an error, check what the error is and show it on the serial terminal
void cardErrorCheck(void)
{
  if(!memcard.errorCode()) return;
  putstring("\n\rSD I/O error:");
  Serial.print(memcard.errorCode());
  putstring(", ");
  Serial.print(memcard.errorData());
  while(1); // Stick here if there is an error
}

void loop() {

  if(Serial.available() > 0) {          // if data available from reader 
    if((val = Serial.read()) == 10) {   // check for header
      bytesread = 0;
      while(bytesread<10) {              // read 10 digit code
        if( Serial.available() > 0) {
          val = Serial.read();
          if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading
            break;                       // stop reading
          }
          code[bytesread] = val;         // add the digit           
          bytesread++;                   // ready to read next digit 
        }
      }
      if(bytesread == 10) {              // if 10 digit read is complete
        playsound(code);
        Serial.print("TAG code is: ");   // possibly a good TAG
        Serial.println(code);            // print the TAG code
        Serial.flush();                  // Flush the serial buffer before trying to read a new code
      }
      bytesread = 0;
    }
  }

}

void playsound(char codetoplay[]) {
  for(int i = 0; i<8; i++) {    // Make a filename from the first 8 characters of the RFID tag number
    filename[0][i]=codetoplay[i];
  }
  filename[0][8]='.';
  filename[0][9]='w';
  filename[0][10]='a';
  filename[0][11]='v';
  silence(); //shut down anything that is currently playing and close that file
  playfile(filename[0]);

}

void playfile(char *name) {
  if (!file.open(root, name)) {
      Serial.print(name);
      putstring_nl("Couldn't open file");
      return;
   }
   if (!wave.create(file)) {
     putstring_nl("Not a valid WAV");
     return;
   }
  wave.play();
}

void silence() {
  if(wave.isplaying) {
    wave.stop();
  }
}
Logged

Ontario, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your file shows an I where it should be a 1
ie. it should read 3D001ED887.wav instead of
3D00IED887.wav

Please change the I to a 1 and try again smiley
« Last Edit: December 13, 2011, 08:17:45 pm by carlito » Logged

Pages: [1] 2   Go Up
Jump to: