Advertisement
pleasedontcode

"Distance Measurement" rev_03

May 21st, 2025
357
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: Arduino Uno
  14.     - Source Code created on: 2025-05-21 17:50:49
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* generate code for reading values of ultrasonic */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Ultrasonic-distance-sensor-easyC-SOLDERED.h>  //https://github.com/SolderedElectronics/Soldered-Ultrasonic-Sensor-easyC-Arduino-Library
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31. void updateOutputs();
  32. void readUltrasonicSensor(); // Function to read ultrasonic sensor values
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t piyush_HC_SR04_Echo_PIN_D5 = 5;
  36.  
  37. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  38. const uint8_t piyush_HC_SR04_Trigger_PIN_D4 = 4;
  39.  
  40. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  41. /***** used to store raw data *****/
  42. bool piyush_HC_SR04_Trigger_PIN_D4_rawData = 0;
  43.  
  44. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  45. /***** used to store data after characteristic curve transformation *****/
  46. float piyush_HC_SR04_Trigger_PIN_D4_phyData = 0.0;
  47.  
  48. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  49. // Create an instance of the Ultrasonic_Sensor class
  50. Ultrasonic_Sensor ultrasonicSensor(piyush_HC_SR04_Echo_PIN_D5, piyush_HC_SR04_Trigger_PIN_D4);
  51.  
  52. void setup(void)
  53. {
  54.     // put your setup code here, to run once:
  55.     pinMode(piyush_HC_SR04_Echo_PIN_D5, INPUT);
  56.     pinMode(piyush_HC_SR04_Trigger_PIN_D4, OUTPUT);
  57. }
  58.  
  59. void loop(void)
  60. {
  61.     // put your main code here, to run repeatedly:
  62.     updateOutputs(); // Refresh output data
  63.     readUltrasonicSensor(); // Read values from the ultrasonic sensor
  64. }
  65.  
  66. void updateOutputs()
  67. {
  68.     digitalWrite(piyush_HC_SR04_Trigger_PIN_D4, piyush_HC_SR04_Trigger_PIN_D4_rawData);
  69. }
  70.  
  71. void readUltrasonicSensor()
  72. {
  73.     // Trigger the ultrasonic sensor and read the distance
  74.     piyush_HC_SR04_Trigger_PIN_D4_rawData = 1; // Set trigger high
  75.     delayMicroseconds(10); // Wait for 10 microseconds
  76.     piyush_HC_SR04_Trigger_PIN_D4_rawData = 0; // Set trigger low
  77.  
  78.     // Get the distance from the ultrasonic sensor
  79.     uint16_t distance = ultrasonicSensor.getDistance();
  80.     piyush_HC_SR04_Trigger_PIN_D4_phyData = distance; // Store the distance in physical data variable
  81. }
  82.  
  83. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement