some minor cleanup

This commit is contained in:
2023-12-08 17:48:05 -08:00
parent c2ff78e0dc
commit 1604378c32

View File

@@ -1,204 +1,201 @@
/***************************************************
DFPlayer - A Mini MP3 Player For Arduino
<https://www.dfrobot.com/product-1121.html>
***************************************************
This example shows the basic function of library for DFPlayer.
Created 2016-12-07
By [Angelo qiao](Angelo.qiao@dfrobot.com)
GNU Lesser General Public License.
See <http://www.gnu.org/licenses/> for details.
All above must be included in any redistribution
****************************************************/
/***********Notice and Trouble shooting***************
1.Connection and Diagram can be found here
<https://www.dfrobot.com/wiki/index.php/DFPlayer_Mini_SKU:DFR0299#Connection_Diagram>
2.This code is tested on Arduino Uno, Leonardo, Mega boards.
****************************************************/
#include "Arduino.h"
#include "DFRobotDFPlayerMini.h"
#if (defined(ARDUINO_AVR_UNO) || defined(ESP8266)) // Using a soft serial port
#include <SoftwareSerial.h>
SoftwareSerial softSerial(/*rx =*/4, /*tx =*/5);
#define FPSerial softSerial
#elif (defined(ARDUINO_attinyxy4))
// #include <SoftwareSerial.h>
// SoftwareSerial softSerial(PIN_PB2, PIN_PB3);
#define FPSerial Serial
#else
#define FPSerial Serial1
#endif
// connection for passive infrared sensor
#define PIR 0
#define SENSORTYPE_PIR 1
// connections for ultrasonic rangefinder
#if (defined(ARDUINO_attinyxy4))
#define TRIGGER PIN_PA4
#define ECHO PIN_PA5
#else
#define TRIGGER 7
#define ECHO 8
#endif
#define SENSORTYPE_ULTRASONIC 2
#define SENSOR_TYPE SENSORTYPE_ULTRASONIC
// #define DEBUG
#if SENSOR_TYPE==SENSORTYPE_ULTRASONIC
#include <Ultrasonic.h>
Ultrasonic rf(TRIGGER, ECHO, 40000UL);
#endif
// bool last=true;
bool playing=true;
bool in_range=false;
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
void setup()
{
delay(2500);
#if (defined ESP32)
FPSerial.begin(9600, SERIAL_8N1, /*rx =*/D3, /*tx =*/D2);
#elif (defined(ARDUINO_attinyxy4))
FPSerial.begin(7680); // 9600/1.25, for 16-MHz parts
#else
FPSerial.begin(9600);
#endif
#ifdef DEBUG
SerialUSB.begin(115200);
SerialUSB.println();
SerialUSB.println(F("DFRobot DFPlayer Mini Demo"));
SerialUSB.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
#endif
if (!myDFPlayer.begin(FPSerial, /*isACK = */true, /*doReset = */true))
{ //Use serial to communicate with mp3.
#ifdef DEBUG
SerialUSB.println(F("Unable to begin:"));
SerialUSB.println(F("1.Please recheck the connection!"));
SerialUSB.println(F("2.Please insert the SD card!"));
#endif
while(true){
delay(0); // Code to compatible with ESP8266 watch dog.
}
}
#ifdef DEBUG
SerialUSB.println(F("DFPlayer Mini online."));
#endif
myDFPlayer.volume(30); //Set volume value. From 0 to 30
myDFPlayer.play(1); //Play the first mp3
}
void loop()
{
#if SENSOR_TYPE==SENSORTYPE_ULTRASONIC
int dist=rf.read(INC);
#ifdef DEBUG
SerialUSB.print("last=");
SerialUSB.print(last?"1":"0");
SerialUSB.print(" playing=");
SerialUSB.print(playing?"1":"0");
SerialUSB.print(" dist=");
SerialUSB.println(dist);
#endif
if (!in_range && dist<36)
{
#ifdef DEBUG
SerialUSB.println("** trigger **");
#endif
in_range=true;
if (!playing)
{
#ifdef DEBUG
SerialUSB.println("** start playing **");
#endif
myDFPlayer.next();
playing=true;
}
}
else
if (dist>=36)
in_range=false;
#endif
if (myDFPlayer.available())
if (myDFPlayer.readType()==DFPlayerPlayFinished)
playing=false;
delay(100);
}
#ifdef DEBUG
void printDetail(uint8_t type, int value){
switch (type) {
case TimeOut:
SerialUSB.println(F("Time Out!"));
break;
case WrongStack:
SerialUSB.println(F("Stack Wrong!"));
break;
case DFPlayerCardInserted:
SerialUSB.println(F("Card Inserted!"));
break;
case DFPlayerCardRemoved:
SerialUSB.println(F("Card Removed!"));
break;
case DFPlayerCardOnline:
SerialUSB.println(F("Card Online!"));
break;
case DFPlayerUSBInserted:
SerialUSB.println("USB Inserted!");
break;
case DFPlayerUSBRemoved:
SerialUSB.println("USB Removed!");
break;
case DFPlayerPlayFinished:
SerialUSB.print(F("Number:"));
SerialUSB.print(value);
SerialUSB.println(F(" Play Finished!"));
break;
case DFPlayerError:
SerialUSB.print(F("DFPlayerError:"));
switch (value) {
case Busy:
SerialUSB.println(F("Card not found"));
break;
case Sleeping:
SerialUSB.println(F("Sleeping"));
break;
case SerialWrongStack:
SerialUSB.println(F("Get Wrong Stack"));
break;
case CheckSumNotMatch:
SerialUSB.println(F("Check Sum Not Match"));
break;
case FileIndexOut:
SerialUSB.println(F("File Index Out of Bound"));
break;
case FileMismatch:
SerialUSB.println(F("Cannot Find File"));
break;
case Advertise:
SerialUSB.println(F("In Advertise"));
break;
default:
break;
}
break;
default:
break;
}
}
/***************************************************
DFPlayer - A Mini MP3 Player For Arduino
<https://www.dfrobot.com/product-1121.html>
***************************************************
This example shows the basic function of library for DFPlayer.
Created 2016-12-07
By [Angelo qiao](Angelo.qiao@dfrobot.com)
GNU Lesser General Public License.
See <http://www.gnu.org/licenses/> for details.
All above must be included in any redistribution
****************************************************/
/***********Notice and Trouble shooting***************
1.Connection and Diagram can be found here
<https://www.dfrobot.com/wiki/index.php/DFPlayer_Mini_SKU:DFR0299#Connection_Diagram>
2.This code is tested on Arduino Uno, Leonardo, Mega boards.
****************************************************/
#include "Arduino.h"
#include "DFRobotDFPlayerMini.h"
#if (defined(ARDUINO_AVR_UNO) || defined(ESP8266)) // Using a soft serial port
#include <SoftwareSerial.h>
SoftwareSerial softSerial(/*rx =*/4, /*tx =*/5);
#define FPSerial softSerial
#elif (defined(ARDUINO_attinyxy4))
#define FPSerial Serial
#else
#define FPSerial Serial1
#endif
// connection for passive infrared sensor
#define PIR 0
#define SENSORTYPE_PIR 1
// connections for ultrasonic rangefinder
#if (defined(ARDUINO_attinyxy4))
#define TRIGGER PIN_PA4
#define ECHO PIN_PA5
#else
#define TRIGGER 7
#define ECHO 8
#endif
#define SENSORTYPE_ULTRASONIC 2
#define SENSOR_TYPE SENSORTYPE_ULTRASONIC
// #define DEBUG
#if SENSOR_TYPE==SENSORTYPE_ULTRASONIC
#include <Ultrasonic.h>
Ultrasonic rf(TRIGGER, ECHO, 40000UL);
#endif
bool playing=true;
bool in_range=false;
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
void setup()
{
delay(2500);
#if (defined ESP32)
FPSerial.begin(9600, SERIAL_8N1, /*rx =*/D3, /*tx =*/D2);
#elif (defined(ARDUINO_attinyxy4))
FPSerial.begin(7680); // 9600/1.25, for 16-MHz parts
#else
FPSerial.begin(9600);
#endif
#ifdef DEBUG
SerialUSB.begin(115200);
SerialUSB.println();
SerialUSB.println(F("DFRobot DFPlayer Mini Demo"));
SerialUSB.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
#endif
if (!myDFPlayer.begin(FPSerial, /*isACK = */true, /*doReset = */true))
{ //Use serial to communicate with mp3.
#ifdef DEBUG
SerialUSB.println(F("Unable to begin:"));
SerialUSB.println(F("1.Please recheck the connection!"));
SerialUSB.println(F("2.Please insert the SD card!"));
#endif
while(true){
delay(0); // Code to compatible with ESP8266 watch dog.
}
}
#ifdef DEBUG
SerialUSB.println(F("DFPlayer Mini online."));
#endif
myDFPlayer.volume(30); //Set volume value. From 0 to 30
myDFPlayer.play(1); //Play the first mp3
}
void loop()
{
#if SENSOR_TYPE==SENSORTYPE_ULTRASONIC
int dist=rf.read(INC);
#ifdef DEBUG
SerialUSB.print("last=");
SerialUSB.print(last?"1":"0");
SerialUSB.print(" playing=");
SerialUSB.print(playing?"1":"0");
SerialUSB.print(" dist=");
SerialUSB.println(dist);
#endif
if (!in_range && dist<36)
{
#ifdef DEBUG
SerialUSB.println("** trigger **");
#endif
in_range=true;
if (!playing)
{
#ifdef DEBUG
SerialUSB.println("** start playing **");
#endif
myDFPlayer.next();
playing=true;
}
}
else
if (dist>=36)
in_range=false;
#endif
if (myDFPlayer.available())
if (myDFPlayer.readType()==DFPlayerPlayFinished)
playing=false;
delay(100);
}
#ifdef DEBUG
void printDetail(uint8_t type, int value){
switch (type) {
case TimeOut:
SerialUSB.println(F("Time Out!"));
break;
case WrongStack:
SerialUSB.println(F("Stack Wrong!"));
break;
case DFPlayerCardInserted:
SerialUSB.println(F("Card Inserted!"));
break;
case DFPlayerCardRemoved:
SerialUSB.println(F("Card Removed!"));
break;
case DFPlayerCardOnline:
SerialUSB.println(F("Card Online!"));
break;
case DFPlayerUSBInserted:
SerialUSB.println("USB Inserted!");
break;
case DFPlayerUSBRemoved:
SerialUSB.println("USB Removed!");
break;
case DFPlayerPlayFinished:
SerialUSB.print(F("Number:"));
SerialUSB.print(value);
SerialUSB.println(F(" Play Finished!"));
break;
case DFPlayerError:
SerialUSB.print(F("DFPlayerError:"));
switch (value) {
case Busy:
SerialUSB.println(F("Card not found"));
break;
case Sleeping:
SerialUSB.println(F("Sleeping"));
break;
case SerialWrongStack:
SerialUSB.println(F("Get Wrong Stack"));
break;
case CheckSumNotMatch:
SerialUSB.println(F("Check Sum Not Match"));
break;
case FileIndexOut:
SerialUSB.println(F("File Index Out of Bound"));
break;
case FileMismatch:
SerialUSB.println(F("Cannot Find File"));
break;
case Advertise:
SerialUSB.println(F("In Advertise"));
break;
default:
break;
}
break;
default:
break;
}
}
#endif