How to play mp3 file when device moves using an acceleromete

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
jvan27
 
Posts: 8
Joined: Mon Nov 24, 2014 1:34 am

How to play mp3 file when device moves using an acceleromete

Post by jvan27 »

Hello! I am a maker beginner and I need some help! :(

I am trying to make a device that detects when it is moving and then plays a song from the computer. Right now I have an accelerometer from adafruit which is attached to the adafruit flora. the information is being displayed through the serial monitor and now I am struggling with trying to play an mp3 file when it hits a certain coordinate that is being displayed in the serial monitor.

does anyone know how they would go about it? Is there any tutorials online that I can search from?

I know it may be a simple question but I am not sure what to do...

Thank you!

User avatar
adafruit_support_bill
 
Posts: 88144
Joined: Sat Feb 07, 2009 10:11 am

Re: How to play mp3 file when device moves using an accelero

Post by adafruit_support_bill »

For playing MP3s, you would want something like the VS1053 breakout or the Music-Maker shield. I'd start by hooking one of those up and getting it to play sounds using the example code from the library.

From there, to add in code to monitor the accelerometer and trigger a sound when you reach some acceleration threshold would be fairly straightfroward.

User avatar
jvan27
 
Posts: 8
Joined: Mon Nov 24, 2014 1:34 am

Re: How to play mp3 file when device moves using an accelero

Post by jvan27 »

Hey!

Sorry for the late reply, but I have gotten quite far in figuring this thing out lol. I have hooked up both the accelerometer and the vs1053 breakout shield successfully to an arduino uno. right now I am working on the code and I am having trouble trying to get both devices to run in the serial monitor. I added both the devices code into one document and it can run with no errors but it only shows the vs1053 in the serial monitor. As I might of mentioned before, I am a beginner so if the answer is an easy one I apologize lol.

Thank you. Here is the code:

Code: Select all

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

#include <Wire.h>
#include <Adafruit_LSM303.h>

Adafruit_LSM303 lsm;

// 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_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_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);
     
     if (!lsm.begin())
      {
    Serial.println("Oops ... unable to initialize the LSM303. Check your wiring!");
    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() {
  
  lsm.read();
  Serial.print("Accel X: "); Serial.print((int)lsm.accelData.x); Serial.print(" ");
  Serial.print("Y: "); Serial.print((int)lsm.accelData.y);       Serial.print(" ");
  Serial.print("Z: "); Serial.println((int)lsm.accelData.z);     Serial.print(" ");
  Serial.print("Mag X: "); Serial.print((int)lsm.magData.x);     Serial.print(" ");
  Serial.print("Y: "); Serial.print((int)lsm.magData.y);         Serial.print(" ");
  Serial.print("Z: "); Serial.println((int)lsm.magData.z);       Serial.print(" ");
  delay(1000);
  
  
  // 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_bill
 
Posts: 88144
Joined: Sat Feb 07, 2009 10:11 am

Re: How to play mp3 file when device moves using an accelero

Post by adafruit_support_bill »

Can you post a sample of the serial output?

User avatar
jvan27
 
Posts: 8
Joined: Mon Nov 24, 2014 1:34 am

Re: How to play mp3 file when device moves using an accelero

Post by jvan27 »

so whats being displayed in the serial monitor? Only the vs1053 runs in the serial monitor and it reads....
Adafruit VS1053 Simple Test
VS1053 found
Playing track 001
which is what I want but I also want the accelerometer to display information as well. Is it even possible to have two devices run in the serial monitor in arduino?

User avatar
adafruit_support_bill
 
Posts: 88144
Joined: Sat Feb 07, 2009 10:11 am

Re: How to play mp3 file when device moves using an accelero

Post by adafruit_support_bill »

Does Track 1 complete?

The code plays track 1 in the foreground, so nothing else can run until it completes.

Then it starts track 2 playing in the background. The loop should run while it is doing that.

Code: Select all

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

User avatar
jvan27
 
Posts: 8
Joined: Mon Nov 24, 2014 1:34 am

Re: How to play mp3 file when device moves using an accelero

Post by jvan27 »

I dont think I explained myself well enough earlier but I did manage to figure it out, but I still have another problem. I got the accelerometer to take measurements on how fast you are moving, and then depending on how fast you are going (fast, medium, slow), it would play a particular song using the vs1053 breakout. My problem is that it measures and says that you are going slow and starts playing the song but once the new measurement comes up it (after 1 second) it starts to play the song again. What I want it to do is to figure out how fast you are going, play the correct song, and then keep measuring an recognize that the song is already playing and keep playing. I know that you need a boolean of some kind but I am not sure how to write it. The library references says "boolean startPlayingFile(char *trackname) - Begin playing the specified file from the SD card using interrupt-driven playback. This allows your program to perform other tasks as the file is playing." but there is no example on how to do this. How do i tell the if statement that the song is already playing?

heres the updated code:

Code: Select all

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

// 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_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_CS, SHIELD_DCS, DREQ, CARDCS);

#include <Wire.h>
#include <Adafruit_LSM303.h>

Adafruit_LSM303 lsm;

#define FAST_THRESHOLD 70
#define SLOW_THRESHOLD 30 

void setup() 
{
  Serial.begin(9600);
  Serial.println("Make my life into a movie!");
  
  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);
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT); 
  
  // Try to initialise and warn if we couldn't detect the chip
  if (!lsm.begin())
  {
    Serial.println("Oops ... unable to initialize the LSM303. Check your wiring!");
    while (1);
  }
  Serial.println(F("Accelerometer LSM303 found"));
}

boolean running = false;

void loop() 
{
  lsm.read();
  Serial.print("Accel X: "); Serial.print(lsm.accelData.x); Serial.print(" ");
  Serial.print("Y: "); Serial.print(lsm.accelData.y);       Serial.print(" ");
  Serial.print("Z: "); Serial.print(lsm.accelData.z);     Serial.print(" ");
 
  // Get the magnitude (length) of the 3 axis vector
  // http://en.wikipedia.org/wiki/Euclidean_vector#Length
  double storedVector = lsm.accelData.x*lsm.accelData.x;
  storedVector += lsm.accelData.y*lsm.accelData.y;
  storedVector += lsm.accelData.z*lsm.accelData.z;
  storedVector = sqrt(storedVector);
  
  // wait a bit
  delay(1000);
  
  // get new data!
  lsm.read();
  double newVector = lsm.accelData.x*lsm.accelData.x;
  newVector += lsm.accelData.y*lsm.accelData.y;
  newVector += lsm.accelData.z*lsm.accelData.z;
  newVector = sqrt(newVector);
  
  if (abs(newVector - storedVector) <= SLOW_THRESHOLD) {
    Serial.println("MOVING SLOW!!");
    musicPlayer.startPlayingFile("track001.mp3");
    running = !running;
  } else if (abs(newVector - storedVector) >= FAST_THRESHOLD) {
    Serial.println("MOVING FAST!!");
    musicPlayer.startPlayingFile("track003.mp3");
  } else {
    Serial.println("MOVING MEDIUM!!");
    musicPlayer.startPlayingFile("track002.mp3");  
  }
}

User avatar
adafruit_support_bill
 
Posts: 88144
Joined: Sat Feb 07, 2009 10:11 am

Re: How to play mp3 file when device moves using an accelero

Post by adafruit_support_bill »

This allows your program to perform other tasks as the file is playing." but there is no example on how to do this.
See this example: https://github.com/adafruit/Adafruit_VS ... rrupts.ino
How do i tell the if statement that the song is already playing?
Use the "playingMusic" property of the music player:

Code: Select all

  while (musicPlayer.playingMusic) {
    // file is now playing in the 'background' so now's a good time
    // to do something else like handling LEDs or buttons :)
    Serial.print(".");
    delay(1000);
  }

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

Return to “Arduino”