
#include <Fat16.h>
#include <Fat16Util.h>
#include <NewSPI.h>
#include <arduino.h>
#include "pins_config.h"
#include "vs10xx.h"
#include "newSDLib.h"
#include "MusicPlayer.h"

//Music player initiation
MusicPlayer myplayer;

//Variables
char title[8];  //holds the assembled music track title
long trackNum;  //holds the randomized track name
boolean switched = LOW;     // tells if switch has been thrown

void setup()
{
  //Serial.begin(9600);
  myplayer.keyDisable();
  myplayer.digitalControlEnable();
  myplayer.begin();//will initialize the hardware and set default mode to be normal.
  myplayer.attachDigitOperation(3, randomSong, HIGH);
  randomSeed(analogRead(4));
  String base = "0";
  trackNum = random(1, 76);
  base.concat(trackNum);
  base.concat(".mp3");
  base.toCharArray(title,9);
  //Serial.println(title);
}
void loop()
{
  myplayer.setPlayMode(MODE_NORMAL);//set mode to repeat to play a song
  myplayer.playSong(title);//play a song named with test.wav
  switched = LOW;
  String base = "0";
  trackNum = random(1, 76);
  base.concat(trackNum);
  base.concat(".mp3");
  base.toCharArray(title,9);
  //Serial.println(title);
}

void randomSong()
  {
    //Serial.println("switch");
    while(digitalRead(3)==HIGH)
    {
      delay(10);
    }
    if (switched == LOW){
      playingState = PS_STOP;
      trackNum = random(1, 101);
      //Serial.println(trackNum);
      String newBase = "0";
      newBase.concat(trackNum);
      newBase.concat(".mp3");
      newBase.toCharArray(title,9);
      //Serial.println(title);
      myplayer.addToPlaylist(title);
      playingState = PS_NEXT_SONG;
      switched = HIGH;
    }

}
