Advertisement
pleasedontcode

OutdoorBinSoundActivation rev_02

Jul 4th, 2025
281
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:
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-07-04 22:12:16
  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 checkObjectDetection(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. /***** Instantiate Ultrasonic sensor object *****/
  58. Ultrasonic ultrasonicSensor(ultrasonicsensor_HC_SR04_Echo_PIN_D3);
  59.  
  60. /***** Detection threshold in centimeters *****/
  61. const float detectionThreshold = 20.0;
  62.  
  63. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  64. DFRobotDFPlayerMini mp3Player;
  65.  
  66. void setup(void)
  67. {
  68.   // put your setup code here, to run once:
  69.   pinMode(ultrasonicsensor_HC_SR04_Echo_PIN_D3, INPUT);
  70.   pinMode(ultrasonicsensor_HC_SR04_Trigger_PIN_D2, OUTPUT);
  71.   mp3player_DFPlayerMini_Serial.begin(9600);
  72.  
  73.   // Initialize MP3 Player
  74.   if (!mp3Player.begin(mp3player_DFPlayerMini_Serial)) {
  75.     // Initialization failed
  76.     while(true);
  77.   }
  78.   mp3Player.volume(20); // Set volume level (0-30)
  79. }
  80.  
  81. void loop(void)
  82. {
  83.   // put your main code here, to run repeatedly:
  84.   checkObjectDetection();
  85. }
  86.  
  87. void checkObjectDetection()
  88. {
  89.   // Read distance from ultrasonic sensor
  90.   float distance = ultrasonicSensor.read();
  91.  
  92.   // Check if object is within detection threshold
  93.   if (distance > 0 && distance <= detectionThreshold)
  94.   {
  95.     // Play sound if not already playing
  96.     if (!mp3Player.isPlaying())
  97.     {
  98.       mp3Player.play(1); // Play the first track
  99.     }
  100.   }
  101.   else
  102.   {
  103.     // Optional: stop playback if no object
  104.     // mp3Player.stop();
  105.   }
  106.   delay(200); // Small delay to prevent rapid triggering
  107. }
  108.  
  109. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement