Advertisement
pleasedontcode

Distance Measurement rev_02

Nov 19th, 2024
32
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: Distance Measurement
  13.     - Source Code compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-11-19 11:49:18
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* My HC-SR04 is with UART, so use tx, rx. I want to */
  21.     /* use this as a transmitter unit for my water level */
  22.     /* monitoring system, which will send the data to a */
  23.     /* receiver unit with esp32 and other components. So */
  24.     /* make sketch for transmitter now. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27.  
  28. /* START CODE */
  29.  
  30. /****** DEFINITION OF LIBRARIES *****/
  31. #include <Ultrasonic.h>  // https://github.com/ErickSimoes/Ultrasonic
  32. #include <HCSR04.h>      // https://github.com/d03n3rfr1tz3/HC-SR04
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37. void updateOutputs(void);
  38.  
  39. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  40. const uint8_t UltraSoundSensor_HC_SR04_Echo_PIN_D13 = 13;
  41.  
  42. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  43. const uint8_t UltraSoundSensor_HC_SR04_Trigger_PIN_D4 = 4;
  44.  
  45. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  46. /***** used to store raw data *****/
  47. bool UltraSoundSensor_HC_SR04_Trigger_PIN_D4_rawData = 0;
  48.  
  49. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  50. /***** used to store data after characteristic curve transformation *****/
  51. float UltraSoundSensor_HC_SR04_Trigger_PIN_D4_phyData = 0.0;
  52.  
  53. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  54. // Instantiate the Ultrasonic sensor
  55. Ultrasonic ultrasonic(UltraSoundSensor_HC_SR04_Trigger_PIN_D4, UltraSoundSensor_HC_SR04_Echo_PIN_D13);
  56.  
  57. // Instantiate the HC-SR04 sensor
  58. UltraSonicDistanceSensor distanceSensor(UltraSoundSensor_HC_SR04_Trigger_PIN_D4, UltraSoundSensor_HC_SR04_Echo_PIN_D13);
  59.  
  60. /****** SETUP FUNCTION *****/
  61. void setup(void)
  62. {
  63.     // Initialize serial communication for debugging
  64.     Serial.begin(9600);
  65.  
  66.     // Set pin modes
  67.     pinMode(UltraSoundSensor_HC_SR04_Echo_PIN_D13, INPUT);
  68.     pinMode(UltraSoundSensor_HC_SR04_Trigger_PIN_D4, OUTPUT);
  69. }
  70.  
  71. /****** LOOP FUNCTION *****/
  72. void loop(void)
  73. {
  74.     // Measure distance and update raw data
  75.     UltraSoundSensor_HC_SR04_Trigger_PIN_D4_rawData = distanceSensor.measureDistanceCm() < 100; // Example condition
  76.  
  77.     // Update outputs
  78.     updateOutputs(); // Refresh output data
  79.  
  80.     // Print distance for debugging
  81.     Serial.print("Distance: ");
  82.     Serial.print(distanceSensor.measureDistanceCm());
  83.     Serial.println(" cm");
  84.  
  85.     delay(1000); // Delay for readability
  86. }
  87.  
  88. /****** UPDATE OUTPUTS FUNCTION *****/
  89. void updateOutputs()
  90. {
  91.     digitalWrite(UltraSoundSensor_HC_SR04_Trigger_PIN_D4, UltraSoundSensor_HC_SR04_Trigger_PIN_D4_rawData);
  92. }
  93.  
  94. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement