Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. /*------------------------------------------------------------------------------
  2.  
  3. LIDARLite Arduino Library
  4. GetDistancePwm
  5.  
  6. This example shows how to read distance from a LIDAR-Lite connected over the
  7. PWM interface.
  8.  
  9. Connections:
  10. LIDAR-Lite 5 Vdc (red) to Arduino 5v
  11. LIDAR-Lite Ground (black) to Arduino GND
  12. LIDAR-Lite Mode control (yellow) to Arduino digital input (pin 3)
  13. LIDAR-Lite Mode control (yellow) to 1 kOhm resistor lead 1
  14. 1 kOhm resistor lead 2 to Arduino digital output (pin 2)
  15.  
  16. (Capacitor recommended to mitigate inrush current when device is enabled)
  17. 680uF capacitor (+) to Arduino 5v
  18. 680uF capacitor (-) to Arduino GND
  19.  
  20. See the Operation Manual for wiring diagrams and more information:
  21. http://static.garmin.com/pumac/LIDAR_Lite_v3_Operation_Manual_and_Technical_Specifications.pdf
  22.  
  23. ------------------------------------------------------------------------------*/
  24. int VAK_A = 1;
  25. int VAK_B = 2;
  26. int VAK_C = 3;
  27.  
  28. int currentState;
  29.  
  30. double pulseWidth;
  31.  
  32. void setup()
  33. {
  34. Serial.begin(115200); // Start serial communications
  35.  
  36. pinMode(2, OUTPUT); // Set pin 2 as trigger pin
  37. digitalWrite(2, LOW); // Set trigger LOW for continuous read
  38.  
  39. pinMode(3, INPUT); // Set pin 3 as monitor pin
  40. }
  41.  
  42. void loop()
  43. {
  44. pulseWidth = pulseIn(3, HIGH); // Count how long the pulse is high in microseconds
  45.  
  46. // If we get a reading that isn't zero, let's print it
  47. if(pulseWidth != 0)
  48. {
  49. pulseWidth = pulseWidth / 1000; // 10usec = 1 cm of distance
  50. printVak(pulseWidth);
  51.  
  52. Serial.println(pulseWidth); // Print the distance
  53. }
  54. }
  55.  
  56. void printVak(double pulseWidth){
  57. if(pulseWidth < VAK_A){
  58. if(currentState != VAK_A)
  59. Serial.println("Vak A");
  60. currentState = VAK_A;
  61. } else if(pulseWidth < VAK_B && pulseWidth > VAK_A){
  62. if(currentState != VAK_B)
  63. Serial.println("Vak B");
  64. currentState = VAK_B;
  65. } else if(pulseWidth < VAK_C && pulseWidth > VAK_B){
  66. if(currentState != VAK_C)
  67. Serial.println("Vak C");
  68. currentState = VAK_C;
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement