Music Maker and RFID?

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
Inge437
 
Posts: 21
Joined: Wed Apr 15, 2015 3:09 pm

Music Maker and RFID?

Post by Inge437 »

Hello all!

I'm trying to make a RFID music player. I have the Music Maker and a ID12 RFID reader on a RFID reader breakout board.

I tested the Music Maker with a library example and that's working. I can also read the ID's of the RFID tags.

I am probally not the first one doing this, but I cannot get it working. Tag 1 should play song 1, etc.
I'm not so advanced with RFID and I have no idea how to achieve this. I'm wondering if it is even possible, as the RFID and the song probably both use the serial?

I hope you can help me out and please bear with me ;-)

Thanks in advance!

Reading the ID's of the RFID tags: (I'm shifting between pin 7 and pin 0 for the RFID read input)

Code: Select all

#include <SoftwareSerial.h>

SoftwareSerial RFID = SoftwareSerial(7, 6); //RX is 7, TX is 6
String key; //The key that was scanned

void setup ()
{
  Serial.begin(9600);
  RFID.begin(9600); //Begins connection with the reader
}

char read; //The most recent character read

void loop ()
{
   while(RFID.available() > 0)
   {
      read = RFID.read();
      key += read;
      if(key.indexOf("1000080049") >= 0) {Serial.println('a'); key = "";} //Change to the ID of your key a
      if(key.indexOf("100007FFC3") >= 0) {Serial.println('b'); key = "";} //Change to the ID of your key b
      if(key.indexOf("100008475F") >= 0) {Serial.println('c'); key = "";} //Change to the ID of your key c
      if(key.indexOf("100008954E") >= 0) {Serial.println('d'); key = "";} //Change to the ID of your key d
   }
}
And the player_simple library example:

Code: Select all

/*************************************************** 
  This is an example for the Adafruit VS1053 Codec Breakout

  Designed specifically to work with the Adafruit VS1053 Codec Breakout 
  ----> https://www.adafruit.com/products/1381

  Adafruit invests time and resources providing this open source code, 
  please support Adafruit and open-source hardware by purchasing 
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution
 ****************************************************/

// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>

// define the pins used
//#define CLK 13       // SPI Clock, shared with SD card
//#define MISO 12      // Input data, from VS1053/SD card
//#define MOSI 11      // Output data, to VS1053/SD card
// Connect CLK, MISO and MOSI to hardware SPI pins. 
// See http://arduino.cc/en/Reference/SPI "Connections"

// These are the pins used for the breakout example
#define BREAKOUT_RESET  9      // VS1053 reset pin (output)
#define BREAKOUT_CS     10     // VS1053 chip select pin (output)
#define BREAKOUT_DCS    8      // VS1053 Data/command select pin (output)
// These are the pins used for the music maker shield
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)

// These are common pins between breakout and shield
#define CARDCS 4     // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin

Adafruit_VS1053_FilePlayer musicPlayer = 
  // create breakout-example object!
  //Adafruit_VS1053_FilePlayer(BREAKOUT_RESET, BREAKOUT_CS, BREAKOUT_DCS, DREQ, CARDCS);
  // create shield-example object!
  Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
  
void setup() {
  Serial.begin(9600);
  Serial.println("Adafruit VS1053 Simple Test");

  if (! musicPlayer.begin()) { // initialise the music player
     Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
     while (1);
  }
  Serial.println(F("VS1053 found"));
  
  SD.begin(CARDCS);    // initialise the SD card
  
  // Set volume for left, right channels. lower numbers == louder volume!
  musicPlayer.setVolume(20,20);

  // Timer interrupts are not suggested, better to use DREQ interrupt!
  //musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT); // timer int

  // If DREQ is on an interrupt pin (on uno, #2 or #3) we can do background
  // audio playing
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);  // DREQ int
  
  // Play one file, don't return until complete
  Serial.println(F("Playing track 001"));
  musicPlayer.playFullFile("track001.mp3");
  // Play another file in the background, REQUIRES interrupts!
  Serial.println(F("Playing track 002"));
  musicPlayer.startPlayingFile("track002.mp3");
}

void loop() {
  // File is playing in the background
  if (musicPlayer.stopped()) {
    Serial.println("Done playing music");
    while (1);
  }
  if (Serial.available()) {
    char c = Serial.read();
    
    // if we get an 's' on the serial console, stop!
    if (c == 's') {
      musicPlayer.stopPlaying();
    }
    
    // if we get an 'p' on the serial console, pause/unpause!
    if (c == 'p') {
      if (! musicPlayer.paused()) {
        Serial.println("Paused");
        musicPlayer.pausePlaying(true);
      } else { 
        Serial.println("Resumed");
        musicPlayer.pausePlaying(false);
      }
    }
  }

  delay(100);
}

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Music Maker and RFID?

Post by adafruit_support_rick »

So the code you posted works, right? Can you post the combined code?

User avatar
Inge437
 
Posts: 21
Joined: Wed Apr 15, 2015 3:09 pm

Re: Music Maker and RFID?

Post by Inge437 »

Yes, they do. I think the merge is also causing problems.
The "player_simple" from the library automatically starts playing the songs from the SD card. Now I want it to wait for input from the RFID reader.
If tag 1 is scanned, play song 1. If tag 2 is scanned, play song 2. Etc, you get the point.

However, I'm not sure what the best way is to merge the code (and if it's even possible!)

Code: Select all

#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
#include <SoftwareSerial.h>

SoftwareSerial RFID = SoftwareSerial(0, 2); //RX is 6, TX is 7
String key; //The key that was scanned

// define the pins used
//#define CLK 13       // SPI Clock, shared with SD card
//#define MISO 12      // Input data, from VS1053/SD card
//#define MOSI 11      // Output data, to VS1053/SD card
// Connect CLK, MISO and MOSI to hardware SPI pins. 
// See http://arduino.cc/en/Reference/SPI "Connections"

// These are the pins used for the breakout example
#define BREAKOUT_RESET  9      // VS1053 reset pin (output)
#define BREAKOUT_CS     10     // VS1053 chip select pin (output)
#define BREAKOUT_DCS    8      // VS1053 Data/command select pin (output)
// These are the pins used for the music maker shield
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)

// These are common pins between breakout and shield
#define CARDCS 4     // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin

Adafruit_VS1053_FilePlayer musicPlayer = 
  // create breakout-example object!
  //Adafruit_VS1053_FilePlayer(BREAKOUT_RESET, BREAKOUT_CS, BREAKOUT_DCS, DREQ, CARDCS);
  // create shield-example object!
  Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
  
void setup ()
{
  Serial.begin(9600);
  RFID.begin(9600); //Begins connection with the reader
  Serial.println("Adafruit VS1053 Simple Test");
  
    if (! musicPlayer.begin()) { // initialise the music player
     Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
     while (1);
  }
    Serial.println(F("VS1053 found"));
  
  SD.begin(CARDCS);    // initialise the SD card
  
  // Set volume for left, right channels. lower numbers == louder volume!
  musicPlayer.setVolume(20,20);

  // Timer interrupts are not suggested, better to use DREQ interrupt!
  //musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT); // timer int

  // If DREQ is on an interrupt pin (on uno, #2 or #3) we can do background
  // audio playing
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);  // DREQ int
}

char read; //The most recent character read

void loop ()
{
   while(RFID.available() > 0)
   {
      read = RFID.read();
      key += read;
      if(key.indexOf("100008004951") >= 0) {
          Serial.println(F("Playing track 001"));
          musicPlayer.playFullFile("track001.mp3");}
        
      if(key.indexOf("100007FFC32B") >= 0) {
          Serial.println(F("Playing track 002"));
          musicPlayer.startPlayingFile("track002.mp3");}
   }
    // File is playing in the background
  if (musicPlayer.stopped()) {
    Serial.println("Done playing music");
    while (1);
  }
  if (Serial.available()) {
    char c = Serial.read();
    
    // if we get an 's' on the serial console, stop!
    if (c == 's') {
      musicPlayer.stopPlaying();
    }
    
    // if we get an 'p' on the serial console, pause/unpause!
    if (c == 'p') {
      if (! musicPlayer.paused()) {
        Serial.println("Paused");
        musicPlayer.pausePlaying(true);
      } else { 
        Serial.println("Resumed");
        musicPlayer.pausePlaying(false);
      }
    }
  }

  delay(100);
}

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Music Maker and RFID?

