Advertisement
pleasedontcode

**Sensor Distances** rev_01

May 19th, 2025
530
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: **Sensor Distances**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-05-19 13:17:57
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Write me a code for ESP32. I have 4 ultrasonic */
  21.     /* sensors acting as a Receiver (Rx1, Rx2, Rx3, and */
  22.     /* Rx4) and another 1 ultrasonic sensor acting as a */
  23.     /* Transmitter (This Transmitter sensor do not care */
  24.     /* about its code). write me a code for the 4 */
  25.     /* ultrasonic se */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /* START CODE */
  29.  
  30. /****** DEFINITION OF LIBRARIES *****/
  31. #include <NewPing.h>      // Include the NewPing library for ultrasonic sensors
  32. #include <Ultrasonic.h>   // Include the Ultrasonic library for additional functionality
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37.  
  38. // Define the pins for the ultrasonic sensors
  39. #define TRIGGER_PIN_1  5  // Trigger pin for Rx1
  40. #define ECHO_PIN_1     18 // Echo pin for Rx1
  41. #define TRIGGER_PIN_2  19 // Trigger pin for Rx2
  42. #define ECHO_PIN_2     21 // Echo pin for Rx2
  43. #define TRIGGER_PIN_3  22 // Trigger pin for Rx3
  44. #define ECHO_PIN_3     23 // Echo pin for Rx3
  45. #define TRIGGER_PIN_4  25 // Trigger pin for Rx4
  46. #define ECHO_PIN_4     26 // Echo pin for Rx4
  47.  
  48. // Create NewPing objects for each receiver
  49. NewPing sonar1(TRIGGER_PIN_1, ECHO_PIN_1);
  50. NewPing sonar2(TRIGGER_PIN_2, ECHO_PIN_2);
  51. NewPing sonar3(TRIGGER_PIN_3, ECHO_PIN_3);
  52. NewPing sonar4(TRIGGER_PIN_4, ECHO_PIN_4);
  53.  
  54. void setup(void)
  55. {
  56.     Serial.begin(115200); // Start the Serial communication
  57.     // Additional setup code can be added here
  58. }
  59.  
  60. void loop(void)
  61. {
  62.     // Read distances from each ultrasonic sensor
  63.     unsigned int distance1 = sonar1.ping_cm(); // Distance from Rx1
  64.     unsigned int distance2 = sonar2.ping_cm(); // Distance from Rx2
  65.     unsigned int distance3 = sonar3.ping_cm(); // Distance from Rx3
  66.     unsigned int distance4 = sonar4.ping_cm(); // Distance from Rx4
  67.  
  68.     // Print the distances to the Serial Monitor
  69.     Serial.print("Distance 1: ");
  70.     Serial.print(distance1);
  71.     Serial.println(" cm");
  72.    
  73.     Serial.print("Distance 2: ");
  74.     Serial.print(distance2);
  75.     Serial.println(" cm");
  76.    
  77.     Serial.print("Distance 3: ");
  78.     Serial.print(distance3);
  79.     Serial.println(" cm");
  80.    
  81.     Serial.print("Distance 4: ");
  82.     Serial.print(distance4);
  83.     Serial.println(" cm");
  84.  
  85.     delay(1000); // Wait for a second before the next reading
  86. }
  87.  
  88. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement