Este es el código de la placa desarrollada:
#include "DFRobotDFPlayerMini.h"
#include "SoftwareSerial.h"
const int PIN_SENSOR = A0;
SoftwareSerial mySoftwareSerial(D1, D2); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
// Configuraciones de sensor
const int UMBRAL = 300; // Umbral para que active o no
unsigned int lecturaSENSOR = 0; // Estado inicial
const int ANTIRREBOTE = 1000;
boolean sensor_on = false; // Booleano para detectar activacion de sensor
boolean play = false; // Booleano para saber si se esta reproduciendo
void leerSENSOR() // Para realice lectura del sensor constantemente.
{
lecturaSENSOR = analogRead(PIN_SENSOR);
if (lecturaSENSOR > UMBRAL) {
sensor_on = true;
}
}
void setup() {
pinMode(PIN_SENSOR, INPUT);
mySoftwareSerial.begin(9600);
Serial.begin(115200);
Serial.println("SETUP");
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
if (!myDFPlayer.begin(
mySoftwareSerial)) { // Use softwareSerial to communicate with mp3.
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while (true)
;
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.setTimeOut(500); // Set serial communication time out 500ms
//----Set volume----
myDFPlayer.volume(25); // Set volume value (0~30).
//----Set different EQ----
myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
//----Set device we use SD as default----
// myDFPlayer.outputDevice(DFPLAYER_DEVICE_U_DISK);
myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
}
void loop() {
leerSENSOR();
if (sensor_on) {
sensor_on = false;
if (!play) {
play = true;
myDFPlayer.play(1); // Play the first mp3
} else {
play = false;
myDFPlayer.pause(); // Pause mp3
}
delay(ANTIRREBOTE);
}
}