Hello,
It has been a while since I have last been here, and have my project running. I have made an mp3 player that analyzes music for frequency and causes a motor to vibrate depending on the frequency that is player, so you can "feel" the music. The project can be seen here http://jennifercrossley.com
I am now trying to get the project to the next step, and need to to be able to "play/pause" and skip track. The white buttons on the device are supposed to be doing this. I have what I think to be the correct code, and have it in my code where I think it should be, I just don't know if it is correct, and how I should wire the hardware buttons to my arduino?
#include <RogueMP3.h>
#include <SoftwareSerial.h>
#include <RogueSD.h>
SoftwareSerial rmp3_s(6, 7);
RogueSD filecommands(rmp3_s);
RogueMP3 rmp3(rmp3_s);
int numberOfSongs;
int lastSong;
#define numberOfBands 6
uint16_t bandfreqs[] = { 300, 1000, 9000 };
//uint8_t outputPin[] = { 2, 3, 4 };
uint8_t outputPin[] = { 11, 12, 13 };
uint16_t numfiles = 0;
#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"
#define THRESHOLD 1
//#define songname "/gold.mp3"
void checkFrequencies(void)
{
// prepare for up to 23 bands
uint8_t v[23];
rmp3.getspectrumanalyzer(v);
for (uint8_t i=0; i<numberOfBands; i++)
{
if (v[i] > THRESHOLD)
digitalWrite(outputPin[i], HIGH);
else
digitalWrite(outputPin[i], LOW);
}
}
void playNextSong()
{
uint8_t i;
char filename[80];
char path[96];
int nextSong = 0;
if (numberOfSongs > 0)
{
// Select our next song randomly
if (numberOfSongs > 2)
{
do
nextSong = random(numberOfSongs);
while (nextSong == lastSong);
}
else if (numberOfSongs == 2)
{
// we only have two songs
if (lastSong == 0)
nextSong = 1;
else
nextSong = 0;
}
// now, get our file name from file list
filecommands.opendir(MP3PATH);
for (i = 0; i <= nextSong; i++)
{
filecommands.readdir(filename, "*.mp3");
}
strcpy(path, MP3PATH);
strcat(path, "/");
strcat(path, filename);
rmp3.playfile(path);
Serial.print("Playing: ");
Serial.println(path);
lastSong = nextSong;
}
else
{
Serial.println("No files to play.");
}
}
void setup(void)
{
Serial.begin(9600);
Serial.println("Started");
rmp3_s.begin(9600);
for (uint8_t i = 0; i < numberOfBands; i++)
pinMode(outputPin[i], OUTPUT);
Serial.println("Starting sync");
rmp3.sync();
Serial.println("Done sync");
rmp3.stop();
filecommands.sync();
}
//void playTrack(void)
//{
// char mp3path[128];
//
// numfiles = filecommands.filecount(MP3FILTER);
//
// Serial.print("MP3 count: ");
// Serial.println(numfiles, DEC);
//
// // play a file (random)
// strcpy(mp3path, MP3PATH);
// filecommands.entrytofilename(mp3path + strlen(mp3path), 127, MP3FILTER, random(0, numfiles));
//
// Serial.println(mp3path);
// rmp3.playfile(mp3path);
//
// rmp3.setspectrumanalyzer(bandfreqs, 10);
//
// Serial.println("Playing");
//}
int8_t isPlaying(void)
{
return (rmp3.getplaybackstatus() == 'P' ? 1 : 0);
}
void loop(void)
{
//if (!isPlaying())
//playTrack();
checkFrequencies();
uint8_t i;
char filename[80];
char path[96];
int nextSong = 0;
filecommands.opendir(MP3PATH);
{
filecommands.readdir(filename, "*.mp3");
}
strcpy(path, MP3PATH);
strcat(path, "/");
strcat(path, "*.mp3"); // we have to do this because the IDE thinks that "/*" needs to be terminated everywhere
numberOfSongs = filecommands.filecount(path);
lastSong = -1;
char c;
// uint8_t i;
int16_t newtime;
playbackinfo playinfo;
char status = 'S';
uint8_t playing = 1;
uint8_t volume = 20;
uint8_t boostOn = false;
volume = rmp3.getvolume(); // this only gets the left volume
playNextSong();
while(1)
{
while(!Serial.available())
{
// we should do fancy stuff like flash lights on our Xmas tree here!
// got lots of time!
delay(200);
status = rmp3.getplaybackstatus();
playinfo = rmp3.getplaybackinfo();
if (status == 'S' && playing)
playNextSong();
}
// OOH!! got a character!
c = Serial.read();
switch(c)
{
case 'p':
// pause
if(status == 'D')
{
// fade in
rmp3.playpause();
rmp3.fade(volume, 400);
}
else if(status == 'P')
{
// fade out
rmp3.fade(100, 400);
rmp3.playpause();
}
else
{
// start playing
playNextSong();
playing = 1;
}
break;
case 's':
rmp3.stop();
playing = 0;
break;
case 'n':
playNextSong();
playing = 1;
break;
case 'e':
if(boostOn)
{
rmp3.setboost(0);
boostOn = false;
}
else
{
rmp3.setboost(8, 6, 7, 3);
boostOn = true;
}
break;
case 'a':
// jump back 5 seconds
newtime = playinfo.position - 5;
if (newtime < 0) newtime = 0;
rmp3.jump(newtime);
break;
case 'd':
// jump forward 5 seconds
rmp3.jump(playinfo.position + 5);
break;
case 'k':
if(volume < 254) volume++;
if(status != 'D') rmp3.setvolume(volume);
break;
case 'i':
if(volume > 0) volume--;
if(status != 'D') rmp3.setvolume(volume);
break;
}
}
}
I have commented out parts that I dont think are necessary, but I am not sure.
Any guidance on how to go from here would be greatly appreciated.
Thank you
Jennifer