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: Object Detection Sound
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2025-07-05 00:46:37
- ********* 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 checkObjectAndPlaySound(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;
- /****** LIBRARY CLASS INSTANCES *****/
- Ultrasonic ultrasonicSensor(ultrasonicsensor_HC-SR04_Trigger_PIN_D2, ultrasonicsensor_HC-SR04_Echo_PIN_D3);
- DFRobotDFPlayerMini myDFPlayer;
- 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 DFPlayer Mini
- if (!myDFPlayer.begin(mp3player_DFPlayerMini_Serial, true, true))
- {
- // Initialization failed
- while (true)
- {
- // Halt execution
- }
- }
- }
- void loop(void)
- {
- // Check for object detection and play sound if detected
- checkObjectAndPlaySound();
- delay(200); // Small delay to prevent rapid repeated triggers
- }
- void checkObjectAndPlaySound(void)
- {
- // Read distance in centimeters
- int distance = ultrasonicSensor.read();
- // Define threshold distance in centimeters for object detection
- const int detectionThreshold = 20; // Adjust as needed
- if (distance > 0 && distance <= detectionThreshold)
- {
- // Object detected within threshold distance
- // Play designated MP3 sound
- myDFPlayer.play(1); // Play the first sound file
- // Optional: Add delay or flag to prevent repeated triggers
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement