1149Views5Replies

Author Options:

Arduino DFPlayer MP3 player with Light sensor (LDR Module) as trigger Answered

Hi everyone :)
Yet another question...

So I got my self a DFPlayer mini (mp3 player module) for Arduino, and a nice tutorial on how make it work, but I want to use s Light sensor (LDR Module) instead of a button (I just need play and pause or stop for my gift box)

So at the moment if I connect the LDR instead of the button and give it some light, it turns the music on and off constantly/repeatedly because the LDR is constantly sending a HIGH signal, not just once like the button.

Here is the question part: How to modify the code, so it only turns on the music when it gets HIGH signal and off if it gets LOW ???

Here is the modified code (I took out the auto play and the next and previous commands:

///              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

int buttonPlay = 3;
boolean isPlaying = false;



void setup () {

  pinMode(buttonPlay, INPUT);
  digitalWrite(buttonPlay, HIGH);

  mySerial.begin (9600);
  delay(1000);
  playFirst();
  isPlaying = true;


}



void loop () {

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

void playFirst()
{
  setVolume(20);
}

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

void play()
{
  execute_CMD(0x0D, 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]);
  }
}

Thank you ever so much!!!

Discussions

0
None
EdmonTeulson

6 months ago

So, with the photoresistor linked to the A5 pin, with something like this it works fine for me :

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);

int PinPhotoRes=5;
int Lumiere=0;

void setup()
{
mySoftwareSerial.begin(9600);
Serial.begin(9600);



if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.

while(true);
}


myDFPlayer.volume(30); //Set volume value. From 0 to 30

}

void loop()
{

Lumiere=analogRead(PinPhotoRes);
Serial.println(Lumiere);
if (Lumiere < 600)
{
myDFPlayer.play(1); //Play the first mp3
}

}

0
None
EdmonTeulson

6 months ago

Hey there,
Did you find any working code?
I would be pretty interested...

0
None
EdmonTeulsonEdmonTeulson

Reply 6 months ago

I think you must connect the light sensor to an analog input of your arduino board and in your code put something like when the amount of light has passed a certain threshold it plays, when it's bellow, it stops...

0
None
Nighter3D

2 years ago

Ok this is a quick little one really. The line:

# define ACTIVATED LOW

It says that things are activated when the input goes low. Replace it by

# define ACTIVATED HIGH

and the program will instead start playing when the signal is High and shuts off when LOW. I would suggest reading more on coding really. this is a really easy modification.

0
None
Downunder35m

2 years ago

Go to the Arduino playground and check the tutorials and example code for the use of sensors, switches and such.
For obvious reasons that would have been the place to start and would have provided the answers and code you need.