Advertisement
stephenvdavis

example read distance

Oct 17th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. //Example code of reading distance
  2.  
  3. #include <ComponentObject.h>
  4. #include <RangeSensor.h>
  5. #include <SparkFun_VL53L1X.h>
  6. #include <vl53l1x_class.h>
  7. #include <vl53l1_error_codes.h>
  8.  
  9. /*
  10. Reading distance from the laser based VL53L1X
  11. By: Nathan Seidle
  12. SparkFun Electronics
  13. Date: April 4th, 2018
  14. License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
  15.  
  16. SparkFun labored with love to create this code. Feel like supporting open source hardware?
  17. Buy a board from SparkFun! https://www.sparkfun.com/products/14667
  18.  
  19. This example prints the distance to an object.
  20.  
  21. Are you getting weird readings? Be sure the vacuum tape has been removed from the sensor.
  22. */
  23.  
  24. #include <Wire.h>
  25. #include "SparkFun_VL53L1X.h"
  26.  
  27. //Optional interrupt and shutdown pins.
  28. #define SHUTDOWN_PIN 2
  29. #define INTERRUPT_PIN 3
  30.  
  31. SFEVL53L1X distanceSensor;
  32. //Uncomment the following line to use the optional shutdown and interrupt pins.
  33. //SFEVL53L1X distanceSensor(Wire, SHUTDOWN_PIN, INTERRUPT_PIN);
  34.  
  35. void setup(void)
  36. {
  37. Wire.begin();
  38.  
  39. Serial.begin(9600);
  40. Serial.println("VL53L1X Qwiic Test");
  41.  
  42. if (distanceSensor.begin() == 0) //Begin returns 0 on a good init
  43. {
  44. Serial.println("Sensor online!");
  45. }
  46. }
  47.  
  48. void loop(void)
  49. {
  50. distanceSensor.startRanging(); //Write configuration bytes to initiate measurement
  51. int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
  52. distanceSensor.stopRanging();
  53.  
  54. Serial.print("Distance(mm): ");
  55. Serial.print(distance);
  56.  
  57. float distanceInches = distance * 0.0393701;
  58. float distanceFeet = distanceInches / 12.0;
  59.  
  60. Serial.print("\tDistance(ft): ");
  61. Serial.print(distanceFeet, 2);
  62.  
  63. Serial.println();
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement