Advertisement
pleasedontcode

Object Detection rev_06

Jul 4th, 2025
332
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 01:21:30
  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 updateOutputs(void);
  37. void checkObjectDetection(void);
  38.  
  39. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  40. const uint8_t ultrasonicsensor_HC-SR04_Echo_PIN_D3 = 3;
  41.  
  42. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  43. const uint8_t ultrasonicsensor_HC-SR04_Trigger_PIN_D2 = 2;
  44.  
  45. /***** DEFINITION OF Software Serial *****/
  46. const uint8_t mp3player_DFPlayerMini_Serial_PIN_SERIAL_TX_A0 = A0;
  47. const uint8_t mp3player_DFPlayerMini_Serial_PIN_SERIAL_RX_A1 = A1;
  48. SoftwareSerial mp3player_DFPlayerMini_Serial(mp3player_DFPlayerMini_Serial_PIN_SERIAL_RX_A1, mp3player_DFPlayerMini_Serial_PIN_SERIAL_TX_A0);
  49.  
  50. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  51. /***** used to store raw data *****/
  52. bool ultrasonicsensor_HC_SR04_Trigger_PIN_D2_rawData = LOW;
  53.  
  54. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  55. /***** used to store data after characteristic curve transformation *****/
  56. float ultrasonicsensor_HC_SR04_Trigger_PIN_D2_phyData = 0.0;
  57.  
  58. /****** LIBRARY CLASS INSTANCES *****/
  59. Ultrasonic ultrasonicSensor(ultrasonicsensor_HC-SR04_Trigger_PIN_D2, ultrasonicsensor_HC-SR04_Echo_PIN_D3);
  60. DFRobotDFPlayerMini myDFPlayer;
  61.  
  62. void setup(void)
  63. {
  64.   // put your setup code here, to run once:
  65.   pinMode(ultrasonicsensor_HC-SR04_Echo_PIN_D3, INPUT);
  66.   pinMode(ultrasonicsensor_HC-SR04_Trigger_PIN_D2, OUTPUT);
  67.   mp3player_DFPlayerMini_Serial.begin(9600);
  68.   // Initialize DFPlayer
  69.   if (!myDFPlayer.begin(mp3player_DFPlayerMini_Serial, true, true)) {
  70.     // Handle initialization failure if needed
  71.   }
  72. }
  73.  
  74. void loop(void)
  75. {
  76.   // put your main code here, to run repeatedly:
  77.   checkObjectDetection();
  78.   updateOutputs();
  79. }
  80.  
  81. void updateOutputs()
  82. {
  83.   digitalWrite(ultrasonicsensor_HC-SR04_Trigger_PIN_D2, ultrasonicsensor_HC_SR04_Trigger_PIN_D2_rawData);
  84. }
  85.  
  86. void checkObjectDetection()
  87. {
  88.   // Read distance from ultrasonic sensor
  89.   int distance_cm = ultrasonicSensor.read(); // in centimeters
  90.   // Define threshold distance in cm for object detection
  91.   const int detectionThreshold = 20; // e.g., 20 cm
  92.   if (distance_cm > 0 && distance_cm <= detectionThreshold)
  93.   {
  94.     // Object detected within threshold
  95.     // Play sound via DFPlayer
  96.     myDFPlayer.play(1); // Play the designated MP3 sound (e.g., sound index 1)
  97.   }
  98. }
  99.  
  100. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement