Play Pause Skip Buttons rmp3 help

HI Jennifer,

intriged by the application I spend some time tinkering with your sketch. I can't compile it as I dont have the shield or the libs
-- update --> Found the libs, now it compiles under IDE 0022. But still don't have the shield ....

What I did:

  • added comments
  • moved most vars to global
  • moved initialization code from loop() to setup()
  • moved lots of code from the loop() to separate functions
  • added code for three buttons PIN 3,4,5

These three button must be connected to GND on one side and to their pin on the other side (can be tested just with a wire)

Give it a try

#include <RogueMP3.h>
#include <SoftwareSerial.h>
#include <RogueSD.h>


#define MP3PATH "/mp3/"
// you need to put the two double quotes ("") in the middle below,
// because the Arduino IDE looks for unclosed comments.
#define MP3FILTER "/mp3/""*.mp3"

// EQUALIZER 
#define MAXBANDS 23
#define THRESHOLD 1 


// PINNUMBERS FOR BUTTONS
#define NEXTPIN   3
#define PAUSEPIN  4
#define STOPPIN   5


// MP3SHIELD VARS
SoftwareSerial rmp3_s(6, 7);
RogueSD filecommands((Stream&)rmp3_s);
RogueMP3 rmp3((Stream&)rmp3_s);


// EQUALIZER VARS
// TODO size of arrays ???? <<<<<<<<<<<<<<<
#define numberOfBands 6
uint16_t bandfreqs[] = { 
  300, 1000, 9000 };
uint8_t outputPin[] = { 
  11, 12, 13 };


// SONG ADMIN VARS
int numberOfSongs = 0;
int lastSong = -1;
int nextSong = 0;


// FILENAME 
char filename[80];
char path[96];


// STATUS VARS
char status = 'S';
uint8_t playing = 1;
uint8_t volume = 20;
uint8_t boostOn = false;
playbackinfo playinfo;


///////////////////////////////////////////////////////////////////////////////////
//
// SETUP 
//
void setup(void)
{
  // COMMUNICATION
  Serial.begin(9600);
  Serial.println("Started");

  rmp3_s.begin(9600);

  // EQUALIZER
  for (uint8_t i = 0; i < numberOfBands; i++)
  {
    pinMode(outputPin[i], OUTPUT);
  }

 // BUTTONS
  pinMode(NEXTPIN, INPUT);
  pinMode(PAUSEPIN  , INPUT);
  pinMode(STOPPIN   , INPUT);
  digitalWrite(NEXTPIN, HIGH);
  digitalWrite(PAUSEPIN  , HIGH);
  digitalWrite(STOPPIN   , HIGH);
  
  Serial.println("Starting sync");
  rmp3.sync();
  Serial.println("Done sync");
  rmp3.stop();
  filecommands.sync();

  // INIT
  checkFrequencies();

  filecommands.opendir(MP3PATH);
  {
    filecommands.readdir(filename, "*.mp3");
  }
  numberOfSongs = filecommands.filecount(MP3FILTER);

  volume = rmp3.getvolume();  // this only gets the left volume

    // ALLWAYS START PLAYING
  playNextSong();
}

///////////////////////////////////////////////////////////////////////////////////
//
// LOOP (endless until power down ... 
//
void loop(void)
{
  // CHECK FOR SERIAL COMMANDS
  if (Serial.available() > 0)
  {
    char c = Serial.read();
    switch(c)
    {
    case 'p': 
      pause(); 	
      break;
    case 's': 
      stop();	
      break;
    case 'n': 
      next();	
      break;
    case 'e': 
      boost();	
      break;
    case 'a': 
      jump(-5);	
      break;
    case 'd': 
      jump(+5); 
      break;
    case 'k': 
      setVolume(+1); 
      break;
    case 'i': 
      setVolume(-1); 
      break;
    }
  }

  // CHECK FOR BUTTONS
  if (digitalRead(NEXTPIN) == LOW) next();
  if (digitalRead(PAUSEPIN) == LOW) pause();
  if (digitalRead(STOPPIN) == LOW) stop();


  // we should do fancy stuff like flash lights on our Xmas tree here!
  // got lots of time!
  // playinfo = rmp3.getplaybackinfo();


  // if song ended play next one
  status = rmp3.getplaybackstatus();
  if (status == 'S' && playing)
  {
    playNextSong();
  }
}


///////////////////////////////////////////////////////////////////////////////////
//
// TACTILE EQUALIZER I/O
// 
void checkFrequencies(void)
{
  uint8_t v[MAXBANDS];
  rmp3.getspectrumanalyzer(v);
  for (uint8_t i=0; i<numberOfBands; i++)
  {
    if (v[i] > THRESHOLD)
      digitalWrite(outputPin[i], HIGH);
    else
      digitalWrite(outputPin[i], LOW);
  }
}


///////////////////////////////////////////////////////////////////////////////////
//
// MP3 FUNCTIONS
//

void playNextSong()
{
  if (numberOfSongs == 0)
  {
    Serial.println("No files to play.");
    return;
  }

  // randomize
  nextSong = random(numberOfSongs);
  if (nextSong == lastSong)
  {
    nextSong = (nextSong + 1) % numberOfSongs;
  }
  lastSong = nextSong;

  // now, get filename from list
  filecommands.opendir(MP3PATH);
  for (int i = 0; i <= nextSong; i++)
  {
    filecommands.readdir(filename, "*.mp3");
  }
  strcpy(path, MP3PATH);
  strcat(path, filename);

  // and play
  rmp3.playfile(path);

  Serial.print("Playing: ");
  Serial.println(path);
}

int8_t isPlaying(void)
{
  return (rmp3.getplaybackstatus() == 'P' ? 1 : 0);
}

void setVolume(int v)
{
  volume = volume + v;
  volume = constrain(volume, 0, 254);
  if(status != 'D') rmp3.setvolume(volume);
}

void jump(int sec)
{
  uint16_t newtime = max(0, playinfo.position + sec);
  rmp3.jump(newtime);
}

void boost()
{
  if(boostOn)
  {
    rmp3.setboost(0);
    boostOn = false;
    return;
  }
  rmp3.setboost(8, 6, 7, 3);
  boostOn = true;
}

void next()
{
  playNextSong();
  playing = 1;
}

void stop()
{
  rmp3.stop();
  playing = 0;
}

void pause()
{
  status = rmp3.getplaybackstatus();
  if (status == 'D')
  {
    // fade in
    rmp3.playpause();
    rmp3.fade(volume, 400);
    return;
  }
  if (status == 'P')
  {
    // fade out
    rmp3.fade(100, 400);
    rmp3.playpause();
    return;
  }
  // start playing
  playNextSong();
  playing = 1;
}