Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project:
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2025-07-04 22:12:16
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* The system should use the HC-SR04 ultrasonic */
- /* sensor to detect items in the outdoor bin and play */
- /* a designated MP3 sound via the DFPlayer Mini when */
- /* an object is detected. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <SoftwareSerial.h>
- #include <DFRobotDFPlayerMini.h> // https://github.com/DFRobot/DFRobotDFPlayerMini
- #include <Ultrasonic.h> // https://github.com/ErickSimoes/Ultrasonic
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void checkObjectDetection(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t ultrasonicsensor_HC_SR04_Echo_PIN_D3 = 3;
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t ultrasonicsensor_HC_SR04_Trigger_PIN_D2 = 2;
- /***** DEFINITION OF Software Serial *****/
- const uint8_t mp3player_DFPlayerMini_Serial_PIN_SERIAL_TX_A0 = A0;
- const uint8_t mp3player_DFPlayerMini_Serial_PIN_SERIAL_RX_A1 = A1;
- SoftwareSerial mp3player_DFPlayerMini_Serial(mp3player_DFPlayerMini_Serial_PIN_SERIAL_RX_A1, mp3player_DFPlayerMini_Serial_PIN_SERIAL_TX_A0);
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- /***** used to store raw data *****/
- bool ultrasonicsensor_HC_SR04_Trigger_PIN_D2_rawData = 0;
- /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
- /***** used to store data after characteristic curve transformation *****/
- float ultrasonicsensor_HC_SR04_Trigger_PIN_D2_phyData = 0.0;
- /***** Instantiate Ultrasonic sensor object *****/
- Ultrasonic ultrasonicSensor(ultrasonicsensor_HC_SR04_Echo_PIN_D3);
- /***** Detection threshold in centimeters *****/
- const float detectionThreshold = 20.0;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- DFRobotDFPlayerMini mp3Player;
- void setup(void)
- {
- // put your setup code here, to run once:
- pinMode(ultrasonicsensor_HC_SR04_Echo_PIN_D3, INPUT);
- pinMode(ultrasonicsensor_HC_SR04_Trigger_PIN_D2, OUTPUT);
- mp3player_DFPlayerMini_Serial.begin(9600);
- // Initialize MP3 Player
- if (!mp3Player.begin(mp3player_DFPlayerMini_Serial)) {
- // Initialization failed
- while(true);
- }
- mp3Player.volume(20); // Set volume level (0-30)
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- checkObjectDetection();
- }
- void checkObjectDetection()
- {
- // Read distance from ultrasonic sensor
- float distance = ultrasonicSensor.read();
- // Check if object is within detection threshold
- if (distance > 0 && distance <= detectionThreshold)
- {
- // Play sound if not already playing
- if (!mp3Player.isPlaying())
- {
- mp3Player.play(1); // Play the first track
- }
- }
- else
- {
- // Optional: stop playback if no object
- // mp3Player.stop();
- }
- delay(200); // Small delay to prevent rapid triggering
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement