Press a Button to Play a Song and Do Stuff While Playing

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
angrystorm
 
Posts: 17
Joined: Wed Mar 01, 2017 12:21 pm

Press a Button to Play a Song and Do Stuff While Playing

Post by angrystorm »

Hello! I'm using the Music Maker Shield in order to make a jukebox. Basically, when a button is pressed, I want it to play a specific file. While that file is playing, I want it to print the name of the song on the LCD and check to see if it should change the volume. Here's my code:

Code: Select all

/***************************************************
  This is an example for the Adafruit VS1053 Codec Breakout

  Designed specifically to work with the Adafruit VS1053 Codec Breakout
  ----> https://www.adafruit.com/products/1381

  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.
  BSD license, all text above must be included in any redistribution
 ****************************************************/

// include SPI, MP3,LCD, and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
#include <LiquidCrystal.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);


LiquidCrystal lcd(9, 10, 1, 2, 5, 8);
////

void setup() {
  lcd.begin(16, 2);

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

  // 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(20, 20);

  /***** 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 useans a pin interrupt. No timers required! But DREQ
  // must be on  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 loop() {
  if (musicPlayer.GPIO_digitalRead(1) == HIGH) {
    // Alternately, we can just play an entire fig filele at once
    // This doesn't happen in the background, instead, the entire
    // file is played and the program will continue when it's done!
    musicPlayer.playFullFile("track001.mp3");

    // Start playing a file, then we can do stuff while waiting for it to finish
    if (! musicPlayer.startPlayingFile("track001.mp3")) {
      Serial.println("Could not open file track001.mp3");
      while (1);
    }
    Serial.println(F("Started playing"));

    while (musicPlayer.playingMusic) {
      lcd.setCursor(0, 0); //LCD stuff
      lcd.print("Now playing:");
      lcd.setCursor(0, 1);
      lcd.print("I Want You Back"); //LCD stuff
      delay(1000);
      volume();
      delay(1000);
    }
    Serial.println("Done playing music");
  }

  if (musicPlayer.GPIO_digitalRead(2) == HIGH) {
    // Alternately, we can just play an entire fig filele at once
    // This doesn't happen in the background, instead, the entire
    // file is played and the program will continue when it's done!
    musicPlayer.playFullFile("track002.mp3");

    // Start playing a file, then we can do stuff while waiting for it to finish
    if (! musicPlayer.startPlayingFile("track002.mp3")) {
      Serial.println("Could not open file track002.mp3");
      while (1);
    }
    Serial.println(F("Started playing"));

    while (musicPlayer.playingMusic) {
      lcd.setCursor(0, 0); //LCD stuff
      lcd.print("Now playing:");
      lcd.setCursor(0, 1);
      lcd.print("Don't Stop Believing");
      for (int positionCounter = 0; positionCounter < 4; positionCounter++) { //need to figure out scrolling
        lcd.scrollDisplayLeft();
        delay(150);
      }
      delay(1000);
      for (int positionCounter = 0; positionCounter < 4; positionCounter++) {
        lcd.scrollDisplayRight();
        delay(150);
      }
      delay(1000);
      volume();
      delay(1000);
    }
    Serial.println("Done playing music");
  }

  if (musicPlayer.GPIO_digitalRead(3) == HIGH) {
    // Alternately, we can just play an entire fig filele at once
    // This doesn't happen in the background, instead, the entire
    // file is played and the program will continue when it's done!
    musicPlayer.playFullFile("track003.mp3");

    // Start playing a file, then we can do stuff while waiting for it to finish
    if (! musicPlayer.startPlayingFile("track003.mp3")) {
      Serial.println("Could not open file track003.mp3");
      while (1);
    }
    Serial.println(F("Started playing"));

    while (musicPlayer.playingMusic) {
      lcd.setCursor(0, 0); //LCD stuff
      lcd.print("Now playing:");
      lcd.setCursor(0, 1);
      lcd.print("El Perdon"); //LCD stuff
      delay(1000);
      volume();
      delay(1000);
    }
    Serial.println("Done playing music");
  }

  if (musicPlayer.GPIO_digitalRead(4) == HIGH) {
    // Alternately, we can just play an entire fig filele at once
    // This doesn't happen in the background, instead, the entire
    // file is played and the program will continue when it's done!
    musicPlayer.playFullFile("track004.mp3");

    // Start playing a file, then we can do stuff while waiting for it to finish
    if (! musicPlayer.startPlayingFile("track004.mp3")) {
      Serial.println("Could not open file track004.mp3");
      while (1);
    }
    Serial.println(F("Started playing"));

    while (musicPlayer.playingMusic) {
      lcd.setCursor(0, 0); //LCD stuff
      lcd.print("Now playing:");
      lcd.setCursor(0, 1);
      lcd.print("I Will Survive"); //LCD stuff
      delay(1000);
      volume();
      delay(1000);
    }
    Serial.println("Done playing music");
  }

}


/// 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();
  }
}

void volume() { //volume
  int rawVol = analogRead(0);

  if ((rawVol > 815) && (rawVol < 850)) {
    //int newVol = 20;
    musicPlayer.setVolume(20, 20);
  }
  if ((rawVol > 770) && (rawVol < 815)) {
    //int newVol = 18;
    musicPlayer.setVolume(18, 18);
  }
  if ((rawVol > 710) && (rawVol < 770)) {
    //int newVol = 16;
    musicPlayer.setVolume(16, 16);
  }
  if ((rawVol > 640) && (rawVol < 710)) {
    //int newVol = 14;
    musicPlayer.setVolume(14, 14);
  }
  if ((rawVol > 550) && (rawVol < 640)) {
    //int newVol = 12;
    musicPlayer.setVolume(12, 12);
  }
  if ((rawVol > 450) && (rawVol < 550)) {
    //int newVol = 10;
    musicPlayer.setVolume(10, 10);
  }
  if ((rawVol > 290) && (rawVol < 450)) {
    //int newVol = 8;
    musicPlayer.setVolume(8, 8);
  }
  if ((rawVol > 200) && (rawVol < 290)) {
    //int newVol = 6;
    musicPlayer.setVolume(6, 6);
  }
  if ((rawVol > 130) && (rawVol < 200)) {
    //int newVol = 4;
    musicPlayer.setVolume(4, 4);
  }
  if ((rawVol > 0) && (rawVol > 130)) {
    //int newVol = 2;
    musicPlayer.setVolume(2, 2);
  }
}
I'm having a hard time getting this to work. Is something wrong with my code? Any help would be appreciated :).

User avatar
adafruit_support_carter
 
Posts: 29056
Joined: Tue Nov 29, 2016 2:45 pm

Re: Press a Button to Play a Song and Do Stuff While Playing

Post by adafruit_support_carter »

What is the behavior of your current code?

User avatar
angrystorm
 
Posts: 17
Joined: Wed Mar 01, 2017 12:21 pm

Re: Press a Button to Play a Song and Do Stuff While Playing

Post by angrystorm »

It's kind of weird. First, it will play through a song once and neither my LCD nor volume knob will work. Then it loops the song a second time and some weird characters appear on the LCD. Does the arduino get stuck on the command "musicPlayer.playFullFile("track001.mp3")" before it moves to the while loop? How can I get music to play and get the while loop to function at the same time?

User avatar
angrystorm
 
Posts: 17
Joined: Wed Mar 01, 2017 12:21 pm

Re: Press a Button to Play a Song and Do Stuff While Playing

Post by angrystorm »

Update: The LCD can function on its own while the shield is attached, but if I try to use the shield and the LCD at the same time, it doesn't work. So I know my code and wiring match up. It looks like the LCD and the music maker shield are conflicting with each other(?)

User avatar
adafruit_support_carter
 
Posts: 29056
Joined: Tue Nov 29, 2016 2:45 pm

Re: Press a Button to Play a Song and Do Stuff While Playing

Post by adafruit_support_carter »

Does the arduino get stuck on the command "musicPlayer.playFullFile("track001.mp3")" before it moves to the while loop?
Yes.
How can I get music to play and get the while loop to function at the same time?
Use startPlayingFile() in conjunction with other functions. See example here:
https://github.com/adafruit/Adafruit_VS ... le.ino#L78
and note how pausing/stopping/etc. are taken care of in the loop() once the file has started playing.
So I know my code and wiring match up. It looks like the LCD and the music maker shield are conflicting with each other(?)
Possibly. It looks like you are re-using some of the same pins. Post a photo of your setup showing all connections.

User avatar
adafruit_support_carter
 
Posts: 29056
Joined: Tue Nov 29, 2016 2:45 pm

Re: Press a Button to Play a Song and Do Stuff While Playing

Post by adafruit_support_carter »

Your images didn't come through for some reason.
Why is Serial.available() being used? What are the c, s, and p for?
Examples reads single letter commands from the serial console:
  • c = the character read from the serial monitor - the user typed it in via a keyboard
  • s = stop
  • p = pause (toggle)
It's basically a crude play back control example.

User avatar
angrystorm
 
Posts: 17
Joined: Wed Mar 01, 2017 12:21 pm

Re: Press a Button to Play a Song and Do Stuff While Playing

Post by angrystorm »

Here are the images. Sorry for the spaghetti wiring!

http://imgur.com/a/4ZyEP

I'm a bit confused by the example code. What's going on with the Serial.available() and the letters?

Sorry about that earlier post. I made a mistake with the photos and wanted to reupload

User avatar
adafruit_support_carter
 
Posts: 29056
Joined: Tue Nov 29, 2016 2:45 pm

Re: Press a Button to Play a Song and Do Stuff While Playing

Post by adafruit_support_carter »

The LCD can function on its own while the shield is attached, but if I try to use the shield and the LCD at the same time, it doesn't work.
Wiring looks OK. Reading this again it sounds like you don't change anything but the code. A sketch that uses just the LCD works, but if you try a sketch that uses both, the LCD doesn't work. Post the code for the sketch that tries to use both but the LCD does not work.

User avatar
angrystorm
 
Posts: 17
Joined: Wed Mar 01, 2017 12:21 pm

Re: Press a Button to Play a Song and Do Stuff While Playing

Post by angrystorm »

Here is the code:

Code: Select all

/***************************************************
  This is an example for the Adafruit VS1053 Codec Breakout

  Designed specifically to work with the Adafruit VS1053 Codec Breakout
  ----> https://www.adafruit.com/products/1381

  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.
  BSD license, all text above must be included in any redistribution
 ****************************************************/

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

// define the pins used
#define CLK 13       // SPI Clock, shared with SD card
#define MISO 12      // Input data, from VS1053/SD card
#define MOSI 11      // Output data, to VS1053/SD card
// Connect CLK, MISO and MOSI to hardware SPI pins.
// See http://arduino.cc/en/Reference/SPI "Connections"

// 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);

LiquidCrystal lcd(9, 10, 1, 2, 0, 8);

void setup() {

  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("Press a button");

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

  // 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(20, 20);



  /***** 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 useans a pin interrupt. No timers required! But DREQ
  // must be on  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
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);  // DREQ int
  if (! musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT)) {
    Serial.println(F("DREQ pin is not an interrupt pin"));
  }
}

void loop() {
  if (musicPlayer.GPIO_digitalRead(1) == HIGH) {
    musicPlayer.startPlayingFile("track001.mp3");
    lcd.clear();
    lcd.print("Now playing:");
    lcd.setCursor(0, 1);
    lcd.print("I Want You Back");

    while (musicPlayer.playingMusic) {
      Serial.println(".");
    }
  }

}

/// 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();
  }
}

User avatar
adafruit_support_carter
 
Posts: 29056
Joined: Tue Nov 29, 2016 2:45 pm

Re: Press a Button to Play a Song and Do Stuff While Playing

Post by adafruit_support_carter »

Now post the code that works with the LCD.

User avatar
angrystorm
 
Posts: 17
Joined: Wed Mar 01, 2017 12:21 pm

Re: Press a Button to Play a Song and Do Stuff While Playing

Post by angrystorm »

Okay so this code works with the LCD, but the volume knob still won't work. I suspect that it's must too much for the Arduino to handle while playing the song and that I should probably use buttons and for loops to change the volume.

Code: Select all

/***************************************************
  This is an example for the Adafruit VS1053 Codec Breakout

  Designed specifically to work with the Adafruit VS1053 Codec Breakout
  ----> https://www.adafruit.com/products/1381

  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.
  BSD license, all text above must be included in any redistribution
 ****************************************************/

char* songs[] = {"track001.mp3", "track002.mp3", "track003.mp3", "track004.mp3"};
int rawVol = analogRead(0);
int buttonPressed = 10;

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

// define the pins used
#define CLK 13       // SPI Clock, shared with SD card
#define MISO 12      // Input data, from VS1053/SD card
#define MOSI 11      // Output data, to VS1053/SD card
// Connect CLK, MISO and MOSI to hardware SPI pins.
// See http://arduino.cc/en/Reference/SPI "Connections"

// 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);

LiquidCrystal lcd(1, 2, 5, 8, 9, 10);

void setup() {

  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("Now playing:");
  lcd.setCursor(0, 1);

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

  // 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(20, 20);



  /***** 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 useans a pin interrupt. No timers required! But DREQ
  // must be on  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
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);  // DREQ int
  if (! musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT)) {
    Serial.println(F("DREQ pin is not an interrupt pin"));


  }
}

void loop() {
  if (musicPlayer.GPIO_digitalRead(1) == HIGH) {
    buttonPressed = 1;
    lcd.print(songs[0]);
    musicPlayer.startPlayingFile(songs[0]);
    while (musicPlayer.playingMusic) {
      volume();
      delay(1000);
    }
    buttonPressed = 0;
  }

  if (musicPlayer.GPIO_digitalRead(2) == HIGH) {
    buttonPressed = 2;
    lcd.print(songs[1]);
    musicPlayer.startPlayingFile(songs[1]);
    while (musicPlayer.playingMusic) {
      volume();
      delay(1000);
    }
    buttonPressed = 0;
  }

  if (musicPlayer.GPIO_digitalRead(3) == HIGH) {
    buttonPressed = 3;
    lcd.print(songs[2]);
    musicPlayer.startPlayingFile(songs[2]);
    while (musicPlayer.playingMusic) {
      volume();
      delay(1000);
    }
    buttonPressed = 0;
  }

  if (musicPlayer.GPIO_digitalRead(4) == HIGH) {
    buttonPressed = 4;
    lcd.print(songs[3]);
    musicPlayer.startPlayingFile(songs[3]);
    while (musicPlayer.playingMusic) {
      volume();
      delay(1000);
    }
    buttonPressed = 0;
  }

  if (buttonPressed == 0) {
    lcd.setCursor(0, 1);
    lcd.print("                         ");
    lcd.setCursor(0, 1);
    buttonPressed = 10;
  }

}

void volume() {
  if ((rawVol > 815) && (rawVol < 850)) {
    //int newVol = 20;
    musicPlayer.setVolume(20, 20);
  }
  if ((rawVol > 770) && (rawVol < 815)) {
    //int newVol = 18;
    musicPlayer.setVolume(18, 18);
  }
  if ((rawVol > 710) && (rawVol < 770)) {
    //int newVol = 16;
    musicPlayer.setVolume(16, 16);
  }
  if ((rawVol > 640) && (rawVol < 710)) {
    //int newVol = 14;
    musicPlayer.setVolume(14, 14);
  }
  if ((rawVol > 550) && (rawVol < 640)) {
    //int newVol = 12;
    musicPlayer.setVolume(12, 12);
  }
  if ((rawVol > 450) && (rawVol < 550)) {
    //int newVol = 10;
    musicPlayer.setVolume(10, 10);
  }
  if ((rawVol > 290) && (rawVol < 450)) {
    //int newVol = 8;
    musicPlayer.setVolume(8, 8);
  }
  if ((rawVol > 200) && (rawVol < 290)) {
    //int newVol = 6;
    musicPlayer.setVolume(6, 6);
  }
  if ((rawVol > 130) && (rawVol < 200)) {
    //int newVol = 4;
    musicPlayer.setVolume(4, 4);
  }
  if ((rawVol > 0) && (rawVol > 130)) {
    //int newVol = 2;
    musicPlayer.setVolume(2, 2);
  }
}

/// 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();
  }
}

User avatar
jim_lee
 
Posts: 709
Joined: Thu May 24, 2012 8:24 pm

Re: Press a Button to Play a Song and Do Stuff While Playing

Post by jim_lee »

Code: Select all

 while (musicPlayer.playingMusic) {
      volume();
      delay(1000);
    }
You don't want to put loops inside your "loop()" function. Just save off the current miliseconds when you start add 100 or more and when now>saved call volume(); This way EVERYTHING is still left running. Even things you don't know bout that need to be running.

example:

Code: Select all

#define WAIT_MS 100.  // You can adjust this up and down.


unsigned long savedMS;   // Global to hold your "timer"

setup() {


bla bla bla..

savedMS = millis();   // Starting your timer already expired.
}


loop() {

   if(millis()> savedMS) {.    // If your timer has expired..
      savedMS = millis() + WAIT_MS;   // Reset it..
      int rawVol = AnalogRead(0);        // In your code this is only read once.
      int vol = map(rawVol, 0, 1024, 0, 60);  // Use a mapper quick & easy.
      musicPlayer.setVol(vol,vol);                       // Stuff it in the board. Done!
   }
//Do other loop() things..
}
Hope this helps.


-jim lee

User avatar
angrystorm
 
Posts: 17
Joined: Wed Mar 01, 2017 12:21 pm

Re: Press a Button to Play a Song and Do Stuff While Playing

Post by angrystorm »

Thank you so much! It's working now. :)

User avatar
adafruit_support_carter
 
Posts: 29056
Joined: Tue Nov 29, 2016 2:45 pm

Re: Press a Button to Play a Song and Do Stuff While Playing

Post by adafruit_support_carter »

Excellent. Good input @jim_lee.

FWIW, here's more info about how to avoid using delay() and multitask an Arduino:
https://learn.adafruit.com/multi-taskin ... 1/overview
https://learn.adafruit.com/multi-taskin ... 2/overview
https://learn.adafruit.com/multi-taskin ... 3/overview

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”