I'm working on a star wars droid with 6 voice recordings and 60 tracks of music. I'm using a dfplayer an Arduino uno a switch and an amplifier. The idea was to get the voice files to play randomly every 10 to 30 seconds if the switch was engaged and if it wasn't play the music randomly. It plays a second and buzzes through.
I'm using this code, any help would be appreciated:
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial softwareSerial(10,11);
const int buttonPin = 2; // Pin where the button is connected
int buttonState = 0; // Variable to store the state of the button
DFRobotDFPlayerMini myPlayer;
void setup() {
pinMode(buttonPin, INPUT); // Set the button pin as input
}
void loop() {
buttonState = digitalRead(buttonPin); // Read the state of the button
if (buttonState == HIGH) { // If the button is pressed
{
int i=0;
while (i < 10)
{
// Generate a random delay between 1000 and 5000 milliseconds (1 to 5 seconds)
int randomDelay = random(10000, 30000);
int randomTrack1 = random(61,66);
softwareSerial.begin(9600);
myPlayer.begin(softwareSerial);
myPlayer.volume(25);
myPlayer.play(randomTrack1);
delay(randomDelay);
// The stuff to repeat
i++;
}
}
} else {
{
int i=0;
while (i < 60)
{
int randomTrack2 = random(0,60);
softwareSerial.begin(9600);
myPlayer.begin(softwareSerial);
myPlayer.volume(25);
myPlayer.play(randomTrack2);
delay(500);
// The stuff to repeat
i++;
}
}
}
}
Is there a reason why you are calling the begin() methods repeatedly in each loop? Unless there is some reason that I am unaware of, it seems like the following code block could be executed just once, in the setup() function:
How to have this all powered? If you are trying to run your DFplayer from the Uno, it doesn't have enough power if the volume gets too high. You need an external power supply.
Then you need an external pull-up resistor in your circuit. An easier method is to declare the pin as INPUT_PULLUP and use the boards internal pull-up resistor. Otherwise, when the button is not pressed, the pin is not connected to anything, aka "floating" which can read as HIGH or LOW.
something like this should get you close... (untested)
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial softwareSerial(10, 11);
const int buttonPin = 2; // Pin where the button is connected
DFRobotDFPlayerMini myPlayer;
unsigned long startTime; // start time when something begins to play
unsigned long randomDelay; // time to delay until next play event
// End variable needs to be last track +1 since that is how random works
// 60 music tracks 1..60
const int musicTrackStart = 1;
const int musicTrackEnd = 61;
// 6 voice tracks 61..66
const int voiceTrackStart = 61;
const int voiceTrackEnd = 67;
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
if (!myDFPlayer.begin(softwareSerial) {
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) {
delay(0);
}
}
Serial.println(F("Ready."));
pinMode(buttonPin, INPUT_PULLUP); // Set the button pin as input
myPlayer.volume(25);
}
void loop() {
// check the state of the player
if ( myPlayer.readState() == Busy ) {
// something is playing, nothing else to do
}
else {
// nothing is playing, see if it is time for another track
if ( millis() - startTime >= randomDelay ) {
// delay is over, start another track
int buttonState = digitalRead(buttonPin); // Read the state of the button
int randomTrack;
if (buttonState == LOW) {
// button is pressed
// play a random voice and wait 10-30 seconds
randomTrack = random(voiceTrackStart, VoiceTrackEnd);
randomDelay = random(10000, 30000);
}
else {
// button is not pressed
// play random music and wait 500 msec
randomTrack = random(musicTrackStart, musicTrackEnd);
randomDelay = 500;
}
myPlayer.play(randomTrack);
startTime = millis();
Serial.print("Playing track: ");
Serial.print(randomTrack);
Serial.print(" and waiting ");
Serial.print(randomDelay);
Serial.println(" msec");
}
}
}