Print variable value to IDE serial monitor

Hello, I am a bit new to using Arduino hardware and IDE software (2.2.1). I am a not a programming expert but have enough coding knowledge to get things working mostly. I would like a little guidance to a project of mine using an UNO board and a MP3 player. I have uploaded the hardware breadboard diagram and below is the code I am using to run the mp3 paler. The player works fine functionally with the code I have loaded to the UNO board. But, I am needing a little guidance to be able to print a couple of variable values (volume level and song name) as the player is running and also how to send a CMD code from the serial monitor interface to the connected UNO board such as the command to change the volume level to a new value. Here is the code that I am using. I am not the originator of the code below. But, it works as described in the uploaded hardware diagram file. The MP3 player I am using is the DFROBOT DFPlayer MP3 Mini. Where and what code is needed to see the current volume level and song file name in the serial monitor window? And how do I send a command from the serial monitor window to change the current volume level to a new level? This guidance will help me learn how this is done in future coding I plan on doing. Thank You, Joe

#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;
int buttonPrevious = 4;
int volumeUP = 5;
int volumeDOWN = 6;
int volume = 15;
boolean isPlaying = false;

void setup () {

pinMode(buttonPause, INPUT);
digitalWrite(buttonPause,HIGH);
pinMode(buttonNext, INPUT);
digitalWrite(buttonNext,HIGH);
pinMode(buttonPrevious, INPUT);
digitalWrite(buttonPrevious,HIGH);
pinMode(volumeUP, INPUT);
pinMode(volumeDOWN, INPUT);
digitalWrite(volumeUP, HIGH);
digitalWrite(volumeDOWN, 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();
    }
  }

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

  if(digitalRead(volumeUP) == ACTIVATED)
{
volumeINC();
}
if(digitalRead(volumeDOWN) == ACTIVATED)
{
volumeDEC();
}
}

void playFirst()
{
execute_CMD(0x3F, 0, 0);
delay(500);
execute_CMD(0x06, 0, volume);
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 volumeINC()
{
volume = volume+1;
if(volume==31)
{
volume=30;
}
execute_CMD(0x06, 0, volume);
delay(500);
}
void volumeDEC()
{
volume = volume-1;
if(volume==-1)
{
volume=0;
}
execute_CMD(0x06, 0, volume);
delay(500);
}

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

Add a Serial.begin() to setup().
Write a function to display the settings that you're interested in.

To prevent the serial monitor to be spammed with messages you can remember the last volume and only print if it changed. For the volume, add a variable (before setup()) to remember the last volume as shown below.

int buttonNext = 2;
int buttonPause = 3;
int buttonPrevious = 4;
int volumeUP = 5;
int volumeDOWN = 6;
int volume = 15;
int lastVolume = 15;  // <--------------
boolean isPlaying = false;

I called the function display() and you can call it from loop()

void display()
{
  if (volume != lastVolume)
  {
    Serial.print(F("Volume changed to "));
    Serial.println(volume);
  }
}

It does not look like you keep track of the track that is playing. Using some variables you can also keep track of that and display the in the display() function.

Hello sterretje,

Thanks for your assistance to help with my project question. I was able to use your code suggestion to use it to increase or decrease the MP3 player volume and see volume status on the serial monitor. Very helpful. I am now trying to implement your suggested code changes for another enhancement to turn on or off the cmd to make the player repeat or not repeat the song being played by the use of one of the buttons. In this case, I changed the buttonNext to buttonLoop and added some more code for this purpose. Unfortunately, I don't have the code working right and am stuck. I think this is possible but can't seem to get it nailed down. Do you have any suggestions to make this repeat on, repeat off to work?

Thank you
Joseph

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.