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
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2025-07-05 01:21:30
- ********* 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 updateOutputs(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 = LOW;
- /***** 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
- if (!myDFPlayer.begin(mp3player_DFPlayerMini_Serial, true, true)) {
- // Handle initialization failure if needed
- }
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- checkObjectDetection();
- updateOutputs();
- }
- void updateOutputs()
- {
- digitalWrite(ultrasonicsensor_HC-SR04_Trigger_PIN_D2, ultrasonicsensor_HC_SR04_Trigger_PIN_D2_rawData);
- }
- void checkObjectDetection()
- {
- // Read distance from ultrasonic sensor
- int distance_cm = ultrasonicSensor.read(); // in centimeters
- // Define threshold distance in cm for object detection
- const int detectionThreshold = 20; // e.g., 20 cm
- if (distance_cm > 0 && distance_cm <= detectionThreshold)
- {
- // Object detected within threshold
- // Play sound via DFPlayer
- myDFPlayer.play(1); // Play the designated MP3 sound (e.g., sound index 1)
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement