Advertisement
pleasedontcode

Object Detection Sound rev_04

Jul 4th, 2025
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Object Detection Sound
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-07-05 00:46:37
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The system should use the HC-SR04 ultrasonic */
  21.     /* sensor to detect items in the outdoor bin and play */
  22.     /* a designated MP3 sound via the DFPlayer Mini when */
  23.     /* an object is detected. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /* START CODE */
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <SoftwareSerial.h>
  30. #include <DFRobotDFPlayerMini.h>    //https://github.com/DFRobot/DFRobotDFPlayerMini
  31. #include <Ultrasonic.h> //https://github.com/ErickSimoes/Ultrasonic
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36. void checkObjectAndPlaySound(void);
  37.  
  38. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  39. const uint8_t ultrasonicsensor_HC-SR04_Echo_PIN_D3 = 3;
  40.  
  41. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  42. const uint8_t ultrasonicsensor_HC-SR04_Trigger_PIN_D2 = 2;
  43.  
  44. /***** DEFINITION OF Software Serial *****/
  45. const uint8_t mp3player_DFPlayerMini_Serial_PIN_SERIAL_TX_A0 = A0;
  46. const uint8_t mp3player_DFPlayerMini_Serial_PIN_SERIAL_RX_A1 = A1;
  47. SoftwareSerial mp3player_DFPlayerMini_Serial(mp3player_DFPlayerMini_Serial_PIN_SERIAL_RX_A1, mp3player_DFPlayerMini_Serial_PIN_SERIAL_TX_A0);
  48.  
  49. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  50. /***** used to store raw data *****/
  51. bool ultrasonicsensor_HC-SR04_Trigger_PIN_D2_rawData = 0;
  52.  
  53. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  54. /***** used to store data after characteristic curve transformation *****/
  55. float ultrasonicsensor_HC-SR04_Trigger_PIN_D2_phyData = 0.0;
  56.  
  57. /****** LIBRARY CLASS INSTANCES *****/
  58. Ultrasonic ultrasonicSensor(ultrasonicsensor_HC-SR04_Trigger_PIN_D2, ultrasonicsensor_HC-SR04_Echo_PIN_D3);
  59. DFRobotDFPlayerMini myDFPlayer;
  60.  
  61. void setup(void)
  62. {
  63.   // put your setup code here, to run once:
  64.   pinMode(ultrasonicsensor_HC-SR04_Echo_PIN_D3, INPUT);
  65.   pinMode(ultrasonicsensor_HC-SR04_Trigger_PIN_D2, OUTPUT);
  66.   mp3player_DFPlayerMini_Serial.begin(9600);
  67.  
  68.   // Initialize DFPlayer Mini
  69.   if (!myDFPlayer.begin(mp3player_DFPlayerMini_Serial, true, true))
  70.   {
  71.     // Initialization failed
  72.     while (true)
  73.     {
  74.       // Halt execution
  75.     }
  76.   }
  77. }
  78.  
  79. void loop(void)
  80. {
  81.   // Check for object detection and play sound if detected
  82.   checkObjectAndPlaySound();
  83.   delay(200); // Small delay to prevent rapid repeated triggers
  84. }
  85.  
  86. void checkObjectAndPlaySound(void)
  87. {
  88.   // Read distance in centimeters
  89.   int distance = ultrasonicSensor.read();
  90.   // Define threshold distance in centimeters for object detection
  91.   const int detectionThreshold = 20; // Adjust as needed
  92.  
  93.   if (distance > 0 && distance <= detectionThreshold)
  94.   {
  95.     // Object detected within threshold distance
  96.     // Play designated MP3 sound
  97.     myDFPlayer.play(1); // Play the first sound file
  98.     // Optional: Add delay or flag to prevent repeated triggers
  99.   }
  100. }
  101.  
  102. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement