Talking teddy bear

Hello I am trying to get a teddy bear to talk by a push of the button. I am using the sparkfun mp3 shield but I am unable to actually get it to work. I have been using code I found on the same project from somewhere else, and have been trying to think of ways to get it to work but I keep coming up blank
here is the code I’ve been using

//This code plays an MP3 song when a button is pressed
//When the button is pressed, the MP3 shield plays the track00x song (a counter x is incremented)
//When pressed again, the track is stopped (if it was playing)
//The musics tracks are stored on the SD card as track00x.mp3 format

//libraries come from http://www.billporter.info/2012/01/28/sparkfun-mp3-shield-arduino-library/
//github source : https://github.com/madsci1016/Sparkfun-MP3-Player-Shield-Arduino-Library
#include <SPI.h> //bus SPI
#include <SdFat.h> //SD card
#include <SdFatUtil.h> //SD card tools
#include <SFEMP3Shield.h> //shield library

SFEMP3Shield MP3player; //player
SdFat sd; //card
const int pinButton = 5;
const int nbTracks = 5; //CHANGE THIS VALUE TO THE NUMBER OF SONGS ON YOUR SD CARD
const int volume = 6;//-3dB. The higher number, the lower volume
int counter = 1;

void setup() {
  //Serial.begin(9600);
  sd.begin(SD_SEL, SPI_HALF_SPEED); //start card 
  pinMode(pinButton, INPUT);
 
  //setup of player
  MP3player.begin(); //start player
  MP3player.setDifferentialOutput(1); //higher output power
  MP3player.setVolume(volume, volume);
}

void loop(){
  if (MP3player.isPlaying() && digitalRead(pinButton)){
    //if playing, then a press of the button stops the music
    MP3player.stopTrack();
    //Serial.println("music stopped");
    delay(500); //to release button         
  }
 
  else if(!MP3player.isPlaying() && digitalRead(pinButton)){  
    //if not playing, then a press of the button starts the music
   
    //first, increment counter
    counter += 1;
    if (counter > nbTracks)
    counter = 1;
   
    int errorCode; //used to debug
    errorCode = MP3player.playTrack(counter); //play
      /*
      Serial.print("playing track ");     
      Serial.print(counter);
      Serial.print(" at ");    
      Serial.print(-volume/2);
      Serial.println("dB");     
      Serial.print("error code (0 is OK): ");
      Serial.println(errorCode);
      */     


     delay(500); //to release button 
  }     
}

moderator: added code tags (that’s the </> button)

What doesn't work? What does it currently do? How does that differ from what you want it to do?

I think maybe you shouldn't be testing for the button being pushed, but rather testing that it just became pushed. When the loop repeats, if the button is still pushed it will trip that if again and pick a new track. See the "State Change" example in the IDE for some inspiration.

Right now it does nothing at all. I have everything hooked up the way it should be but when I press the button nothing happens. what I want it to do is play a track with a push of a button, then when that track is done, play the next track with a push of the button. I will look into the library you suggested

I checked ou tthe library but that is what I already have the arduino trying to read

I didn't suggest any library I suggested looking at an example sketch, and if you think the "State Change" example is doing the same thing you are, then you need to look again.

Let's say I push the button and the MP3 starts, but I don't get my finger off the button in time for the half second delay you have then the program still reads the button pushed and turns the thing right back off.

Let's say I push the button and push quickly so I don't have that problem but I push it during the half second delay so it never gets read then nothing happens.

The only way your program works now is if I push the button at exactly the right time AND get my finger off at exactly the right time.

Have you verified that the MP3 shield is working right? Try a simple sketch without the button stuff that does nothing but play an MP3 file and see if you get output. Debugging the rest of this would be futile if there is some problem with the MP3 shield.

pinMode(pinButton, INPUT);

Do you have an external pull-down resistor to make sure the pin reads LOW when the button isn't pressed? It's much easier to use INPUT_PULLUP and wire the button so that it connects to ground when pressed. In that case a pressed button reads LOW and unpressed reads HIGH.

I trouble shooted (shot?) all components to make sure everything worked. I have a 10k ohm resistor between the push pin and ground.

Senita:
I trouble shooted (shot?) all components to make sure everything worked. I have a 10k ohm resistor between the push pin and ground.

Between the button and Ground? How? It should be from the pin to ground. The button should be between the pin and 5V with the code written the way it is.

There was more to my responses that you aren't addressing. What about the timing of button pushes and all (Reply #4)?

here is the circuitry lay out i tried using a state change but still nothing worked.
#include <SPI.h> //bus SPI
#include <SdFat.h> //SD card
#include <SdFatUtil.h> //SD card tools
#include <SFEMP3Shield.h> //shield library

SFEMP3Shield MP3player; //player
SdFat sd; //card
const int pinButton = 5;
const int nbTracks = 2; //CHANGE THIS VALUE TO THE NUMBER OF SONGS ON YOUR SD CARD
const int volume = 6;
int counter = 01;
int buttonState = 0;
int lastButtonState = 0;

void setup() {
//Serial.begin(9600);
sd.begin(SD_SEL, SPI_HALF_SPEED); //start card
pinMode(pinButton, INPUT);

//setup of player
MP3player.begin(); //start player
MP3player.setDifferentialOutput(1); //higher output power
MP3player.setVolume(6, 6);
}
void loop() {
buttonState = digitalRead(pinButton);
if (buttonState != lastButtonState) {
if(buttonState == HIGH) {
counter ++;
MP3player.playTrack(counter);
}
else {
Serial.println(“off”);
}
delay(50);
}
lastButtonState = buttonState;
}

WIN_20151122_13_05_27_Pro.jpg

WIN_20151122_13_04_09_Pro.jpg

I know you know how to use code tags because you have them in your first post. Is there a reason you want to make this post harder to read?

It's hard to see what's going on in those pictures. Can you draw a schematic?

I have no idea how I did the quote the first time

Senita:
I have no idea how I did the quote the first time

It’s not a quote. Code tags. Use the button that looks like </> above where you type.

Your schematic looks good. If the MP3 player works by itself then it should be working here. I don't really know enough about the MP3 shields to know if you've got that code right, but if you say the same code works without the button stuff then it must be.

int counter = 01;

This isn't your problem, but watch out putting leading zeroes on numbers. That tells the compiler to interpret them as octal and not decimal numbers.

ie. 010 == 8 not 10

Put a Serial.print("ON") right before the counter++ line and use the serial monitor to see if the code logic is running right. If it prints the ON but doesn't play the file then you must be using the MP3 shield wrong and I'll let someone else take over for that.

I haven't been able to get an code to work with the shield other than the source code. I put the lead zero down because the source code requires me to put 01 in the serial monitor to get it to play the track on the micro sd

Senita:
I haven't been able to get an code to work with the shield other than the source code.

Post the code that worked so we can compare.

Senita:
I put the lead zero down because the source code requires me to put 01 in the serial monitor to get it to play the track on the micro sd

That may have to do with how the code is parsing your serial input. It has nothing to do with using a variable in your code. But in this case it really doesn't matter as octal 1 and decimal 1 are the same 1.

nothing is coming up on the serial monitor

Senita:
nothing is coming up on the serial monitor

Even when you press the button?

Put a print line at the end of setup and one at the beginning of loop and see if the program is hanging somewhere.

Post the code that you said worked to play an MP3 from the shield.

Oh wait, did you put a Serial.begin statement in the setup part of your code?