I've got an Adafruit VS1053 shield hooked up with a button. I want to play ambient star trek noises and then a blast off countdown when a button is pushed. I'm using and if / else and it's not working. Code below.
Currently, the ambient sounds play and then the push button doesn't change anything when pressed. If I take out all of the content within the else brackets. The button works as expected.
This is for a "spaceship toy" for my kiddos, an image is attached. I want the ambient sounds on when that push button isn't pushed. The audio is controlled with a separate Arduino that shares that push button.
Any thoughts?
// 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 SHIELD_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_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);
int button = 5;
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"));
SD.begin(CARDCS); // initialise the SD card
// Set volume for left, right channels. lower numbers == louder volume!
musicPlayer.setVolume(5,5);
// Timer interrupts are not suggested, better to use DREQ interrupt!
//musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT); // timer int
// 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
musicPlayer.GPIO_pinMode(2, INPUT);
pinMode(button, INPUT);
}
void loop() {
int buttonstate = digitalRead(button);
//When button PIN 5 is pressed play track003.mp3
if ((buttonstate == HIGH)) {
Serial.println(("Playing Blast Off Sounds"));
musicPlayer.playFullFile("/track003.mp3");
}
else{
Serial.println(("Playing Ambient Bridge Sounds"));
musicPlayer.playFullFile("/startrek.mp3");}
}
@Mark T I do have an external pull-up.
To elaborate more, The pushbutton works when I take out the ambient sound code. When the else argument is blank the push button plays the other track as expected. When I add in the else content and the push button gets pressed the ambient sound just keeps playing and nothing happens.
@halloweenrick, not error messages. Grounds are tied together.
So then that makes me think its a programming issue - in that there is no "cue" for the ELSE command to activate. It reads that IF happens when a button push occurs HIGH, but nothing to activate when ELSE occurs- no button push, no actual timing of the mp3 length, etc. I think you need to look at State Machine language here, in that:
Case 1: Button HIGH- activates MP3 player
Case 2: Button LOW or length of time (delay or millis timing of mp3 length)
Try adding a DELAY of the MP3 Length of the Blast off sounds, I'm sure it can't be very long as a quick and dirty solution to see if that helps any in State Machine/Case code.
Here is my current code. The ambient sounds called the startrek.mp3 track play, but when I hit the pushbutton nothing happens. It keeps playing the startrek sound and does not play track003.mp3.
What in the world!?
// 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 SHIELD_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_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 shield-example object!
Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
int button = 5;
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"));
SD.begin(CARDCS); // initialise the SD card
// Set volume for left, right channels. lower numbers == louder volume!
musicPlayer.setVolume(5,5);
// Timer interrupts are not suggested, better to use DREQ interrupt!
//musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT); // timer int
// 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
musicPlayer.GPIO_pinMode(2, INPUT);
pinMode(button, INPUT);
}
void loop() {
int buttonstate = digitalRead(button);
//When button PIN 5 is pressed play track003.mp3
if ((buttonstate == HIGH)) {
Serial.println(("Playing Blast Off Sounds"));
musicPlayer.startPlayingFile("/track003.mp3");
while (musicPlayer.playingMusic) {
musicPlayer.setVolume(25,25);
delay(12000);
}
}
else {
Serial.println(("Playing startrek"));
musicPlayer.startPlayingFile("/startrek.mp3");
while (musicPlayer.playingMusic) {
musicPlayer.setVolume(5,5);
delay(15000);
}
}
}
Oh hey! This shield has Interrupt- which is very similiar to the DF Mini Player's Advertise command- which will allow you to play another MP3 file on a button push, and the music will resume where it left off! Its right here in the code you posted:
// If DREQ is on an interrupt pin (on uno, #2 or #3) we can do background[color=#222222][/color]
// audio playing[color=#222222][/color]
musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT); // DREQ int[color=#222222][/color]
musicPlayer.GPIO_pinMode(2, INPUT);[color=#222222][/color]
pinMode(button, INPUT);
See if you can move your Pushbutton to Pin 2 or 3 and see if your library has an Interrupt example you can follow. That should solve everything.
What happens in the serial monitor when you just print inside the if/else?
How long do you hold the button? Does holding it down for a long time make it work?
Remove the delay in the ambient section. Leave it in the takeoff.
If print works put the play first and see what happens.