Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: OutdoorBinSoundActivation
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2025-02-24 10:01:36
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* The system should use the HC-SR04 ultrasonic */
- /* sensor to detect items in the outdoor bin and play */
- /* a designated MP3 sound via the DFPlayer Mini when */
- /* an object is detected. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <SoftwareSerial.h>
- #include <DFRobotDFPlayerMini.h> //https://github.com/DFRobot/DFRobotDFPlayerMini
- #include <Ultrasonic.h> //https://github.com/ErickSimoes/Ultrasonic
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void updateOutputs();
- void detectObjectAndPlaySound(); // New function to detect objects and play sound
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t ultrasonicsensor_HC-SR04_Echo_PIN_D3 = 3;
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t ultrasonicsensor_HC-SR04_Trigger_PIN_D2 = 2;
- /***** DEFINITION OF Software Serial *****/
- const uint8_t mp3player_DFPlayerMini_Serial_PIN_SERIAL_TX_A0 = A0;
- const uint8_t mp3player_DFPlayerMini_Serial_PIN_SERIAL_RX_A1 = A1;
- SoftwareSerial mp3player_DFPlayerMini_Serial(mp3player_DFPlayerMini_Serial_PIN_SERIAL_RX_A1, mp3player_DFPlayerMini_Serial_PIN_SERIAL_TX_A0);
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- /***** used to store raw data *****/
- bool ultrasonicsensor_HC-SR04_Trigger_PIN_D2_rawData = 0;
- /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
- /***** used to store data after characteristic curve transformation *****/
- float ultrasonicsensor_HC-SR04_Trigger_PIN_D2_phyData = 0.0;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- // Instantiate the DFPlayer Mini and Ultrasonic sensor objects
- DFRobotDFPlayerMini mp3Player;
- Ultrasonic ultrasonic(ultrasonicsensor_HC-SR04_Trigger_PIN_D2, ultrasonicsensor_HC-SR04_Echo_PIN_D3);
- void setup(void)
- {
- // put your setup code here, to run once:
- pinMode(ultrasonicsensor_HC-SR04_Echo_PIN_D3, INPUT);
- pinMode(ultrasonicsensor_HC-SR04_Trigger_PIN_D2, OUTPUT);
- mp3player_DFPlayerMini_Serial.begin(9600);
- mp3Player.begin(mp3player_DFPlayerMini_Serial); // Initialize DFPlayer Mini
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- detectObjectAndPlaySound(); // Check for objects and play sound
- }
- void detectObjectAndPlaySound()
- {
- // Measure distance using the ultrasonic sensor
- long distance = ultrasonic.read(); // Get distance in cm
- if (distance < 10) // If an object is detected within 10 cm
- {
- mp3Player.play(1); // Play the first MP3 file (adjust the number as needed)
- delay(1000); // Delay to prevent multiple triggers
- }
- }
- void updateOutputs()
- {
- digitalWrite(ultrasonicsensor_HC-SR04_Trigger_PIN_D2, ultrasonicsensor_HC-SR04_Trigger_PIN_D2_rawData);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement