Hi all,
I'm building my very first arduino project and what I want it to do is to use a potentiometer (attached to A5) to select mp3 tracks on a SD card using the sparkfun mp3 shield and play them back. I have the difficult stuff figured out (I think), I can get the audio to play, and I can even get the tracks to change a few times, but that is when it starts running into problems. I have the serial monitor running as well for debugging purposes and every time, after changing the tracks a few times either the serial monitor crashes and stops updating it's values, and the mp3 track keeps playing (but won't start a new track) or that both the serial monitor stops updating and the mp3 stops playing. The other odd thing it is doing is I have 5 mp3's it should be selecting, however it seems to be assigning tracks at random (i.e. sometimes when it should be selecting track 1 it actually plays track 3). I'm using a library provided by this guy Sparkfun MP3 Shield Arduino Library « The Mind of Bill Porter who has made my life a lot easier, but I'm not sure if something is messing up behind the scenes. I wouldn't know what to look for if it was. The code for the project is below. I've started using volatile Int's because I was hoping that was the problem. I'm not sure if they all need to be volatile. I've even commented out the serial monitoring in case that was causing a conflict somehow but that didn't help. The delay is in there for the same reason. Anyway, any help would be appreciated, it is probably something very obvious that I am missing. I've been teaching myself how to code so I'm sure there are holes in my education.
Thanks!
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>
SFEMP3Shield MP3player;
int potPin = A5;
void setup (){
pinMode (potPin, INPUT);
MP3player.begin();
MP3player.SetVolume(1, 1);
Serial.begin(9600);
}
boolean checkMovement (int oldPinValue){
volatile int currentValue = analogRead(potPin);
currentValue = constrain(currentValue, 0, 1000);
volatile int val = map (currentValue, 0, 1000, 1, 5);
if (oldPinValue == val){
return(false);
}
else {
MP3player.stopTrack();
return(true);
}
}
void loop (){
volatile int tempPin = analogRead(potPin);
volatile int pinValue = analogRead(potPin);
pinValue = constrain (pinValue, 0, 1000);
volatile int track = map (pinValue, 0, 1000, 1, 5);
Serial.println(track);
Serial.println(tempPin);
delay(500);
if (checkMovement (track) == false){
}
else {
MP3player.playTrack(track);
}
}