Advertisement
pleasedontcode

"Distance Measurement" rev_01

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