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: **Sensor Distances**
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-05-19 13:17:57
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Write me a code for ESP32. I have 4 ultrasonic */
- /* sensors acting as a Receiver (Rx1, Rx2, Rx3, and */
- /* Rx4) and another 1 ultrasonic sensor acting as a */
- /* Transmitter (This Transmitter sensor do not care */
- /* about its code). write me a code for the 4 */
- /* ultrasonic se */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <NewPing.h> // Include the NewPing library for ultrasonic sensors
- #include <Ultrasonic.h> // Include the Ultrasonic library for additional functionality
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- // Define the pins for the ultrasonic sensors
- #define TRIGGER_PIN_1 5 // Trigger pin for Rx1
- #define ECHO_PIN_1 18 // Echo pin for Rx1
- #define TRIGGER_PIN_2 19 // Trigger pin for Rx2
- #define ECHO_PIN_2 21 // Echo pin for Rx2
- #define TRIGGER_PIN_3 22 // Trigger pin for Rx3
- #define ECHO_PIN_3 23 // Echo pin for Rx3
- #define TRIGGER_PIN_4 25 // Trigger pin for Rx4
- #define ECHO_PIN_4 26 // Echo pin for Rx4
- // Create NewPing objects for each receiver
- NewPing sonar1(TRIGGER_PIN_1, ECHO_PIN_1);
- NewPing sonar2(TRIGGER_PIN_2, ECHO_PIN_2);
- NewPing sonar3(TRIGGER_PIN_3, ECHO_PIN_3);
- NewPing sonar4(TRIGGER_PIN_4, ECHO_PIN_4);
- void setup(void)
- {
- Serial.begin(115200); // Start the Serial communication
- // Additional setup code can be added here
- }
- void loop(void)
- {
- // Read distances from each ultrasonic sensor
- unsigned int distance1 = sonar1.ping_cm(); // Distance from Rx1
- unsigned int distance2 = sonar2.ping_cm(); // Distance from Rx2
- unsigned int distance3 = sonar3.ping_cm(); // Distance from Rx3
- unsigned int distance4 = sonar4.ping_cm(); // Distance from Rx4
- // Print the distances to the Serial Monitor
- Serial.print("Distance 1: ");
- Serial.print(distance1);
- Serial.println(" cm");
- Serial.print("Distance 2: ");
- Serial.print(distance2);
- Serial.println(" cm");
- Serial.print("Distance 3: ");
- Serial.print(distance3);
- Serial.println(" cm");
- Serial.print("Distance 4: ");
- Serial.print(distance4);
- Serial.println(" cm");
- delay(1000); // Wait for a second before the next reading
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement