Hello,
I'm using the following code to have a sound sensor trigger files at random from an MP3 shield paired with an Arduino Uno. This code has worked in the past, but for some reason the sound is no longer audible. I've double-checked the hardware connections, wiring, and SD card formatting. It all is working and is fine when I use the example simple code for the VS1053 library.
Does anyone know if there has been a firmware update to this MP3 shield? It's been 3 years since I last used it, so maybe something has changed. The serial monitor is showing that the sounds are being triggered, but nothing.
/***************************************************
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 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.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 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);
//breath_sensor consts/variables
const int breathSensor = A0; // the electret mic sensor is connected to analog pin 0
const int threshold = 225; // threshold value to decide when the detected sound is a breath or not
int sensorReading = 0;
const int volumePin = A1;
//the current volume level, set to min at start
byte volumeState = 254;
void setup() {
Serial.begin(9600);
Serial.println("Adafruit VS1053 Simple Test");
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"));
if (!SD.begin(CARDCS)) {
Serial.println(F("SD failed, or not present"));
while (1); // don't do anything more
}
// list files
printDirectory(SD.open("/"), 0);
// Set volume for left, right channels. lower numbers == louder volume!
musicPlayer.setVolume(10,10);
// If DREQ is on an interrupt pin (on uno, #2 or #3) we can do background
// audio playing
musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT); // DREQ int
}
void loop() {
Serial.println("loop");
sensorReading = analogRead(breathSensor);
// if the sensor reading is greater than the threshold:
if (sensorReading >= threshold) {
int randNumber = random(1,40);
//Serial.println("breath");
Serial.println(randNumber);
musicPlayer.stopPlaying();
if(randNumber == 1){musicPlayer.startPlayingFile("track002.mp3"); }
else if (randNumber == 2){musicPlayer.startPlayingFile("track003.mp3"); }
else if (randNumber == 3){musicPlayer.startPlayingFile("track004.mp3"); }
else if (randNumber == 4){musicPlayer.startPlayingFile("track005.mp3"); }
else if (randNumber == 5){musicPlayer.startPlayingFile("track006.mp3"); }
else if (randNumber == 6){musicPlayer.startPlayingFile("track007.mp3"); }
else if (randNumber == 7){musicPlayer.startPlayingFile("track008.mp3"); }
else if (randNumber == 8){musicPlayer.startPlayingFile("track009.mp3"); }
else if (randNumber == 9){musicPlayer.startPlayingFile("track010.mp3"); }
else if (randNumber == 10){musicPlayer.startPlayingFile("track011.mp3"); }
else if (randNumber == 11){musicPlayer.startPlayingFile("track012.mp3"); }
else if (randNumber == 12){musicPlayer.startPlayingFile("track013.mp3"); }
else if (randNumber == 13){musicPlayer.startPlayingFile("track014.mp3"); }
else if (randNumber == 14){musicPlayer.startPlayingFile("track015.mp3"); }
else if (randNumber == 15){musicPlayer.startPlayingFile("track016.mp3"); }
else if (randNumber == 16){musicPlayer.startPlayingFile("track017.mp3"); }
else if (randNumber == 17){musicPlayer.startPlayingFile("track018.mp3"); }
else if (randNumber == 18){musicPlayer.startPlayingFile("track019.mp3"); }
else if (randNumber == 19){musicPlayer.startPlayingFile("track020.mp3"); }
else if (randNumber == 20){musicPlayer.startPlayingFile("track021.mp3"); }
else if (randNumber == 21){musicPlayer.startPlayingFile("track022.mp3"); }
else if (randNumber == 22){musicPlayer.startPlayingFile("track023.mp3"); }
else if (randNumber == 23){musicPlayer.startPlayingFile("track024.mp3"); }
else if (randNumber == 24){musicPlayer.startPlayingFile("track025.mp3"); }
else if (randNumber == 25){musicPlayer.startPlayingFile("track026.mp3"); }
else if (randNumber == 26){musicPlayer.startPlayingFile("track027.mp3"); }
else if (randNumber == 27){musicPlayer.startPlayingFile("track028.mp3"); }
else if (randNumber == 28){musicPlayer.startPlayingFile("track029.mp3"); }
else if (randNumber == 29){musicPlayer.startPlayingFile("track030.mp3"); }
else if (randNumber == 30){musicPlayer.startPlayingFile("track031.mp3"); }
else if (randNumber == 31){musicPlayer.startPlayingFile("track032.mp3"); }
else if (randNumber == 32){musicPlayer.startPlayingFile("track033.mp3"); }
else if (randNumber == 33){musicPlayer.startPlayingFile("track034.mp3"); }
else if (randNumber == 34){musicPlayer.startPlayingFile("track035.mp3"); }
else if (randNumber == 35){musicPlayer.startPlayingFile("track036.mp3"); }
else if (randNumber == 36){musicPlayer.startPlayingFile("track037.mp3"); }
else if (randNumber == 37){musicPlayer.startPlayingFile("track038.mp3"); }
else if (randNumber == 38){musicPlayer.startPlayingFile("track039.mp3"); }
else if (randNumber == 39){musicPlayer.startPlayingFile("track040.mp3"); }
else if (randNumber == 40){musicPlayer.startPlayingFile("track041.mp3"); }
}
// if nothing plays start background
if (musicPlayer.stopped()) {
Serial.println("restarting background file");
musicPlayer.startPlayingFile("track001.mp3");
}
//check the volume and set it
checkVolume();
delay(200);
}
//checks the value of the potentiometer
//if it has changed by 2 then set the new volume
void checkVolume()
{
//read the state of the volume potentiometer
int read = analogRead(volumePin);
//set the range of the volume from max=0 to min=254
//(limit max volume to 20 and min to 60)
byte state = map(read, 0, 1023, 20, 60);
//recognize state (volume) changes in steps of two
if (state < volumeState - 1 || state > volumeState + 1)
{
//remember the new volume state
volumeState = state;
//set volume max=0, min=254
musicPlayer.setVolume(volumeState, 254);
}
}
/// 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();
}
}
Any ideas or help appreciated!