Advertisement
pleasedontcode

Distance Measurement rev_01

Oct 21st, 2024
90
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: 2024-10-21 18:30:28
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* вывод показаний на монитор порта */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24. #include <Wire.h>
  25. #include <Adafruit_VL53L0X.h>   //https://github.com/adafruit/Adafruit_VL53L0X
  26.  
  27. /****** FUNCTION PROTOTYPES *****/
  28. void setup(void);
  29. void loop(void);
  30. void updateOutputs(void); // Prototype for the updateOutputs function
  31.  
  32. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  33. const uint8_t hjb_VL53L0X_GPIO_PIN_D3       = 3;
  34.  
  35. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  36. const uint8_t hjb_VL53L0X_XSHUT_PIN_D2      = 2;
  37.  
  38. /***** DEFINITION OF I2C PINS *****/
  39. const uint8_t hjb_VL53L0X_I2C_PIN_SDA_A4        = A4;
  40. const uint8_t hjb_VL53L0X_I2C_PIN_SCL_A5        = A5;
  41. const uint8_t hjb_VL53L0X_I2C_SLAVE_ADDRESS     = 41;
  42.  
  43. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  44. /***** used to store raw data *****/
  45. bool    hjb_VL53L0X_XSHUT_PIN_D2_rawData        = 0;
  46.  
  47. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  48. /***** used to store data after characteristic curve transformation *****/
  49. float   hjb_VL53L0X_XSHUT_PIN_D2_phyData        = 0.0;
  50.  
  51. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  52. Adafruit_VL53L0X lox = Adafruit_VL53L0X(); // Initialize the VL53L0X object
  53.  
  54. void setup(void)
  55. {
  56.     // Start serial communication at 115200 baud rate
  57.     Serial.begin(115200);
  58.     while (!Serial) delay(1); // Wait for the serial port to open
  59.  
  60.     // Initialize GPIO and XSHUT pins
  61.     pinMode(hjb_VL53L0X_GPIO_PIN_D3, INPUT_PULLUP);
  62.     pinMode(hjb_VL53L0X_XSHUT_PIN_D2, OUTPUT);
  63.  
  64.     // Initialize the VL53L0X sensor
  65.     if (!lox.begin()) {
  66.         Serial.println(F("Failed to boot VL53L0X"));
  67.         while(1); // Halt if the sensor fails to initialize
  68.     }
  69. }
  70.  
  71. void loop(void)
  72. {
  73.     // Refresh output data
  74.     updateOutputs();
  75.  
  76.     // Create a measurement data structure
  77.     VL53L0X_RangingMeasurementData_t measure;
  78.     lox.rangingTest(&measure, false); // Perform a ranging test
  79.  
  80.     // Check if the measurement is valid
  81.     if (measure.RangeStatus != 4) {
  82.         // Output the measured distance to the serial monitor
  83.         Serial.print("Distance (mm): ");
  84.         Serial.println(measure.RangeMilliMeter); // Print the measured distance
  85.     } else {
  86.         Serial.println(" out of range "); // Indicate if the measurement is out of range
  87.     }
  88.  
  89.     delay(100); // Delay for a short period before the next loop iteration
  90. }
  91.  
  92. void updateOutputs()
  93. {
  94.     // Update the output state
  95.     digitalWrite(hjb_VL53L0X_XSHUT_PIN_D2, hjb_VL53L0X_XSHUT_PIN_D2_rawData);
  96. }
  97.  
  98. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement