Hello,
I have a Nano v3, a DFRobots Mini mp3 Player, and 3 arcade buttons. The program does this: On button press, play corresponding track number on the DFplayer mini. If you press the button which corresponds to the currently playing track, stop the track. If you press the button which corresponds to a different track, stop the track and start playing the new one.
Currently a 9v battery only lasts about 2 days. I'd like to incorporate some power saving libraries or circuits and need advice on how to approach this. I'd rather do a library but don't know how to modify the code.
//This code adopted from Bounce 2 examples and DFRobotPlayer mini Examples
//Hardware is a Nano v3.0, a DFRobot DFplayerMini, and a 9V battery
// INCLUDES
#include <Bounce2.h>
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
//DEFINITIONS
#define NUM_BUTTONS 3
const uint8_t BUTTON_PINS[NUM_BUTTONS] = {5, 6, 7};
int activeButton;
Bounce * buttons = new Bounce[NUM_BUTTONS];
SoftwareSerial mySoftwareSerial(10, 11); // use digital 10 for RX, use digital 11 for TX
DFRobotDFPlayerMini myDFPlayer;
void setup() {
mySoftwareSerial.begin(9600); //open serial communication with DFPlayer on port 9600
Serial.begin(115200);
Serial.println();
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while (true);
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.volume(25);
for (int i = 0; i < NUM_BUTTONS; i++) {
buttons[i].attach( BUTTON_PINS[i] , INPUT_PULLUP ); //setup the bounce instance for the current button
buttons[i].interval(25); // interval in ms
}
}
void loop() {
bool needToToggleLed = false;
int buttonPressed;
for (int i = 0; i < NUM_BUTTONS; i++) {
buttonPressed = i+1;
// Update the Bounce instance :
buttons[i].update();
// If it fell, flag the need to toggle the LED
if ( buttons[i].fell() ) {
needToToggleLed = true;
if(activeButton == buttonPressed){
myDFPlayer.stop();
activeButton = 0;
} else {
activeButton = buttonPressed;
myDFPlayer.play(buttonPressed);
}
}
}
}