Post by adafruit_support_rick »

Those are different key numbers than in the earlier example you posted.

Can you post what comes out on Serial Monitor?

User avatar
Inge437
 
Posts: 21
Joined: Wed Apr 15, 2015 3:09 pm

Re: Music Maker and RFID?

Post by Inge437 »

That's correct, when I scanned them again they had a different number (no idea how or why, but these id numbers are correct)

The serial monitor says:

Code: Select all

AdaAdafruit VS1053 Simple Test
VS1053 found
Done playing music
When I try to read the RFID tags, nothing happens.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Music Maker and RFID?

Post by adafruit_support_rick »

Oh! It's here. The first time it goes through loop, there's no RFID available, so it goes to here

Code: Select all

  if (musicPlayer.stopped()) {
    Serial.println("Done playing music");
    while (1);
  }
It prints "Done playing music", and then goes into an infinite loop. That's why it doesn't work. You don't want that infinite loop.

User avatar
Inge437
 
Posts: 21
Joined: Wed Apr 15, 2015 3:09 pm

Re: Music Maker and RFID?

Post by Inge437 »

No, I don't want that, but I don't know how to change this to take it out of the loop. I assume the RFID should be in the loop? I would need help with this by getting the code right.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Music Maker and RFID?

Post by adafruit_support_rick »

You aren't playing files in the background anyway, so just get rid of that entire if statement

Code: Select all

#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
#include <SoftwareSerial.h>

SoftwareSerial RFID = SoftwareSerial(0, 2); //RX is 6, TX is 7
String key; //The key that was scanned

// define the pins used
//#define CLK 13       // SPI Clock, shared with SD card
//#define MISO 12      // Input data, from VS1053/SD card
//#define MOSI 11      // Output data, to VS1053/SD card
// Connect CLK, MISO and MOSI to hardware SPI pins. 
// See http://arduino.cc/en/Reference/SPI "Connections"

// These are the pins used for the breakout example
#define BREAKOUT_RESET  9      // VS1053 reset pin (output)
#define BREAKOUT_CS     10     // VS1053 chip select pin (output)
#define BREAKOUT_DCS    8      // VS1053 Data/command select pin (output)
// These are the pins used for the music maker shield
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)

// These are common pins between breakout and shield
#define CARDCS 4     // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin

Adafruit_VS1053_FilePlayer musicPlayer = 
  // create breakout-example object!
  //Adafruit_VS1053_FilePlayer(BREAKOUT_RESET, BREAKOUT_CS, BREAKOUT_DCS, DREQ, CARDCS);
  // create shield-example object!
  Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
  
void setup ()
{
  Serial.begin(9600);
  RFID.begin(9600); //Begins connection with the reader
  Serial.println("Adafruit VS1053 Simple Test");
  
    if (! musicPlayer.begin()) { // initialise the music player
     Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
     while (1);
  }
    Serial.println(F("VS1053 found"));
  
  SD.begin(CARDCS);    // initialise the SD card
  
  // Set volume for left, right channels. lower numbers == louder volume!
  musicPlayer.setVolume(20,20);

  // Timer interrupts are not suggested, better to use DREQ interrupt!
  //musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT); // timer int

  // If DREQ is on an interrupt pin (on uno, #2 or #3) we can do background
  // audio playing
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);  // DREQ int
}

char read; //The most recent character read

void loop ()
{
   while(RFID.available() > 0)
   {
      read = RFID.read();
      key += read;
      if(key.indexOf("100008004951") >= 0) {
          Serial.println(F("Playing track 001"));
          musicPlayer.playFullFile("track001.mp3");
        }
        
      if(key.indexOf("100007FFC32B") >= 0) {
          Serial.println(F("Playing track 002"));
          musicPlayer.playFullFile("track002.mp3");
        }
   }
    // File is playing in the background
  if (Serial.available()) {
    char c = Serial.read();
    
    // if we get an 's' on the serial console, stop!
    if (c == 's') {
      musicPlayer.stopPlaying();
    }
    
    // if we get an 'p' on the serial console, pause/unpause!
    if (c == 'p') {
      if (! musicPlayer.paused()) {
        Serial.println("Paused");
        musicPlayer.pausePlaying(true);
      } else { 
        Serial.println("Resumed");
        musicPlayer.pausePlaying(false);
      }
    }
  }

  delay(100);
}

User avatar
Inge437
 
Posts: 21
Joined: Wed Apr 15, 2015 3:09 pm

Re: Music Maker and RFID?

Post by Inge437 »

Awesome! :D
I have three more questions. 1) When I scan tag 1, it keeps on playing the song forever. It is impossible to scan another tag.

2) If tag 1 is scanned and song 1 is playing, it should have an option to stop it (without using the keyboard). So basically, if I scan tag 1 again (when song 1 is playing) it should stop the music.

And, when song 1 is playing but I scan tag 2, song 2 should start playing.
I'm not sure if that is possible when it's using the serial.

Because right now, if song 1 is playing and I scan another RFID tag, nothing shows up the in serial monitor. It seems like it is blocking it?

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Music Maker and RFID?

Post by adafruit_support_rick »

OK, that's a little more complicated.
Try this and see if it does what you want:

Code: Select all

#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
#include <SoftwareSerial.h>

SoftwareSerial RFID = SoftwareSerial(0, 2); //RX is 6, TX is 7
String key; //The key that was scanned

// define the pins used
//#define CLK 13       // SPI Clock, shared with SD card
//#define MISO 12      // Input data, from VS1053/SD card
//#define MOSI 11      // Output data, to VS1053/SD card
// Connect CLK, MISO and MOSI to hardware SPI pins. 
// See http://arduino.cc/en/Reference/SPI "Connections"

// These are the pins used for the breakout example
#define BREAKOUT_RESET  9      // VS1053 reset pin (output)
#define BREAKOUT_CS     10     // VS1053 chip select pin (output)
#define BREAKOUT_DCS    8      // VS1053 Data/command select pin (output)
// These are the pins used for the music maker shield
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)

// These are common pins between breakout and shield
#define CARDCS 4     // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin

Adafruit_VS1053_FilePlayer musicPlayer = 
  // create breakout-example object!
  //Adafruit_VS1053_FilePlayer(BREAKOUT_RESET, BREAKOUT_CS, BREAKOUT_DCS, DREQ, CARDCS);
  // create shield-example object!
  Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
  
void setup ()
{
  Serial.begin(9600);
  RFID.begin(9600); //Begins connection with the reader
  Serial.println("Adafruit VS1053 Simple Test");
  
    if (! musicPlayer.begin()) { // initialise the music player
     Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
     while (1);
  }
    Serial.println(F("VS1053 found"));
  
  SD.begin(CARDCS);    // initialise the SD card
  
  // Set volume for left, right channels. lower numbers == louder volume!
  musicPlayer.setVolume(20,20);

  // Timer interrupts are not suggested, better to use DREQ interrupt!
  //musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT); // timer int

  // If DREQ is on an interrupt pin (on uno, #2 or #3) we can do background
  // audio playing
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);  // DREQ int
}

char read; //The most recent character read
int track = -1;  //current track playing.  -1 means not playing anything.

void loop ()
{
   while(RFID.available() > 0)
   {
      read = RFID.read();
      key += read;
      if(key.indexOf("100008004951") >= 0) {
          if (!musicPlayer.stopped())
            musicPlayer.stopPlaying();
          if (track != 1)
          {
            Serial.println(F("Playing track 001"));
            musicPlayer.startPlayingFile("track001.mp3");
            track = 1; //current track is track 1
          }
          else
            track = -1; //not playing anything
        }
        
      if(key.indexOf("100007FFC32B") >= 0) {
          if (!musicPlayer.stopped())
            musicPlayer.stopPlaying();
          if (track != 2)
          {
            Serial.println(F("Playing track 002"));
            musicPlayer.startPlayingFile("track002.mp3");
            track = 2; //current track is track 2
          }
          else
            track = -1;  //not playing anything
        }
   }
    // File is playing in the background
  if (Serial.available()) {
    char c = Serial.read();
    
    // if we get an 's' on the serial console, stop!
    if (c == 's') {
      musicPlayer.stopPlaying();
    }
    
    // if we get an 'p' on the serial console, pause/unpause!
    if (c == 'p') {
      if (! musicPlayer.paused()) {
        Serial.println("Paused");
        musicPlayer.pausePlaying(true);
      } else { 
        Serial.println("Resumed");
        musicPlayer.pausePlaying(false);
      }
    }
  }

  delay(100);
}

