Issues with my RFID code

Hey everyone.

My name is Will Brett and I am a product design student from the UK. I love the Arduino and have done a few simple projects using it, however I am not very good with code and for this reason I have run into a problem which is probably obviously simple to solve but I can not.

I am building a toy that uses RFID tags. Very similar to the Rfid Teddy project on Make, in fact I am using some of the code to build a basic prototype, however the highlighted bit is the bugger thats playing (by which i mean iv messed up).

I want a specific tag to turn on a vibration motor, but each time i bring one of the tags near the RFID it turns on both motors which i dont understand. But the tags that start the audio dont turn the vibration motors on so its definitely an issue with my additions.

Hope someone can help me with this. I am a novice but I am more than willing to learn

here is the code:

// An interactive Toy for children - RFID triggered sound and vibration 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 5

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

char knowntags[NUMTAGS][11] = {"00005D51AE", "00005DCE95", "03000E152C"};
char soundfiles[NUMTAGS][9] = {"1000.WAV", "1001.WAV", "1002.WAV"};
char filename[1][13];

String matchingTagOne = "8400336E87";
String matchingTagTwo = "8400338DD8";

const int vibOne = 8;         // Vibration Motor One
const int vibTwo = 6;         // Vibration Motor Two

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);

  pinMode(vibOne, OUTPUT);
  pinMode(vibTwo, 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 
        {
          if (matchingTagOne.equals("8400336E87")) 
          {
            digitalWrite(vibOne, HIGH);
            delay(200);
            digitalWrite(vibOne, LOW);
          }
          else 
          {
            digitalWrite(vibOne, LOW);
          }                     
          if (matchingTagTwo.equals("8400338DD8")) 
          {
            digitalWrite(vibTwo, HIGH);
            delay(200);
            digitalWrite(vibTwo, LOW);
          }
          else 
          {
            digitalWrite(vibTwo, LOW);
          }

        }
        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();
  }
}

You need to repost your code, correctly this time. Modify your post. Select the code. Delete it. Press the # icon. Paste your code, and save, without trying to color code it. Put comments in if you need to direct our attention to a particular bit of code.

I doubt that the code displays in italics in the IDE, like your code does here.

Before you repost the code, though, put each { on a new line, and use Tools + Auto Format to properly indent the code.

Break long comments into multiple lines. There is no reason to have to scroll back and forth to read long comments. That’s why your keyboard comes equipped with a return key.

Then, I suggest that you ditch the String class. There is no need for it. Learn to use char arrays, instead.

char filename[1][13];

A 2D array that is one column wide just screams clueless.

In the highlighted code, you are not comparing the scanned tag to some known value. You are comparing some known value to another known value. Do you know that turning the pin on does anything to the motor? What kind of motor is it? There are very, very few that draw less than the 20mA that the Arduino can safely provide.

for(int i = 0; i<8; i++) {    // Make a filename from the first 8 characters of the RFID tag number
    filename[0]=codetoplay;
  }

The strncpy() function can do this in one line, not 4.

  filename[0][8]='.';
  filename[0][9]='w';
  filename[0][10]='a';
  filename[0][11]='v';

The strcat() function can do this in one line, not 4.

Thanks Paul for replying.

I have updated the post with the code added correctly.

However I havent made those changes yet as I do not understand some of the things you have suggested e.g. the strncpy() function and the strcat() function because as you have put it "just screams clueless". Which to be honest is a fair comment but those sections were written by David Harris from Make and not me so I am not entirely sure what they are doing other than what the comments say.

I have a lot of books about arduino that o'reilly have printed so I will go through those and learn how to use char properly.

The vibration motors that I am using are coin/pancake vibration motors bought from ebay: http://www.ebay.co.uk/itm/290794946294?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2649

datasheet: http://www.sparkfun.com/datasheets/Robotics/310-101_datasheet.pdf

mA rated 75 start mA 85

mA rated 75 start mA 85

Which is more than double what an Arduino pin can provide, even at the maximum end of the scale. You will need a transistor and an external power source to drive those motors.

So an external powersource and a transistor are the way to go... NPN was one someone recomended on instructables