Home Forums Sparkfun MP3 Shield Library Support Forum vs1053 + keypad: mp3 code

  • This topic has 2 replies, 2 voices, and was last updated 10 years ago by Anonymous.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #3123
    Anonymous
    Inactive

    Hello all, this is my first post here… got all the code from this great library… and from Michael Flaga sketch…
    I got the above sketch that controls volume, call some mp3 files, do the back and the after…

    The case 6 is my problem here. It plays a sequence of files all the way thru… but while it doesn’t complete the (for cycle) the other buttons doesn’t work stoping the music and doing their action.

    Any help is appreciated, tank you

    #include <Keypad.h>
    #include <SPI.h>
    #include <SdFat.h>
    #include <SdFatUtil.h>
    #include <SFEMP3Shield.h>

    SdFat sd;
    SFEMP3Shield MP3player;

    long randNumber;
    int trackNumber = 2;

    const byte ROWS = 4;
    const byte COLS = 3;

    char keys[ROWS][COLS] = {
    {‘1′,’2′,’3’},
    {‘4′,’5′,’6’},
    {‘7′,’8′,’9’},
    {‘*’,’0′,’#’}
    };

    byte rowPins[ROWS] = {A0,A1,A2,A3};
    byte colPins[COLS] = {A4,A5,3};

    Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

    void setup()
    {
    Serial.begin(9600);
    sd.begin(SD_SEL, SPI_HALF_SPEED);
    MP3player.begin();
    }

    void loop() {

    char key = kpd.getKey();
    if(key != NO_KEY)
    {
    switch (key)
    {

    case ‘1’:
    if(MP3player.isPlaying()) MP3player.stopTrack();
    MP3player.playMP3(“track001.mp3”);
    break;
    case ‘2’:
    if(MP3player.isPlaying()) MP3player.stopTrack();
    MP3player.playMP3(“track002.mp3”);
    break;
    case ‘3’:
    if(MP3player.isPlaying()) MP3player.stopTrack();
    MP3player.playMP3(“track003.mp3”);
    break;
    case ‘4’:
    if(MP3player.isPlaying()) MP3player.stopTrack();
    MP3player.playMP3(“track004.mp3”);
    break;
    case ‘5’:
    if(MP3player.isPlaying()) MP3player.stopTrack();
    MP3player.playMP3(“track005.mp3”);
    break;
    case ‘6’:
    if(MP3player.isPlaying()) MP3player.stopTrack();
    for (trackNumber=1; trackNumber <= 4; trackNumber++) {
    if(!MP3player.playTrack(trackNumber))
    { while (MP3player.isPlaying());
    MP3player.stopTrack(); } }
    break;
    case ‘7’:
    if(MP3player.isPlaying()) {MP3player.stopTrack();}
    MP3player.playTrack(trackNumber);
    trackNumber–;
    if(trackNumber < 1) {
    trackNumber = 30;
    }
    break;
    case ‘8’:
    randNumber = random(1, 40);
    if(MP3player.isPlaying()) MP3player.stopTrack();
    MP3player.playTrack(randNumber);
    break;
    case ‘9’:
    union twobyte mp3_vol; // Cria dois bytes um para cada lado
    mp3_vol.word = MP3player.getVolume();
    if(mp3_vol.byte[1] <= 2)
    {
    mp3_vol.byte[1] = 2;
    }
    else {
    mp3_vol.byte[1] -= 2;
    }
    MP3player.setVolume(mp3_vol.byte[1], mp3_vol.byte[1]); // Altera o valor do volume nos dois lados (L e R)
    break;
    case ‘*’:
    if( MP3player.getState() == playback) {
    MP3player.pauseMusic(); }
    else if( MP3player.getState() == paused_playback) { MP3player.resumeMusic(); }
    break;
    case ‘0’:
    union twobyte mp3_vol2; // Cria dois bytes um para cada lado
    mp3_vol2.word = MP3player.getVolume();
    if(mp3_vol2.byte[1] >= 254)
    {
    mp3_vol2.byte[1] = 254;
    }
    else {
    mp3_vol2.byte[1] += 2;
    }
    MP3player.setVolume(mp3_vol2.byte[1], mp3_vol2.byte[1]); // Altera o valor do volume nos dois lados (L e R)
    break;
    case ‘#’:
    if(MP3player.isPlaying()) {MP3player.stopTrack();}
    MP3player.playTrack(trackNumber);
    trackNumber++;
    if(trackNumber > 30) {
    trackNumber = 1;
    }
    break;
    }
    }
    }

    #3125

    Your while isPlaying inside case 6 is blocking, until the track finishes. Hence the keypad is not getting checked. Nor any action on its output can be performed. You may want to check the keypad and return if it was pressed. Or convert the Case 6’s for loop to a state, as not to be blocking.

    #3130
    Anonymous
    Inactive

    Tank you so much for your help…

    i used a digital input… and it did the job…

    case ‘6’:
    for (trackNumber=1; trackNumber <= 20; trackNumber++) {
    if(!MP3player.playTrack(trackNumber)) { // if it returns an error move on to next track.
    while (MP3player.isPlaying() && digitalRead(4) == LOW );
    MP3player.stopTrack();
    }
    }
    break;

    i couldn’t figure out your tip about the check the keypad and return if it was pressed… i tried a lot of things with keypad and it was always blocked…

    obrigado

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.