Go Down

Topic: Adafruit_VS1053.h and Wire.h on Arduino UNO R3 (Read 309 times) previous topic - next topic

sebbo111

Hello everybody,

iam relative new on arduino and c programming, but get it to work to run 2 ardurino uno r3, where one is for the master ic receiver and one controls a led stripe. Now iam connecting a third uno r3 with an adafruit mp3 shield (https://learn.adafruit.com/adafruit-music-maker-shield-vs1053-mp3-wav-wave-ogg-vorbis-player/) which is already receiving the ic remote commands, but everything i tried to simply play a song while the ic commands work like play/pause/volume+-/next/previous/... etc. failed. I googled a lot and also changed a lot of code, but nothing seems to really work, for ic commands in parallel process. Below my last code changes and here are some of my tries/figures.

- loop doesn't seems to be executed after "if (!musicPlayer.startPlayingFile(song))", if i remove the while "while (musicPlayer.playingMusic)" it doesn't play the song anymore and when i used "playFullFile" everything is blocked and no ic signal is received anymore

- actually the volume +/- functions seems to work as they output always the correct value, but after 4-6 times for example highering the volume it breaks and the song is completly quiet (as there are still ........... output from the example code)

Hope someone got an idea or did a similar project, anyways thanks a lot for your help

I've also tried to ask on the adafruit forum before but seems that nobody there got an idea.

Hopefully someone here may have a clue.

Thanks and greetings,
Sebastian

Code: [Select]

    #include <Wire.h>
    // include SPI, MP3 and SD libraries
    #include <SPI.h>
    #include <Adafruit_VS1053.h>
    #include <SD.h>

    // These are the pins used for the breakout example
    #define BREAKOUT_RESET  9      // VS1053 reset pin (output)
    #define BREAKOUT_CS     10     // VS1053 chip select pin (output)
    #define BREAKOUT_DCS    8      // VS1053 Data/command select pin (output)
    // These are the pins used for the music maker shield
    #define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
    #define SHIELD_CS     7      // VS1053 chip select pin (output)
    #define SHIELD_DCS    6      // VS1053 Data/command select pin (output)

    // These are common pins between breakout and shield
    #define CARDCS 4     // Card chip select pin
    // DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
    #define DREQ 3       // VS1053 Data request, ideally an Interrupt pin

    Adafruit_VS1053_FilePlayer musicPlayer =
      // create breakout-example object!
      // Adafruit_VS1053_FilePlayer(BREAKOUT_RESET, BREAKOUT_CS, BREAKOUT_DCS, DREQ, CARDCS);
      // create shield-example object!
      Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);

    enum Mode {PLAY,PAUSE,OFF,ON,LOUDER,QUIETER,NEXT,PREVIOUS};
    Mode mode = PLAY;
    Mode lastMode = PLAY;

    const char *currSong = "track001.mp3";

    int volume = 20;
    int off = 0;

    void setup() {
      Serial.begin(9600);
      Serial.println("Adafruit VS1053 Library Test");

      Wire.begin(9);                // Start I2C Bus as a Slave (Device Number 9)
      Wire.onReceive(receiveEvent); // register event

      // initialise the music player
      if (!musicPlayer.begin()) { // initialise the music player
         Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
         while (1);
      }
      Serial.println(F("VS1053 found"));

      musicPlayer.sineTest(0x44, 500);    // Make a tone to indicate VS1053 is working
     
      if (!SD.begin(CARDCS)) {
        Serial.println(F("SD failed, or not present"));
        while (1);  // don't do anything more
      }
      Serial.println("SD OK!");
     
      // list files
      printDirectory(SD.open("/"), 0);
     
      // Set volume for left, right channels. lower numbers == louder volume!
      musicPlayer.setVolume(volume,volume);

      /***** Two interrupt options! *******/
      // This option uses timer0, this means timer1 & t2 are not required
      // (so you can use 'em for Servos, etc) BUT millis() can lose time
      // since we're hitchhiking on top of the millis() tracker
      //musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT);
     
      // This option uses a pin interrupt. No timers required! But DREQ
      // must be on an interrupt pin. For Uno/Duemilanove/Diecimilla
      // that's Digital #2 or #3
      // See http://arduino.cc/en/Reference/attachInterrupt for other pins
      // *** This method is preferred
      if (! musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT))
        Serial.println(F("DREQ pin is not an interrupt pin"));
    }


    void receiveEvent(int bytes) {
     
      // Here, we set the mode based on the IR signal received. Check the debug log when you press a button on your remote, and
      // add the hex code here (you need 0x prior to each command to indicate it's a hex value)
      while(Wire.available())
       {
          unsigned int received = Wire.read();
          Serial.print("Receiving IR hex: ");
          Serial.println(received,HEX);
          lastMode = mode;
          switch(received){
            case 0xFD:
              mode = PAUSE; break;
            case 0x5D:
              if(off == 1) {
                off = 0;
                mode = ON; break;
              } else {
                off = 1;
                mode = OFF; break;
              }
            case 0x9D:
              volume_louder(); break;
            case 0x57:
              volume_quieter(); break;
            case 0x9F:
              mode = NEXT; break;
            case 0x5F:
              mode = PREVIOUS; break;       
          }
       }
    }

    void loop() {
     
      // Maps mode names to code functions.
      switch(mode){
        case PLAY: play_song(currSong);break;
        case PAUSE: pause_play_song();break;
        case OFF: shut_off();break;
        case ON: play_song(currSong);break;
        case LOUDER: volume_louder();break;
        case QUIETER: volume_quieter();break;
        case NEXT: next_song();break;
        case PREVIOUS: previous_song();break;
        default: play_song(currSong);break;
      }
     
    }

    void play_song(const char* song) {
      // Start playing a file, then we can do stuff while waiting for it to finish
      if (!musicPlayer.startPlayingFile(song)) {
        String out = "Could not open file ";
        out += song;
        Serial.println(out.c_str());
        while (1);
      }
      Serial.println(F("Started playing"));

      while (musicPlayer.playingMusic) {
        // file is now playing in the 'background' so now's a good time
        // to do something else like handling LEDs or buttons :)
        Serial.print(".");
        delay(1000);
      }
      Serial.println("Done playing music");
    }


    void pause_play_song() {
      if (!musicPlayer.paused()) {
        Serial.println("Paused");
        musicPlayer.pausePlaying(true);
      } else {
        Serial.println("Resumed");
        musicPlayer.pausePlaying(false);
      }
    }
     
    void shut_off() {

    }

    void volume_louder() {
      if(volume > 0) {
         volume--;
         Serial.print(volume);
         musicPlayer.setVolume(volume,volume);
         delay(50);
      }
    }

    void volume_quieter() {
      if(volume < 100) {
         volume++;
         Serial.print(volume);
         musicPlayer.setVolume(volume,volume);
         delay(50);
      }
    }

    void next_song() {}
    void previous_song() {}
    void reset() {}


    /// File listing helper
    void printDirectory(File dir, int numTabs) {
       while(true) {
         
         File entry =  dir.openNextFile();
         if (! entry) {
           // no more files
           //Serial.println("**nomorefiles**");
           break;
         }
         for (uint8_t i=0; i<numTabs; i++) {
           Serial.print('\t');
         }
         Serial.print(entry.name());
         if (entry.isDirectory()) {
           Serial.println("/");
           printDirectory(entry, numTabs+1);
         } else {
           // files have sizes, directories do not
           Serial.print("\t\t");
           Serial.println(entry.size(), DEC);
         }
         entry.close();
       }
    }

Go Up