Hi Everyone!
I am doing a school project that is due very soon, and I am using DfPlayerMini TF-16P, an Arduino Uno and 2 buttons. I was trying to make it so that when I push one button it asks one question, and when I push the other one it asks a different question. I was wondering if anyone could please make me code that would work for this project. I have tried the code below but the code is not working. The buttons don't work, and there is no sound. I have attached my wiring diagram below (sorry if it is bad, this is my first time creating a circuit schematic)
#include "SoftwareSerial.h"
SoftwareSerial mySerial(10, 11);
# define Start_Byte 0x7E
# define Version_Byte 0xFF
# define Command_Length 0x06
# define End_Byte 0xEF
# define Acknowledge 0x00 //Returns info with command 0x41 [0x01: info, 0x00: no info]
# define ACTIVATED LOW
int buttonNext = 2;
int buttonPause = 3;
boolean isPlaying = false;
void setup () {
digitalWrite(buttonPause, HIGH);
digitalWrite(buttonNext, HIGH);
mySerial.begin (9600);
delay(1000);
playFirst();
isPlaying = true;
}
void loop () {
if (digitalRead(buttonPause) == ACTIVATED)
{
if (isPlaying)
{
pause();
isPlaying = false;
} else
{
isPlaying = true;
play();
}
}
if (digitalRead(buttonNext) == ACTIVATED)
{
if (isPlaying)
{
playNext();
}
}
}
void playFirst()
{
execute_CMD(0x3F, 0, 0);
delay(500);
setVolume(20);
delay(500);
execute_CMD(0x11, 0, 0);
delay(500);
}
void pause()
{
execute_CMD(0x0E, 0, 0);
delay(500);
}
void play()
{
execute_CMD(0x0D, 0, 0);
delay(500);
}
void playNext()
{
execute_CMD(0x01, 0, 1);
delay(500);
}
void setVolume(int volume)
{
execute_CMD(0x06, 0, volume); // Set the volume (0x00~0x30)
delay(2000);
}
void execute_CMD(byte CMD, byte Par1, byte Par2)
// Excecute the command and parameters
{
// Calculate the checksum (2 bytes)
word checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
// Build the command line
byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge,
Par1, Par2, highByte(checksum), lowByte(checksum), End_Byte
};
//Send the command line to the module
for (byte k = 0; k < 10; k++)
{
mySerial.write( Command_line[k]);
}
}
What are the titles of the two MP3 files (that are your 2 questions) on the microSD?
Here are just 2 simple mp3's that I have recorded with my voice, nothing special
Oh sorry, they are 0001.mp3 and 0002.mp3
I hope you can find 'Library Manager'.
You need to download a library --
DFRobotDFPlayerMini.h
I have just installed it, but it is still the same result.
Naturally.
You need a sketch that works with that library.
would you be able to recommend me one that would work with my setup? Thanks
These should play your two files.
Next step is to do that as a result of 'buttonpushing'.
Tell me if it works or not.
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial mySerial(10, 11);
// Create the Player object
DFRobotDFPlayerMini player;
void setup()
{
Serial.begin(19200);
softwareSerial.begin(9600);
if (player.begin(softwareSerial))
{
Serial.println("OK");
// Set volume to maximum (0 to 30).
player.volume(9); //30 is very loud
}
else
{
Serial.println("Connecting to DFPlayer Mini failed!");
}
//pinMode(13,OUTPUT);
digitalWrite(13,LOW);
}
void loop()
{
Serial.println("Playing #1");
player.play(1);
delay(5000);
Serial.println("Playing #2");
player.play(2);
delay(5000);
}
Thank you. It works, I just had to fix it a little bit. Just now we have to figure out how to code in the buttons
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial mySerial(10, 11);
// Create the Player object
DFRobotDFPlayerMini player;
void setup()
{
Serial.begin(19200);
mySerial.begin(9600);
if (player.begin(mySerial))
{
Serial.println("OK");
// Set volume to maximum (0 to 30).
player.volume(9); //30 is very loud
}
else
{
Serial.println("Connecting to DFPlayer Mini failed!");
}
//pinMode(13,OUTPUT);
digitalWrite(13,LOW);
}
void loop()
{
Serial.println("Playing #1");
player.play(1);
delay(5000);
Serial.println("Playing #2");
player.play(2);
delay(50000000);
}
Too Loud?
(No way!)
delay(50000000);
That'll be a while.
Yeah, I was just checking how loud it can get . The second audio is just free calm music (only for testing purposes), and the audio is long. Once I add the second question I will adjust the delay appropriately.
The volume is surprising, depending on the speaker. (Put it in a plastic cup.)
I am using a 4ohm 3-watt speaker. It can get pretty loud!
Oh my god! And I thought mine was loud!
I am still very new to C++, and I am struggling severely with the coding. Whatever I try, it always fails .
The pushbutton goes from Pin7 (D7) to GND.
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial mySerial(10, 11);
// Create the Player object
DFRobotDFPlayerMini player;
const byte pb = 7;
bool pr = false;
void setup()
{
pinMode(pb, INPUT_PULLUP);
Serial.begin(19200);
mySerial.begin(9600);
if (player.begin(mySerial))
{
Serial.println("OK");
// Set volume to maximum (0 to 30).
player.volume(9); //30 is very loud
}
else
{
Serial.println("Connecting to DFPlayer Mini failed!");
}
}
void loop()
{
clickIt();
change();
}
void clickIt()
{
pr = !pr;
byte aa;
do
{
aa = digitalRead(pb);
}while(aa);
}
void change()
{
if(pr)
{
Serial.println("Playing #1");
player.play(1);
}
else
{
Serial.println("Playing #2");
player.play(2);
}
delay(200); // cheap debounce
}