Advertisement
pleasedontcode

Object Detection rev_03

Jul 4th, 2025
226
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
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-07-05 00:45:17
  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 ultrasonic(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.   if (!myDFPlayer.begin(mp3player_DFPlayerMini_Serial, true, true))
  68.   {
  69.     // Initialization failed
  70.     while (true)
  71.     {
  72.       // Halt execution
  73.     }
  74.   }
  75.   // Set volume to a moderate level
  76.   myDFPlayer.volume(10);
  77. }
  78.  
  79. void loop(void)
  80. {
  81.   // Check for object detection and play sound if detected
  82.   checkObjectAndPlaySound();
  83.  
  84.   // Update outputs (trigger pin control)
  85.   updateOutputs();
  86.  
  87.   // Small delay to prevent excessive polling
  88.   delay(100);
  89. }
  90.  
  91. void checkObjectAndPlaySound(void)
  92. {
  93.   // Read distance in centimeters
  94.   int distance = ultrasonic.read();
  95.  
  96.   // Define threshold distance in centimeters for object detection
  97.   const int detectionThreshold = 50; // e.g., 50cm
  98.  
  99.   if (distance > 0 && distance < detectionThreshold)
  100.   {
  101.     // Object detected within threshold distance
  102.     // Play designated MP3 sound if not already playing
  103.     static bool soundPlayed = false;
  104.     if (!soundPlayed)
  105.     {
  106.       myDFPlayer.play(1); // Play the first MP3 file
  107.       soundPlayed = true;
  108.     }
  109.   }
  110.   else
  111.   {
  112.     // No object detected or out of range
  113.     // Reset the flag to allow future detection
  114.     static bool soundPlayed = false;
  115.     soundPlayed = false;
  116.   }
  117. }
  118.  
  119. void updateOutputs()
  120. {
  121.   // Set trigger pin high for a brief moment to initiate measurement
  122.   digitalWrite(ultrasonicsensor_HC_SR04_Trigger_PIN_D2, ultrasonicsensor_HC_SR04_Trigger_PIN_D2_rawData);
  123. }
  124.  
  125. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement