Very new to programming but I’m having some success with a test program intended to receive an input from another Uno to an Adafruit MusicMaker via A0 in order to turn on and off randomly selected .MP3 files. I’ve verified the pin is pulled up and down properly and can play files using musicPlayer.playFullFile, but I want the play to stop when A0 is pulled low so I believe I need to use musicPlayer.startPlayingFile. I’m currently trying a Last State routine but the sound files stop and loop. I’m not certain if my current state routine is screwed up or I’m using musicPlayer.startPlayingFile wrong but I feel I’ve got both screwed up and getting frustrated. Any help would be greatly appreciated.
// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
int comState = 2;
int lastComState = 2;
//Communication pin from lighting Arduino (needs grounded)
// 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);
char myFile[3] = "0";
void setup()
{
Serial.begin(9600);
pinMode(A0,INPUT); //Communication pin from lighting Arduino (needs grounded)
Serial.println("Adafruit VS1053 Simple Test");
musicPlayer.begin(); // initialise the music player
Serial.println(F("VS1053 found"));
if (!SD.begin(CARDCS))
{
Serial.println(F("SD failed, or not present"));
while (1); // don't do anything more
}
}
void loop()
{
char myFile[7];
comState = digitalRead(A0);
if (comState != lastComState)
Serial.println(comState);
Serial.println(lastComState);
{
if (comState == LOW)
{
musicPlayer.stopPlaying();
lastComState = comState;
delay(500);
}
if (comState == HIGH)
{
Serial.println(F("Playing cabinet track"));
sprintf(myFile, "%d.MP3", random(81,99));
musicPlayer.startPlayingFile(myFile);
Serial.println(myFile);
lastComState = comState;
delay(500);
}
delay(250);
}
}
In the IDE, if you auto format your code (Ctrl-T) it will indent lines properly. If will show you that your first if() statement does not have any curly braces associated with it so only the very next line is affected by it. All the rest of your code happens every time through loop() which is not what you want. You only want to start playing when the pin is HIGH or stop when the pin is HIGH.
Look at the State Change Example in the IDE (File->examples->02.digital->State Change Detection)
You also need an extern pull-up or pull-down on you A0 pin. Do you have it installed? It is much more common to use the internal pullup resistors by defining the pin as INPUT_PULLUP.
Thanks for your time. I moved my Serial.printlns inside my curly brackets so things look better and everything works well with playFullFile but it won’t stop and I can’t get startPlayingFile to work with a current state routine. I believe I need to be using useInterrupt and have found an example to show me how. Thanks again
I’ve incorporated useInterrupt and got sound starting and stopping as intended but the sound is garbled as if files are being quickly started and stopped. My last state routine seems to be working and I feel I’m not using useInterrupt correctly. Any ideas?
// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
int comState = 2;
int lastComState = 2;
const int comPin = 3;
//Communication pin from lighting Arduino (needs grounded)
// 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);
char myFile[3] = "0";
void setup()
{
Serial.begin(9600);
pinMode(comPin,INPUT); //Communication pin from lighting Arduino (needs grounded)
Serial.println("Adafruit VS1053 Simple Test");
musicPlayer.begin(); // initialise the music player
musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);
musicPlayer.setVolume(20,20);
Serial.println(F("VS1053 found"));
if (!SD.begin(CARDCS))
{
Serial.println(F("SD failed, or not present"));
while (1); // don't do anything more
}
}
void loop()
{
char myFile[7];
comState = digitalRead(comPin);
if (comState != lastComState)
{
Serial.println(comState);
Serial.println(lastComState);
if (comState == LOW)
{
musicPlayer.stopPlaying();
lastComState = comState;
delay(500);
}
if (comState == HIGH)
{
Serial.println(F("Playing cabinet track"));
sprintf(myFile, "%d.MP3", random(81,99));
Serial.println(myFile);
musicPlayer.startPlayingFile(myFile);
lastComState = comState;
delay(500);
}
delay(250);
}
}
If you look at the interrupt example that comes with the library, the statement musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT); uses interrupts on the DREQ pin which you have defined as 3. You also have your comPin set to 3. Probably should use a different pin...
You also need a pull-up or pull-down resistor on your comPin unless you define it as INPUT_PULLUP. If it is floating (unconnected to either ground or VCC through a resistor) then you can get any bogus reading...