#include "Wire.h"
#include "VL6180X.h"
// Pin 13 is connected to the SHUT pin on the left VL6180X module, sensor_l.
const int shutdown_l_low_pin = 13;
const int reset_low_l = 16;
const int reset_low_r = 17;
VL6180X sensor_l;
VL6180X sensor_r;
void setup() {
Serial.begin(9600);
Wire.begin();
pinMode(2, OUTPUT);
pinMode(shutdown_l_low_pin, OUTPUT);
pinMode(reset_low_l, OUTPUT);
pinMode(reset_low_r, OUTPUT);
// Disable sensor_l so we can set the address of sensor_r.
digitalWrite(shutdown_l_low_pin, LOW);
sensor_r.init();
sensor_r.configureDefault();
sensor_r.setTimeout(500);
// Set sensor_r to a different address to avoid I2C bus conflict.
sensor_r.setAddress(30);
// Now that we have changed the address of sensor_r, we can enable sensor_l.
digitalWrite(shutdown_l_low_pin, HIGH);
delay(100);
sensor_l.init();
sensor_l.configureDefault();
sensor_l.setTimeout(500);
digitalWrite(reset_low_l, HIGH);
digitalWrite(reset_low_r, HIGH);
}
int d_l_old = 255;
int d_r_old = 255;
void loop() {
int d_r = sensor_r.readRangeSingleMillimeters();
int d_l = sensor_l.readRangeSingleMillimeters();
const int mm_per_cycle_threshold = 5;
bool beep_l = (d_l < d_l_old - mm_per_cycle_threshold) && (d_l < 100);
bool beep_r = (d_r < d_r_old - mm_per_cycle_threshold) && (d_r < 100);
digitalWrite(reset_low_l, beep_l);
digitalWrite(reset_low_r, beep_r);
d_l_old = d_l;
d_r_old = d_r;
}