/
LIDARLite_Wire_Library_GetDistance_ContinuousRead.ino
50 lines (36 loc) · 1.57 KB
/
LIDARLite_Wire_Library_GetDistance_ContinuousRead.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
http://pulsedlight3d.com
This sketch demonstrates getting distance with the LIDAR-Lite Sensor
It utilizes the 'Arduino Wire Library'
*/
#include <Wire.h>
#define LIDARLite_ADDRESS 0x62 // Default I2C Address of LIDAR-Lite.
#define RegisterMeasure 0x00 // Register to write to initiate ranging.
#define MeasureValue 0x04 // Value to initiate ranging.
#define RegisterHighLowB 0x8f // Register to get both High and Low bytes in 1 call.
int reading = 0;
void setup()
{
Wire.begin(); // join i2c bus
Serial.begin(9600); // start serial communication at 9600bps
}
void loop()
{
Wire.beginTransmission((int)LIDARLite_ADDRESS); // transmit to LIDAR-Lite
Wire.write((int)RegisterMeasure); // sets register pointer to (0x00)
Wire.write((int)MeasureValue); // sets register pointer to (0x00)
Wire.endTransmission(); // stop transmitting
delay(20); // Wait 20ms for transmit
Wire.beginTransmission((int)LIDARLite_ADDRESS); // transmit to LIDAR-Lite
Wire.write((int)RegisterHighLowB); // sets register pointer to (0x8f)
Wire.endTransmission(); // stop transmitting
delay(20); // Wait 20ms for transmit
Wire.requestFrom((int)LIDARLite_ADDRESS, 2); // request 2 bytes from LIDAR-Lite
if(2 <= Wire.available()) // if two bytes were received
{
reading = Wire.read(); // receive high byte (overwrites previous reading)
reading = reading << 8; // shift high byte to be high 8 bits
reading |= Wire.read(); // receive low byte as lower 8 bits
Serial.println(reading); // print the reading
}
}