User avatar
Inge437
 
Posts: 21
Joined: Wed Apr 15, 2015 3:09 pm

Re: Music Maker and RFID?

Post by Inge437 »

I see what you did. When I scan tag 1, it plays and stop. When I scan another tag, it keeps on playing song 1 and sometimes mixes a bit of song 2. It becomes never-ending.

The serial monitor:

Code: Select all

Adast
VS1053 found
Adafruit VS1053 Simple Test
VS1053 found
Playing track 001
Playing track 001
Playing track 001
Playing track 001
Playing track 001
Playing track 001
Playing track 001
Playing track 001
Playing track 001
Playing track 002
Playing track 001
Playing track 002
Playing track 001
Playing track 002
Playing track 001
Playing track 002
....
....

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Music Maker and RFID?

Post by adafruit_support_rick »

You're using pin 0 for both the RFID and for Serial. You really can't do that. Do you have to have the RFID attached to pins 0 and 2? You can't use different pins?

If you can't use different pins, eliminate the Serial.begin and the other references to Serial, and see what difference that makes, if any.

Code: Select all

#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
#include <SoftwareSerial.h>

SoftwareSerial RFID = SoftwareSerial(0, 2); //RX is 6, TX is 7
String key; //The key that was scanned

// define the pins used
//#define CLK 13       // SPI Clock, shared with SD card
//#define MISO 12      // Input data, from VS1053/SD card
//#define MOSI 11      // Output data, to VS1053/SD card
// Connect CLK, MISO and MOSI to hardware SPI pins. 
// See http://arduino.cc/en/Reference/SPI "Connections"

// These are the pins used for the breakout example
#define BREAKOUT_RESET  9      // VS1053 reset pin (output)
#define BREAKOUT_CS     10     // VS1053 chip select pin (output)
#define BREAKOUT_DCS    8      // VS1053 Data/command select pin (output)
// These are the pins used for the music maker shield
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)

// These are common pins between breakout and shield
#define CARDCS 4     // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin

Adafruit_VS1053_FilePlayer musicPlayer = 
// create breakout-example object!
//Adafruit_VS1053_FilePlayer(BREAKOUT_RESET, BREAKOUT_CS, BREAKOUT_DCS, DREQ, CARDCS);
// create shield-example object!
Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);

void setup ()
{
  RFID.begin(9600); //Begins connection with the reader
//  Serial.println("Adafruit VS1053 Simple Test");

  if (! musicPlayer.begin()) { // initialise the music player
//    Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
    while (1);
  }
//  Serial.println(F("VS1053 found"));

  SD.begin(CARDCS);    // initialise the SD card

  // Set volume for left, right channels. lower numbers == louder volume!
  musicPlayer.setVolume(20,20);

  // Timer interrupts are not suggested, better to use DREQ interrupt!
  //musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT); // timer int

  // If DREQ is on an interrupt pin (on uno, #2 or #3) we can do background
  // audio playing
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);  // DREQ int
}

char read; //The most recent character read
int track = -1;  //current track playing.  -1 means not playing anything.

void loop ()
{
  while(RFID.available() > 0)
  {
    read = RFID.read();
    key += read;
    if(key.indexOf("100008004951") >= 0) {
      if (!musicPlayer.stopped())
        musicPlayer.stopPlaying();
      if (track != 1)
      {
 //       Serial.println(F("Playing track 001"));
        musicPlayer.startPlayingFile("track001.mp3");
        track = 1; //current track is track 1
      }
      else
        track = -1; //not playing anything
    }

    if(key.indexOf("100007FFC32B") >= 0) {
      if (!musicPlayer.stopped())
        musicPlayer.stopPlaying();
      if (track != 2)
      {
//        Serial.println(F("Playing track 002"));
        musicPlayer.startPlayingFile("track002.mp3");
        track = 2; //current track is track 2
      }
      else
        track = -1;  //not playing anything
    }
  }


  delay(100);
}

