Skip to content

Instantly share code, notes, and snippets.

@mpflaga
Last active December 21, 2015 23:29
Show Gist options
  • Save mpflaga/6382516 to your computer and use it in GitHub Desktop.
Save mpflaga/6382516 to your computer and use it in GitHub Desktop.
Short Example of how to use Bounce Library to control SFEMP3Shield library
/**
* \file MP3ButtonPlayer.ino
*
* \brief Example sketch of using the MP3Shield Arduino driver using buttons,
* with improved debounce library
* \remarks comments are implemented with Doxygen Markdown format
*
* \author Michael P. Flaga
*
* This sketch demonstrates the use of digital input pins used as buttons as
* NEXT, PLAY and STOP to control the tracks that are to be played.
* Where PLAY or STOP will begin or cancel the stream of track000.mp3 through
* track999.mp3, as indexed by NEXT, begining with 0.
*
* Additionally PLAY0, 1 and 2 have been added to play specific tracks.
* \note Use this example uses the bounce library to provide debouncing fuctions. The following library of bounce found at https://github.com/mpflaga/Arduino-Bounce
*
* \note the pins have internal pull-ups enabled, assuming they are supported.
*
* \warning do not use bounce2.h, which is a more primative version of the above library and is advocated by Arduino's website at http://playground.arduino.cc/code/bounce in short it has less features.
*/
// libraries
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>
#include <Bounce.h>
/**
* \breif Macro for the debounced NEXT pin, with pull-up
*/
#define B_NEXT A0
/**
* \breif Macro for the debounced STOP pin, with pull-up
*/
#define B_STOP A1
/**
* \breif Macro for the debounced PLAY pin, with pull-up
*/
#define B_PLAY A2
/**
* \breif Macro for the debounced PLAY Track 0 pin, with pull-up
*/
#define B_PLAYTRACK0 A3
/**
* \breif Macro for the debounced PLAY Track 0 pin, with pull-up
*/
#define B_PLAYTRACK1 A4
/**
* \breif Macro for the debounced PLAY Track 0 pin, with pull-up
*/
#define B_PLAYTRACK2 A5
/**
* \breif Macro for the Debounce Period [milliseconds]
*/
#define BUTTON_DEBOUNCE_PERIOD 20 //ms
/**
* \brief Object instancing the SdFat library.
*
* principal object for handling all SdCard functions.
*/
SdFat sd;
/**
* \brief Object instancing the SFEMP3Shield library.
*
* principal object for handling all the attributes, members and functions for the library.
*/
SFEMP3Shield MP3player;
/**
* \brief Object instancing the Next Button.
*/
Bounce b_Next = Bounce( B_NEXT , BUTTON_DEBOUNCE_PERIOD );
/**
* \brief Object instancing the Stop Button library.
*/
Bounce b_Stop = Bounce( B_STOP , BUTTON_DEBOUNCE_PERIOD );
/**
* \brief Object instancing the Play Button library.
*/
Bounce b_Play = Bounce( B_PLAY , BUTTON_DEBOUNCE_PERIOD );
/**
* \brief Object instancing the Play Track 0 Button library.
*/
Bounce b_PlayTrack0 = Bounce( B_PLAYTRACK0 , BUTTON_DEBOUNCE_PERIOD );
/**
* \brief Object instancing the Play Track 1 Button library.
*/
Bounce b_PlayTrack1 = Bounce( B_PLAYTRACK1 , BUTTON_DEBOUNCE_PERIOD );
/**
* \brief Object instancing the Play Track 2 Button library.
*/
Bounce b_PlayTrack2 = Bounce( B_PLAYTRACK2 , BUTTON_DEBOUNCE_PERIOD );
/**
* \brief Index of the current track playing.
*
* Value indicates current playing track, used to populate "x" for playing the
* filename of "track00x.mp3" for track000.mp3 through track254.mp3
*/
int8_t current_track = 0;
//------------------------------------------------------------------------------
/**
* \brief Setup the Arduino Chip's feature for our use.
*
* After Arduino's kernel has booted initialize basic features for this
* application, such as Serial port and MP3player objects with .begin.
*/
void setup() {
Serial.begin(115200);
pinMode(B_NEXT, INPUT_PULLUP);
pinMode(B_STOP, INPUT_PULLUP);
pinMode(B_PLAY, INPUT_PULLUP);
pinMode(B_PLAYTRACK0, INPUT_PULLUP);
pinMode(B_PLAYTRACK1, INPUT_PULLUP);
pinMode(B_PLAYTRACK2, INPUT_PULLUP);
if(!sd.begin(9, SPI_HALF_SPEED)) sd.initErrorHalt();
if (!sd.chdir("/")) sd.errorHalt("sd.chdir");
MP3player.begin();
MP3player.setVolume(10,10);
Serial.println(F("Looking for Buttons to be depressed..."));
}
//------------------------------------------------------------------------------
/**
* \brief Main Loop the Arduino Chip
*
* This is called at the end of Arduino kernel's main loop before recycling.
* And is where the user's is executed.
*
* \note If the means of refilling is not interrupt based then the
* MP3player object is serviced with the availaible function.
*/
void loop() {
// Below is only needed if not interrupt driven. Safe to remove if not using.
#if defined(USE_MP3_REFILL_MEANS) \
&& ( (USE_MP3_REFILL_MEANS == USE_MP3_SimpleTimer) \
|| (USE_MP3_REFILL_MEANS == USE_MP3_Polled) )
MP3player.available();
#endif
if (b_Play.update()) {
if (b_Play.fallingEdge()) {
Serial.print(F("B_PLAY pressed, Start Playing Track # "));
Serial.println(current_track);
MP3player.playTrack(current_track);
}
}
if (b_Stop.update()) {
if (b_Stop.fallingEdge()) {
Serial.print(F("B_STOP pressed, Stopping Track #"));
Serial.println(current_track);
MP3player.stopTrack();
}
}
if (b_Next.update()) {
if (b_Next.fallingEdge()) {
Serial.print(F("B_NEXT pressed, Start Playing Next Track #"));
Serial.println(++current_track);
MP3player.stopTrack();
MP3player.playTrack(current_track);
}
}
if (b_PlayTrack0.update()) {
if (b_Play.fallingEdge()) {
Serial.println(F("B_PLAYTRACK0 pressed, Start Playing Track # 0"));
MP3player.playTrack(0);
}
}
if (b_PlayTrack1.update()) {
if (b_Play.fallingEdge()) {
Serial.println(F("B_PLAYTRACK0 pressed, Start Playing Track # 1"));
MP3player.playTrack(1);
}
}
if (b_PlayTrack2.update()) {
if (b_Play.fallingEdge()) {
Serial.println(F("B_PLAYTRACK2 pressed, Start Playing Track # 2"));
MP3player.playTrack(2);
}
}
//Do something. Have fun with it.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment