MP3 Shield coding

Hello, I'm not very good at programming arduino (still learning). But I need a code for my project with a MP3 Shield. I have a song (track0001.mp3) saved on my SD card and want to play that song when a motion sensor detects movement. The song should play repeatedly when movement is detected and should stop 2 minutes after the last detected movement.

I can find some examples of standard codes for a MP3 shield on the internet, but I need a code for my project and since i'm not a experienced programmer I was hoping wheter you guys can help me with this? :roll_eyes:

Thanks in advance!

you guys can help me with this?

How much are you paying, and when is our homework dues?

Detecting when motion IS being sensed, when it BECOMES sensed, and when it BECOMES not sensed, are three different things. Which one(s) are of interest to you? What have you been able to to so far, to discover that motion is sensed?

Noticing when motion BECOMES sensed is illustrated in the state change detection example.

Noticing what time that happens, and doing something (or stopping doing something) some fixed time later is illustrated in the blink without delay example.

No paying i'm afraid, just a 'poor' student trying to learn...

I have changed my plan a little bit. I want the arduino to play music when a push button sends a LOW signal. The song should play for 2 minutes from that moment. After the 2 minutes the song should stop when the signal is HIGH, but should repeat the song when the signal is LOW (just 1 song on SD card). This is what I have so far:

#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>

SFEMP3Shield MP3player;
SdFat sd;
int8_t current_track = 0;

const int buttonPin = 0;
unsigned long playTime = 120000; // how long a file will play in milliseconds (15000=15 seconds)
unsigned long pauseTime = 5000; // how long the pause will be after the sound ends (10000=10 seconds)
int readingInterval = 500; // how often to read the sensor

// changing variables

unsigned long currentMillis = 0; // time variable to track running time: current time
unsigned long startingMillis = 0; // time variable to track running time: starting time

// setup

void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT);

MP3player.begin();
MP3player.playTrack(1);
MP3player.setVolume(10, 10);
Serial.println(F(""));
}

void loop(){
if (digitalRead(buttonPin)== HIGH) {
Serial.print(F("track001.mp3"));
Serial.println(current_track);
MP3player.playTrack(current_track);
playtime();
MP3player.stopTrack();
delay(pauseTime);
}

delay(readingInterval);
}

void playtime() {
startingMillis = millis(); // set both time counter
currentMillis = millis();
while ( currentMillis - startingMillis < playTime ) { // while track plays, runs until playTime is reached
currentMillis = millis(); // set to actual time
delay(40); // debounce
}
}

I have produced this by combining some existing codes and adding something by myself (understand the code globally, not every line).

When I run this I get no faults, but also no music =(

Can you guys help me please?

I want the arduino to play music when a push button sends a LOW signal.

  MP3player.playTrack(1);

Why are you telling the device to play the track, in setup()?

      if (digitalRead(buttonPin)== HIGH) {   
Serial.print(F("track001.mp3"));
Serial.println(current_track);
MP3player.playTrack(current_track);
  1. Doesn't look like a LOW to me.
  2. The farting around with "track001.mp3" and printing current_track anonymously do nothing useful.
  void playtime() {
       startingMillis = millis();                                 // set both time counter
       currentMillis = millis();
      while ( currentMillis - startingMillis < playTime ) {      // while track plays, runs until playTime is reached
          currentMillis = millis();                               // set to actual time
          delay(40);                                              // debounce
       }     
   }

A big fat delay()!

When I run this I get no faults, but also no music

Then, you need a really simple sketch. What should loop() look like:

void loop()
{
}

That's right. You do NOTHING in loop(). Not a thing. Make setup() play the track. Do not add any more code until you know that the hardware is working.

First of all, thanks for the reply.

  1. Doesn't look like a LOW to me.

I'm aware of that, just did it for testing

That's right. You do NOTHING in loop(). Not a thing. Make setup() play the track. Do not add any more code until you know that the hardware is working.

Allright, so de code should look something like this?

#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>

SFEMP3Shield MP3player;
SdFat sd;
int8_t current_track = 0;

const int buttonPin = 0;
unsigned long playTime = 120000; // how long a file will play in milliseconds (15000=15 seconds)
unsigned long pauseTime = 5000; // how long the pause will be after the sound ends (10000=10 seconds)
int readingInterval = 500; // how often to read the sensor

// changing variables

unsigned long currentMillis = 0; // time variable to track running time: current time
unsigned long startingMillis = 0; // time variable to track running time: starting time

// setup

void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT);

MP3player.begin();
MP3player.playTrack(1);
MP3player.setVolume(10, 10);
Serial.println(F(""));

if (digitalRead(buttonPin)== LOW) {
Serial.print(F());
Serial.println();
MP3player.playTrack();
playtime();
MP3player.stopTrack();
delay(pauseTime);
}

delay(readingInterval);
}

void playtime() {
startingMillis = millis(); // set both time counter
currentMillis = millis();
while ( currentMillis - startingMillis < playTime ) { // while track plays, runs until playTime is reached
currentMillis = millis(); // set to actual time
delay(5); // debounce
}
}
void loop(){ }

Allright, so de code should look something like this?

No. There is no need to concern yourself with what happens after the song ends, until you get to hear some noise. So, ditch the delay() function you wrote, and anything after you tell the device to play a track.

Since you aren't reimplementing delay(), most of your global variables go away. Since you are NOT reading a switch, all that crap goes away, too.

#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>

SFEMP3Shield MP3player;
SdFat sd;

void setup()
{
    Serial.begin(115200);
 
    MP3player.begin(); 
    MP3player.setVolume(10, 10);
    MP3player.playTrack(1); 
}

void loop()
{
}
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>

SFEMP3Shield MP3player;
SdFat sd;

void setup()
{
    Serial.begin(115200);
 
    MP3player.begin(); 
    MP3player.setVolume(10, 10);
    MP3player.playTrack(1); 
}

void loop()
{
}

So when I upload this code to the arduino it should start playing track 1 when it has power? or how should I start it?

I know that the hardware is fine because when i upload the 'fileplayer' example code from the SFEMP3Shield library I can let the song play with the seriel monitor

I know that the hardware is fine because when i upload the 'fileplayer' example code from the SFEMP3Shield library

Which you haven't posted, or posted a link to.

Link:

That example uses MP3player.playMP3(filename); to play the file by name. You are trying to use MP3player.playTrack().
What are the FULL names of all the files on your SD card?

I only have 1 file on the SD card by the name 'track001.mp3'