• If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Social distancing? Try a better way to work remotely on your online files. Dokkio, a new product from PBworks, can help your team find, organize, and collaborate on your Drive, Gmail, Dropbox, Box, and Slack files. Sign up for free.


Harrymj Lab 6

Page history last edited by Harry Johnson 7 years ago


Describe the tone pattern you hear (ie, high or low?  how long? etc)

Intermittent scale. 


Tell a member of the teaching team what your song is (or better yet, play it for us). If you don't know the name, just decipher some of the lyrics.

Manamana, Muppet Show.


Does this code play all the songs in all the directories of the SD card? If not, which songs does it play? How does it keep from playing the text file?

No, it only plays from files marked with the file extension .mp3 or .wav located in the root directory.


Change the 'song' code to save the current volume setting in your EEPROM and fetch and set that value during initialization.  Note that the first max_num_songs*max_name_len (in this case, 520) bytes of the EEPROM are used to store file names, so don't overwrite those. 



Inserted these lines in setup:

  EEPROM.write(521, mp3_vol);



Now, take what you learned to make an interrupt driven pause function for your Barebones MP3 player! 

#include <SD.h>

#include <EEPROM.h>

#include <SPI.h>

#include <VS1053.h>


VS1053 myVS(A1, A2, A3, -1);


#define max_title_len 60

#define max_name_len  13

#define max_num_songs 40


#define read_buffer  512

#define mp3_vol      50


#define sd_cs         SS


File sd_file;


unsigned char num_songs = 0, current_song = 0;


char fn[max_name_len];


char title[max_title_len + 1];


enum state { DIR_PLAY, MP3_PLAY, PAUSED };

volatile state current_state = DIR_PLAY;



void setup()





// while(!Serial);






  EEPROM.write(521, mp3_vol);


  pinMode(2, INPUT_PULLUP);

  attachInterrupt(1, pause_ISR, FALLING);

// Serial.println("1");





void dir_play() {

  if (sd_file) {



//    mp3_play();


  else {

    // since 'sd_file' isn't open, the recently playing song must have ended.

    // increment the index, and open the next song, unless it's the last song

    // in the directory. in that case, just set the state to PAUSED.


    if (current_song < (num_songs - 1)) {

      Serial.println("Selecting song");




      current_state = MP3_PLAY;


    else {

      current_state = PAUSED;





void mp3_play() {

  unsigned char bytes[read_buffer]; // buffer to read and send to the decoder

  unsigned int bytes_to_read;       // number of bytes read from microsd card


  // first fill the 'bytes' buffer with (up to) 'read_buffer' count of bytes.

  // that happens through the 'sd_file.read()' call, which returns the actual

  // number of bytes that were read (which can be fewer than 'read_buffer' if

  // at the end of the file). then send the retrieved bytes out to be played.


  // 'sd_file.read()' manages the index pointer into the file and knows where

  // to start reading the next batch of bytes. 'Mp3.play()' manages the index

  // pointer into the 'bytes' buffer and knows how to send it to the decoder.


  bytes_to_read = sd_file.read(bytes, read_buffer);

// Serial.println(millis());

  myVS.playChunk(bytes, bytes_to_read);


  // 'bytes_to_read' is only smaller than 'read_buffer' when the song's over.


  if (bytes_to_read < read_buffer) {




    // if we've been in the MP3_PLAY state, then we want to pause the player.


  //  if (current_state == MP3_PLAY) {

      current_state == PAUSED;





void loop()


  switch(current_state) {


    case DIR_PLAY:

     // Serial.println("DIR_PLAY");




    case MP3_PLAY:

    //  Serial.println("MP3_PLAY");




    case PAUSED:

     // Serial.println("PAUSED");



 // delay(100);



void pause_ISR() {

  if(current_state == MP3_PLAY) { current_state = PAUSED; Serial.println("Pausing"); return; }

  if(current_state == PAUSED) { current_state = MP3_PLAY; Serial.println("Playing"); return; }




Comments (0)

You don't have permission to comment on this page.