#include <Arduino.h>
//#include <WiFi.h>
//#include <SPIFFS.h>
//#include <ESP32_MP3_Decoder.h>
//#include <SD.h>
//#include <HC_SR04.h>
#include <HardwareSerial.h>
//#include "DFRobotDFPlayerMini.h"
//#include <DFRobotDFPlayerMini.h>
//DFRobotDFPlayerMini playerMP3;
//HardwareSerial playerMP3Serial(2); // for UART1, leaves standard UART0 alone for debug output via USB/serial bridge
#define ECHO_PIN 22
#define TRIG_PIN 21
// WIFI
//const char* ssid = "ssid";
//const char* password = "password";
// Variable for HC-SR04
HC_SR04 hcsr04;
// Variable for storing the current distance
float distance;
// Variable for storing the current song
String currentSong;
// Array for storing the list of songs
String songList[] = {"song1.mp3", "song2.mp3", "song3.mp3"};
// Variable for song index
int songIndex = 0;
// Variable for storing the total number of songs
int numSongs = sizeof(songList) / sizeof(songList[0]);
// Variable for the MP3 decoder
ESP32_MP3_Decoder decoder;
// Setup function
void setup() {
Serial.begin(9600);
// Connect to WIFI
//Serial.println("Connecting to WIFI...");
//WiFi.begin(ssid, password);
//while (WiFi.status() != WL_CONNECTED) {
// delay(500);
// Serial.print(".");
}
// Serial.println();
//Serial.println("WiFi connected");
//Serial.println("IP address: ");
//Serial.println(WiFi.localIP());
// Initialize HC-SR04
hcsr04.init(ECHO_PIN, TRIG_PIN);
// Initialize SD card
if (!SD.begin()) {
Serial.println("Card Mount Failed");
return;
}
// Initialize SPIFFS
if (!SPIFFS.begin(true)) {
Serial.println("SPIFFS Mount Failed");
return;
}
// Initialize decoder
decoder.begin();
}
// Main loop
void loop() {
// Get the current distance
distance = hcsr04.getDistance();
// If the current distance is less than 20 cm
if (distance < 20) {
// Choose a random song
songIndex = random(numSongs);
currentSong = songList[songIndex];
// Play the song
decoder.playMP3(currentSong.c_str());
}
delay(100);
}
}