#include <Arduino.h>
#include <Wire.h>
#include <DFRobotDFPlayerMini.h> // Include the library for DFPlayerMini voice module
#define LDR_PIN A0 // Pin connected to the LDR sensor
#define THRESHOLD 500 // Adjust this threshold according to ambient light conditions
#define AMP_OUT_PIN 23 // Pin connected to the amplifier module
DFRobotDFPlayerMini dfplayer; // Create an instance of DFPlayerMini
void setup() {
Serial.begin(115200);
// Initialize DFPlayerMini
if (!dfplayer.begin(Serial2)) { // Serial2 is connected to TX2 and RX15
Serial.println("Unable to begin:");
Serial.println("1.Please recheck the connection!");
Serial.println("2.Please insert the SD card!");
while(true);
}
Serial.println("DFPlayer Mini online.");
// Set the baud rate for the voice module
dfplayer.setTimeOut(500); //Set serial communication time out 500ms
dfplayer.volume(20); //Set volume value (0~30).
pinMode(LDR_PIN, INPUT);
pinMode(AMP_OUT_PIN, OUTPUT);
}
void loop() {
int lightLevel = analogRead(LDR_PIN);
if (lightLevel < THRESHOLD) {
// Dark place detected
digitalWrite(AMP_OUT_PIN, HIGH); // Turn on the amplifier
delay(500); // Delay before playing the message to ensure the amplifier is ready
// Play the voice message
dfplayer.playMp3Folder(1); // Assume voice message "You have entered a dark place" is stored in folder 1
delay(5000); // Adjust delay to match the length of the message
digitalWrite(AMP_OUT_PIN, LOW); // Turn off the amplifier
} else {
// Bright place detected
digitalWrite(AMP_OUT_PIN, LOW); // Turn off the amplifier
}
delay(500); // Adjust delay according to your requirement
}