-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvl53l0x_dual.ino
163 lines (131 loc) · 4.13 KB
/
vl53l0x_dual.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "Adafruit_VL53L0X.h"
// address we will assign if dual sensor is present
#define LOX1_ADDRESS 0x30
#define LOX2_ADDRESS 0x31
#define LOX3_ADDRESS 0x32
#define LOX4_ADDRESS 0x33
// set the pins to shutdown
#define SHT_LOX1 5
#define SHT_LOX2 13
#define SHT_LOX3 23
#define SHT_LOX4 2
// objects for the vl53l0x
Adafruit_VL53L0X lox1 = Adafruit_VL53L0X();
Adafruit_VL53L0X lox2 = Adafruit_VL53L0X();
Adafruit_VL53L0X lox3 = Adafruit_VL53L0X();
Adafruit_VL53L0X lox4 = Adafruit_VL53L0X();
// this holds the measurement
VL53L0X_RangingMeasurementData_t measure1;
VL53L0X_RangingMeasurementData_t measure2;
/*
Reset all sensors by setting all of their XSHUT pins low for delay(10), then set all XSHUT high to bring out of reset
Keep sensor #1 awake by keeping XSHUT pin high
Put all other sensors into shutdown by pulling XSHUT pins low
Initialize sensor #1 with lox.begin(new_i2c_address) Pick any number but 0x29 and it must be under 0x7F. Going with 0x30 to 0x3F is probably OK.
Keep sensor #1 awake, and now bring sensor #2 out of reset by setting its XSHUT pin high.
Initialize sensor #2 with lox.begin(new_i2c_address) Pick any number but 0x29 and whatever you set the first sensor to
*/
void setID() {
// all reset
digitalWrite(SHT_LOX1, LOW);
digitalWrite(SHT_LOX2, LOW);
digitalWrite(SHT_LOX3, LOW);
digitalWrite(SHT_LOX4, LOW);
delay(10);
// all unreset
digitalWrite(SHT_LOX1, HIGH);
digitalWrite(SHT_LOX2, HIGH);
digitalWrite(SHT_LOX3, HIGH);
digitalWrite(SHT_LOX4, HIGH);
delay(10);
// activating LOX1 and reseting LOX2
digitalWrite(SHT_LOX1, HIGH);
digitalWrite(SHT_LOX2, LOW);
digitalWrite(SHT_LOX3, LOW);
digitalWrite(SHT_LOX4, LOW);
// initing LOX1
if(!lox1.begin(LOX1_ADDRESS)) {
Serial.println(F("Failed to boot first VL53L0X"));
while(1);
}
delay(10);
// activating LOX2
digitalWrite(SHT_LOX2, HIGH);
delay(10);
//initing LOX2
if(!lox2.begin(LOX2_ADDRESS)) {
Serial.println(F("Failed to boot second VL53L0X"));
while(1);
}
// activating LOX3
digitalWrite(SHT_LOX3, HIGH);
delay(10);
//initing LOX3
if(!lox3.begin(LOX3_ADDRESS)) {
Serial.println(F("Failed to boot third VL53L0X"));
while(1);
}
// activating LOX4
digitalWrite(SHT_LOX4, HIGH);
delay(10);
//initing LOX4
if(!lox4.begin(LOX4_ADDRESS)) {
Serial.println(F("Failed to boot fourth VL53L0X"));
while(1);
}
}
void read_dual_sensors() {
lox1.rangingTest(&measure1, false); // pass in 'true' to get debug data printout!
lox2.rangingTest(&measure2, false); // pass in 'true' to get debug data printout!
// print sensor one reading
Serial.print(F("1: "));
if(measure1.RangeStatus != 4) { // if not out of range
Serial.print(measure1.RangeMilliMeter);
} else {
Serial.print(F("Out of range"));
}
Serial.print(F(" "));
// print sensor two reading
Serial.print(F("2: "));
if(measure2.RangeStatus != 4) {
Serial.print(measure2.RangeMilliMeter);
} else {
Serial.print(F("Out of range"));
}
// print sensor three reading
Serial.print(F("3: "));
if(measure2.RangeStatus != 4) {
Serial.print(measure3.RangeMilliMeter);
} else {
Serial.print(F("Out of range"));
}
// print sensor four reading
Serial.print(F("4: "));
if(measure2.RangeStatus != 4) {
Serial.print(measure4.RangeMilliMeter);
} else {
Serial.print(F("Out of range"));
}
Serial.println();
}
void setup() {
Serial.begin(115200);
// wait until serial port opens for native USB devices
while (! Serial) { delay(1); }
pinMode(SHT_LOX1, OUTPUT);
pinMode(SHT_LOX2, OUTPUT);
pinMode(SHT_LOX3, OUTPUT);
pinMode(SHT_LOX4, OUTPUT);
Serial.println(F("Shutdown pins inited..."));
digitalWrite(SHT_LOX1, LOW);
digitalWrite(SHT_LOX2, LOW);
digitalWrite(SHT_LOX3, LOW);
digitalWrite(SHT_LOX4, LOW);
Serial.println(F("Both in reset mode...(pins are low)"));
Serial.println(F("Starting..."));
setID();
}
void loop() {
read_dual_sensors();
delay(100);
}