Pcomp Week #4 – Analog Input/Output

For this week, I tried to make an mp3 player.

I used a DFPlayer mini, a micro sd card, a speaker, three push buttons, a potentiometer several 10k resistors and some wires to finish this project.

I referred to this youtube tutorial to connect the dfplay mini. After finishing that, I added a potentiometer to the circuit and used map function to let the potentiometer control the volume of the music.

</pre>
#include <DFPlayer_Mini_Mp3.h>

/// MP3 PLAYER PROJECT
/// http://educ8s.tv/arduino-mp3-player/
//////////////////////////////////////////

#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

&nbsp;

# define PotPin A0 //added

int buttonNext = 2;
int buttonPause = 3;
int buttonPrevious = 4;
boolean isPlaying = false;
int oldAnalogReading = -1;

&nbsp;

void setup () {

pinMode(buttonPause, INPUT);
digitalWrite(buttonPause, HIGH);
pinMode(buttonNext, INPUT);
digitalWrite(buttonNext, HIGH);
pinMode(buttonPrevious, INPUT);
digitalWrite(buttonPrevious, HIGH);

mySerial.begin (9600);
Serial.begin (9600);//added

delay(1000);
playFirst();
isPlaying = true;

}

void loop () {

int v = analogRead(PotPin);
if (v != oldAnalogReading ) {
int newv = map(v, 20, 1020, 0, 25);
Serial.println(newv);
setVolume(newv); // added
oldAnalogReading = v;
}

if (digitalRead(buttonPause) == ACTIVATED)
{
if (isPlaying)
{
pause();
isPlaying = false;
} else
{
isPlaying = true;

play();
}
}

if (digitalRead(buttonNext) == ACTIVATED)
{
if (isPlaying)
{
playNext();
}
}

if (digitalRead(buttonPrevious) == ACTIVATED)
{
if (isPlaying)
{
playPrevious();
}
}
}

void playFirst() {

execute_CMD(0x3F, 0, 0);
//setVolume(30);
delay(500);
delay(500);
execute_CMD(0x11, 0, 1);
delay(500);
}

void pause()
{
execute_CMD(0x0E, 0, 0);
delay(500);
}

void play()
{
execute_CMD(0x0D, 0, 1);
delay(500);
}

void playNext()
{
execute_CMD(0x01, 0, 1);
delay(500);
}

void playPrevious()
{
execute_CMD(0x02, 0, 1);
delay(500);
}

void setVolume(int volume)
{
execute_CMD(0x06, 0, volume); // Set the volume (0x00~0x30)
delay(20);
}

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]);
}
}
<pre>

One thought on “Pcomp Week #4 – Analog Input/Output”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s