Play a random file when button is pressed for mp3 music make

Breakout boards, sensors, other Adafruit kits, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
ald12
 
Posts: 18
Joined: Wed Mar 29, 2017 6:23 am

Play a random file when button is pressed for mp3 music make

Post by ald12 »

Hello, I have an arduino uno attached to an mp3 music maker. I want when a button is pressed to play a random file from the database, and to stop the play if the button is pressed again and to play another file if the button is pressed a third time. Is the below code correct? Also for the hardware, I've soldered in all the headers and attached the speakers and mounted it to the uno with power. Do I have to do anything else besides wire up the button? For the example, do I have to attach a button to an interrupt pin or anything like that?


Code: Select all

#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>


int buttonPin=11;//change button number


int functionFlag;
int playFlag;
int startTime;
int endTime;
int currentTime;

char *soundBank[] = { "ALNGCHNA.ogg", "ALNGPPLE.ogg", "ALNGPUTN.ogg"

}; //sound archive


long int timerBank[]= {3000,15000, 3700
};//delays for each soundfile


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



void setup() {
  Serial.begin(9600);
  musicPlayer.reset();
startTime=0;
endTime=0;
playFlag=0;


pinMode(buttonPin,INPUT_PULLUP);//set up button


Serial.println("Begin Program");
}

void loop() {

int val=digitalRead(buttonPin);
currentTime=millis();




if (val==LOW && playFlag==1 && (currentTime >= startTime+1000))
{
  Serial.println("Interrupt Activated");
musicPlayer.stopPlaying(true);
playFlag=0;
}

 //if (val==LOW && switchval==HIGH && playFlag==0)

  if (val==LOW)//when button is pressed,play sound
  {
    Serial.println("Sound Button Activated");
    // print a random number from 0 to 80
    int randNumber = random(80);//pick a random file from the soundBank
    startTime=millis();
      musicPlayer.startPlaying(soundBank[randNumber]);
    endTime=startTime+timerBank[randNumber];
    playFlag=1;
 
   //allow sound to play 
  }



if (playFlag==1 && (currentTime>endTime))
{
playFlag=0;
 musicPlayer.stopPlaying();
}


  
}

User avatar
Franklin97355
 
Posts: 23938
Joined: Mon Apr 21, 2008 2:33 pm

Re: Play a random file when button is pressed for mp3 music

Post by Franklin97355 »

You have a few errors in your code try this.

Code: Select all

#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>


int buttonPin=11;//change button number


int functionFlag;
int playFlag;
int startTime;
int endTime;
int currentTime;

char *soundBank[] = { "ALNGCHNA.ogg", "ALNGPPLE.ogg", "ALNGPUTN.ogg"

}; //sound archive


long int timerBank[]= {3000,15000, 3700
};//delays for each soundfile


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



void setup() {
  Serial.begin(9600);
  musicPlayer.reset();
startTime=0;
endTime=0;
playFlag=0;


pinMode(buttonPin,INPUT_PULLUP);//set up button


Serial.println("Begin Program");
}

void loop() {

int val=digitalRead(buttonPin);
currentTime=millis();
if (val==LOW && playFlag==1 && (currentTime >= startTime+1000))
{
  Serial.println("Interrupt Activated");
musicPlayer.stopPlaying();
playFlag=0;
}
 //if (val==LOW && switchval==HIGH && playFlag==0)
  if (val==LOW)//when button is pressed,play sound
  {
    Serial.println("Sound Button Activated");
    // print a random number from 0 to 80
    int randNumber = random(80);//pick a random file from the soundBank
    startTime=millis();
      musicPlayer.startPlayingFile(soundBank[randNumber]);
    endTime=startTime+timerBank[randNumber];
    playFlag=1;
 
   //allow sound to play 
  }
if (playFlag==1 && (currentTime>endTime))
{
playFlag=0;
 musicPlayer.stopPlaying();
}  
}
I have no idea if it will work but it compiles. If it does not work post the symptoms of the failure.

User avatar
ald12
 
Posts: 18
Joined: Wed Mar 29, 2017 6:23 am

Re: Play a random file when button is pressed for mp3 music

Post by ald12 »

franklin97355 wrote:You have a few errors in your code try this.

Code: Select all

#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>


int buttonPin=11;//change button number


int functionFlag;
int playFlag;
int startTime;
int endTime;
int currentTime;

char *soundBank[] = { "ALNGCHNA.ogg", "ALNGPPLE.ogg", "ALNGPUTN.ogg"

}; //sound archive


long int timerBank[]= {3000,15000, 3700
};//delays for each soundfile


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



void setup() {
  Serial.begin(9600);
  musicPlayer.reset();
startTime=0;
endTime=0;
playFlag=0;


pinMode(buttonPin,INPUT_PULLUP);//set up button


Serial.println("Begin Program");
}

void loop() {

int val=digitalRead(buttonPin);
currentTime=millis();
if (val==LOW && playFlag==1 && (currentTime >= startTime+1000))
{
  Serial.println("Interrupt Activated");
musicPlayer.stopPlaying();
playFlag=0;
}
 //if (val==LOW && switchval==HIGH && playFlag==0)
  if (val==LOW)//when button is pressed,play sound
  {
    Serial.println("Sound Button Activated");
    // print a random number from 0 to 80
    int randNumber = random(80);//pick a random file from the soundBank
    startTime=millis();
      musicPlayer.startPlayingFile(soundBank[randNumber]);
    endTime=startTime+timerBank[randNumber];
    playFlag=1;
 
   //allow sound to play 
  }
if (playFlag==1 && (currentTime>endTime))
{
playFlag=0;
 musicPlayer.stopPlaying();
}  
}
I have no idea if it will work but it compiles. If it does not work post the symptoms of the failure.
No sound plays. The print statement "sound button activated" is hit and a print statement I added to the last if block is hit sometime later but no sound comes out. I'm using speakers as output btw.

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

Return to “Other Products from Adafruit”