Advertisement
pleasedontcode

"Distance Measurement" rev_01

Feb 9th, 2025
40
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-02-09 12:21:42
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* the system will utilize data from the ultrasonic */
  21.     /* sensor  and ouput in serial monitor */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Ultrasonic.h> //https://github.com/ErickSimoes/Ultrasonic
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void updateOutputs(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t ultrasonicsensor_HC_SR04_Echo_PIN_D3 = 3;
  36.  
  37. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  38. const uint8_t ultrasonicsensor_HC_SR04_Trigger_PIN_D2 = 2;
  39.  
  40. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  41. /***** used to store raw data *****/
  42. bool ultrasonicsensor_HC_SR04_Trigger_PIN_D2_rawData = 0;
  43.  
  44. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  45. /***** used to store data after characteristic curve transformation *****/
  46. float ultrasonicsensor_HC_SR04_Trigger_PIN_D2_phyData = 0.0;
  47.  
  48. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  49. // Create an instance of the Ultrasonic class
  50. Ultrasonic ultrasonic(ultrasonicsensor_HC_SR04_Trigger_PIN_D2, ultrasonicsensor_HC_SR04_Echo_PIN_D3);
  51.  
  52. void setup(void)
  53. {
  54.     // Initialize serial communication
  55.     Serial.begin(9600);
  56.  
  57.     // Set pin modes
  58.     pinMode(ultrasonicsensor_HC_SR04_Echo_PIN_D3, INPUT);
  59.     pinMode(ultrasonicsensor_HC_SR04_Trigger_PIN_D2, OUTPUT);
  60. }
  61.  
  62. void loop(void)
  63. {
  64.     // Refresh output data
  65.     updateOutputs();
  66.  
  67.     // Read distance from the ultrasonic sensor
  68.     ultrasonic.distanceRead(); // Get the distance in cm
  69.  
  70.     // Print the distance to the serial monitor
  71.     Serial.print("Distance: ");
  72.     Serial.print(ultrasonic.read()); // Read and print the distance
  73.     Serial.println(" cm");
  74.  
  75.     // Delay for a short period before the next reading
  76.     delay(500);
  77. }
  78.  
  79. void updateOutputs()
  80. {
  81.     // Trigger the ultrasonic sensor to take a reading
  82.     digitalWrite(ultrasonicsensor_HC_SR04_Trigger_PIN_D2, HIGH);
  83.     delayMicroseconds(10);
  84.     digitalWrite(ultrasonicsensor_HC_SR04_Trigger_PIN_D2, LOW);
  85. }
  86.  
  87. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement