Hello, I am using a Arduino Mega 2560 and DFPlayer Mini to produce sound for my project, and was wandering, "Can I add a potentiometer to the Arduino to adjust the volume?" Of course, the answer is "Yes!" But, what I want to know is how to make the potentiometer adjust the volume of the music WHILE the music is playing, how do I do that? I tested the code below and the code works just fine (as you will see in the video), except, the code needs to be modified to where I can adjust the volume WHILE music is going on. So, how do I do that? The Arduino is powered via USB and the DFPlayer Mini is powered via a wall adapter. And, yes, their grounds are connected together via a breadboard.
UptownKitten:
Thanks! That was awesome! Great job!
No prob!
UptownKitten:
How does the code work?
The Arduino starts out by sending a command to the DFPlayer to "start" the track. While the track is playing, the Arduino constantly samples the voltage on the pot and sends the corresponding volume to the DFPlayer. The DFPlayer has the ability to change volume while a track is playing. Once enough time has passed and the track is assumed to be complete, it's played again.
TBH, a better example would be to use the player.loop() function instead of player.play():
Okay, so, the Arduino was running its program. It was working. I messed myself up by turning off the power supply and turning it back on AND I unplugged the power from the Arduino via USB. So, then, what happened was that the DFPlayer just stopped working, like, it just suddenly cut out. As if I accidently bumped into a wire. But, there is an exception. When I reset the Arduino it still didn't work. I thought, "Well, maybe there is just a loose connection." But, there wasn't. How did I know that? This is because I tested it with another program, a test program. And, the test program worked just fine. But, the other program that you gave me didn't work. The only thing I changed was "myMP3.play(8);" to "myMP3.loop(8);" and then, it just all of a sudden jacked everything up. I am using the code from the first post too, all I did was modify the code a teensy bit no pun intended. So, I added a delay like you said, still didn't work. I used a test program again, same result as mentioned previously, it worked. So, I don't know what the problem is. I am thinking it is a syntax error in my code. I copied and pasted the code again, still didn't work.
Okay, so I tested the program again, and the only thing that seems to work is when I change
myMP3.loop(8);
back to
myMP3.play(8)
I wonder why that is... Strange... I used the test program again and the .loop didn't work, the only thing that worked was the .play. I guess that the song is too long to be looped. Any explanation?
Try using the official DFRobot lib instead of the _fast version..
I think I recall a loop() issue being brought up before with this. (and other functions not working).. not sure what version you got or if things are updated.
See if that changes your results for .loop().
I dont recall if I have ever changed the volume during the code before.. only during set-up DFPlayer initialization