User avatar
Inge437
 
Posts: 21
Joined: Wed Apr 15, 2015 3:09 pm

Re: Music Maker and RFID?

Post by Inge437 »

Ah! No, I can use any pin that the Music Maker Shield is not using. It is not bounded to 0 and 2. I will give it a try!

User avatar
Inge437
 
Posts: 21
Joined: Wed Apr 15, 2015 3:09 pm

Re: Music Maker and RFID?

Post by Inge437 »

I changed the RFID puns to 2 and 5 (and 5 and 2) but it didn't do the trick.
It still keeps on looping the spng. First try is okey, but reading it again (or another one) is causing problems. It will play the first song until eternity and mixes another song into it.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Music Maker and RFID?

Post by adafruit_support_rick »

Ah. You are concatenating each key you read into one big long string:

Code: Select all

      key += read;
So, the string 'key' will always contain whatever key you read, and the tests will always pass. What you need to do is reset 'key' when you detect an entire key.

You don't actually need the char 'read' either. And I added the reserve function to make the String key a little more memory efficient:

Code: Select all

#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
#include <SoftwareSerial.h>

SoftwareSerial RFID = SoftwareSerial(0, 2); //RX is 6, TX is 7
String key; //The key that was scanned

// define the pins used
//#define CLK 13       // SPI Clock, shared with SD card
//#define MISO 12      // Input data, from VS1053/SD card
//#define MOSI 11      // Output data, to VS1053/SD card
// Connect CLK, MISO and MOSI to hardware SPI pins. 
// See http://arduino.cc/en/Reference/SPI "Connections"

// These are the pins used for the breakout example
#define BREAKOUT_RESET  9      // VS1053 reset pin (output)
#define BREAKOUT_CS     10     // VS1053 chip select pin (output)
#define BREAKOUT_DCS    8      // VS1053 Data/command select pin (output)
// These are the pins used for the music maker shield
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)

// These are common pins between breakout and shield
#define CARDCS 4     // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin

Adafruit_VS1053_FilePlayer musicPlayer = 
// create breakout-example object!
//Adafruit_VS1053_FilePlayer(BREAKOUT_RESET, BREAKOUT_CS, BREAKOUT_DCS, DREQ, CARDCS);
// create shield-example object!
Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);

void setup ()
{
  RFID.begin(9600); //Begins connection with the reader
  key.reserve(16);
  Serial.begin(9600);
  Serial.println("Adafruit VS1053 Simple Test");

  if (! musicPlayer.begin()) { // initialise the music player
    Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
    while (1);
  }
  Serial.println(F("VS1053 found"));

  SD.begin(CARDCS);    // initialise the SD card

  // Set volume for left, right channels. lower numbers == louder volume!
  musicPlayer.setVolume(20,20);

  // Timer interrupts are not suggested, better to use DREQ interrupt!
  //musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT); // timer int

  // If DREQ is on an interrupt pin (on uno, #2 or #3) we can do background
  // audio playing
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);  // DREQ int
}

int track = -1;  //current track playing.  -1 means not playing anything.

void loop ()
{
  while(RFID.available() > 0)
  {
    key += RFID.read();;
    if(key.indexOf("100008004951") >= 0) {
      if (!musicPlayer.stopped())
        musicPlayer.stopPlaying();
      if (track != 1)
      {
        Serial.println(F("Playing track 001"));
        musicPlayer.startPlayingFile("track001.mp3");
        track = 1; //current track is track 1
      }
      else
        track = -1; //not playing anything
      
      key = "";
    }

    if(key.indexOf("100007FFC32B") >= 0) {
      if (!musicPlayer.stopped())
        musicPlayer.stopPlaying();
      if (track != 2)
      {
        Serial.println(F("Playing track 002"));
        musicPlayer.startPlayingFile("track002.mp3");
        track = 2; //current track is track 2
      }
      else
        track = -1;  //not playing anything
    }
    
    key = "";
  }

  delay(100);
}

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”