Advertisement
pleasedontcode

Ultrasonic Distance rev_01

Feb 16th, 2024
68
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: Ultrasonic Distance
  13.     - Source Code compiled for: Arduino Pro Mini 5V
  14.     - Source Code created on: 2024-02-16 14:48:51
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The drone should have a trigger pin (D2) and an */
  21.     /* echo pin (D3) connected to the HC-SR04 sensor for */
  22.     /* accurate distance measurement. */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Ultrasonic.h>
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31. void updateOutputs(void);
  32. void printDistance(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t sensor_HC_SR04_Echo_PIN_D3 = 3;
  36.  
  37. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  38. const uint8_t sensor_HC_SR04_Trigger_PIN_D2 = 2;
  39.  
  40. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  41. bool sensor_HC_SR04_Trigger_PIN_D2_rawData = false;
  42.  
  43. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  44. float sensor_HC_SR04_Trigger_PIN_D2_phyData = 0.0;
  45.  
  46. /****** DEFINITION OF CLASS INSTANCES*****/
  47. Ultrasonic ultrasonic(sensor_HC_SR04_Trigger_PIN_D2, sensor_HC_SR04_Echo_PIN_D3);
  48.  
  49. void setup(void)
  50. {
  51.   // put your setup code here, to run once:
  52.   pinMode(sensor_HC_SR04_Echo_PIN_D3, INPUT);
  53.   pinMode(sensor_HC_SR04_Trigger_PIN_D2, OUTPUT);
  54.  
  55.   Serial.begin(9600); // Initialize serial communication
  56.  
  57.   // Print the header of the distance data
  58.   Serial.println("Distance Measurement");
  59. }
  60.  
  61. void loop(void)
  62. {
  63.   // put your main code here, to run repeatedly:
  64.   updateOutputs(); // Refresh output data
  65.   printDistance(); // Print the distance to the serial monitor
  66.  
  67.   delay(1000); // Delay for 1 second
  68. }
  69.  
  70. void updateOutputs()
  71. {
  72.   digitalWrite(sensor_HC_SR04_Trigger_PIN_D2, sensor_HC_SR04_Trigger_PIN_D2_rawData);
  73. }
  74.  
  75. void printDistance()
  76. {
  77.   unsigned int distance = ultrasonic.read();
  78.   Serial.print("Distance: ");
  79.   Serial.print(distance);
  80.   Serial.println(" cm");
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement