0

Adafruit Music Maker Shield: Repeat Track Indefinitely
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Adafruit Music Maker Shield: Repeat Track Indefinitely

by caleb_churchill on Wed Jul 21, 2021 3:09 pm

Anyone have a tip on how to get this single track to repeat after it has played through once?

Basically this is code for a "telephone" that plays a track when you lift the receiver. Buttons works and it plays the track as needed, however once the track plays through it stops. I would like it to loop indefinitely.

Here's what I've got so far...

Code: Select all | TOGGLE FULL SIZE
/***************************************************
As this is not a redistribution, the text did not need to be kept.
 ****************************************************/

// 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_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

unsigned long sleepTime = 600000; //ten minutes of music
unsigned long playTime = 0;

int vol = 15; //louder = lower

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

  if (!SD.begin(CARDCS)) {
    Serial.println(F("SD failed, or not present"));
    while (1);  // don't do anything more
  }

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

  // 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.startPlayingFile("/track001.mp3");
  //musicPlayer.sineTest(0x44,500); //make a tone to indicate that switch is working
  boolean playingMusic = true;
  musicPlayer.GPIO_pinMode(1, INPUT);
}

void loop() {
  for (uint8_t i = 0; i < 8; i++) {

    if (musicPlayer.GPIO_digitalRead(1) == LOW) {
      Serial.println("stopped");
      musicPlayer.pausePlaying(true);
    }

    if (musicPlayer.GPIO_digitalRead(1) == HIGH) {
      Serial.println("play");
      musicPlayer.pausePlaying(false);
      playTime = millis();

      if (playTime > sleepTime) {
        musicPlayer.startPlayingFile("/track001.mp3");
        delay(10);
      }
    }
    delay(5);
  }
}

caleb_churchill
 
Posts: 2
Joined: Thu Jun 03, 2021 12:52 pm

Re: Adafruit Music Maker Shield: Repeat Track Indefinitely

by caleb_churchill on Wed Jul 21, 2021 7:22 pm

I did some tinkering and cleaned up the code, so here is an updated version.

Code: Select all | TOGGLE FULL SIZE
// 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_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

unsigned long sleepTime = 300000; //five minutes of music
unsigned long playTime = 0;

int vol = 15; //louder = lower

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

void setup() {
  Serial.begin(9600);
  Serial.println("B+C Sky Phone copyright 2021");

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

  if (!SD.begin(CARDCS)) {
    Serial.println(F("SD failed, or not present"));
    while (1);  // don't do anything more
  }

  // 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.startPlayingFile("/track001.mp3");
}

void loop() {

  if (musicPlayer.GPIO_digitalRead(1) == LOW) {
    Serial.println("Paused");
    musicPlayer.pausePlaying(true);
  } if (musicPlayer.GPIO_digitalRead(1) == HIGH) {
    Serial.println("Resumed");
    musicPlayer.pausePlaying(false);
  }

  delay(200);
}

/// File listing helper
void printDirectory(File dir, int numTabs) {
  while (true) {

    File entry =  dir.openNextFile();
    if (! entry) {
      // no more files
      //Serial.println("**nomorefiles**");
      break;
    }
    for (uint8_t i = 0; i < numTabs; i++) {
      Serial.print('\t');
    }
    Serial.print(entry.name());
    if (entry.isDirectory()) {
      Serial.println("/");
      printDirectory(entry, numTabs + 1);
    } else {
      // files have sizes, directories do not
      Serial.print("\t\t");
      Serial.println(entry.size(), DEC);
    }
    entry.close();
  }
}

caleb_churchill
 
Posts: 2
Joined: Thu Jun 03, 2021 12:52 pm

Please be positive and constructive with your questions and comments.