Advertisement
pleasedontcode

OutdoorBinSoundActivation

Feb 24th, 2025
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 3.41 KB | Source Code | 0 0
  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: OutdoorBinSoundActivation
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-02-24 10:01:36
  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();
  37. void detectObjectAndPlaySound(); // New function to detect objects and play sound
  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 = 0;
  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. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  59. // Instantiate the DFPlayer Mini and Ultrasonic sensor objects
  60. DFRobotDFPlayerMini mp3Player;
  61. Ultrasonic ultrasonic(ultrasonicsensor_HC-SR04_Trigger_PIN_D2, ultrasonicsensor_HC-SR04_Echo_PIN_D3);
  62.  
  63. void setup(void)
  64. {
  65.     // put your setup code here, to run once:
  66.     pinMode(ultrasonicsensor_HC-SR04_Echo_PIN_D3, INPUT);
  67.     pinMode(ultrasonicsensor_HC-SR04_Trigger_PIN_D2, OUTPUT);
  68.     mp3player_DFPlayerMini_Serial.begin(9600);
  69.     mp3Player.begin(mp3player_DFPlayerMini_Serial); // Initialize DFPlayer Mini
  70. }
  71.  
  72. void loop(void)
  73. {
  74.     // put your main code here, to run repeatedly:
  75.     detectObjectAndPlaySound(); // Check for objects and play sound
  76. }
  77.  
  78. void detectObjectAndPlaySound()
  79. {
  80.     // Measure distance using the ultrasonic sensor
  81.     long distance = ultrasonic.read(); // Get distance in cm
  82.  
  83.     if (distance < 10) // If an object is detected within 10 cm
  84.     {
  85.         mp3Player.play(1); // Play the first MP3 file (adjust the number as needed)
  86.         delay(1000); // Delay to prevent multiple triggers
  87.     }
  88. }
  89.  
  90. void updateOutputs()
  91. {
  92.     digitalWrite(ultrasonicsensor_HC-SR04_Trigger_PIN_D2, ultrasonicsensor_HC-SR04_Trigger_PIN_D2_rawData);
  93. }
  94.  
  95. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement