#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
#include "Adafruit_VL53L0X.h"
// address we will assign if dual sensor is present
#define LOX1_ADDRESS 0x30
#define LOX2_ADDRESS 0x31
// set the pins to shutdown
#define SHT_LOX1 23
#define SHT_LOX2 5
// objects for the vl53l0x
Adafruit_VL53L0X lox1 = Adafruit_VL53L0X();
Adafruit_VL53L0X lox2 = Adafruit_VL53L0X();
// this holds the measurement
VL53L0X_RangingMeasurementData_t measure1;
VL53L0X_RangingMeasurementData_t measure2;
#define led1 18
#define led2 19
int sm1=0,sm2=0;
String sos="";
bool mask1=true,mask2=true;
void setID() {
// all reset
digitalWrite(SHT_LOX1, LOW);
digitalWrite(SHT_LOX2, LOW);
delay(10);
// all unreset
digitalWrite(SHT_LOX1, HIGH);
digitalWrite(SHT_LOX2, HIGH);
delay(10);
// activating LOX1 and resetting LOX2
digitalWrite(SHT_LOX1, HIGH);
digitalWrite(SHT_LOX2, LOW);
// initing LOX1
if(!lox1.begin(LOX1_ADDRESS)) {
Serial.println(F("Failed to boot first VL53L0X"));
while(1);
}
lox1.begin(LOX1_ADDRESS);
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);
}
lox2.begin(LOX2_ADDRESS);
}
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
sm1=measure1.RangeMilliMeter;
Serial.print(sm1);
} else {
Serial.print(F("Out of range"));
sm1=1500;}
Serial.print(F(" "));
// print sensor two reading
Serial.print(F("2: "));
if(measure2.RangeStatus != 4) {
sm2=measure2.RangeMilliMeter;
Serial.print(sm2);
} else {
Serial.print(F("Out of range"));
sm2=1500; }
Serial.println();
}
void setup() {
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.setCursor(1,1);
lcd.print("-------------");
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(led1, OUTPUT);
pinMode(led2, OUTPUT);
Serial.println(F("Shutdown pins inited..."));
digitalWrite(SHT_LOX1, LOW);
digitalWrite(SHT_LOX2, LOW);
Serial.println(F("Both in reset mode...(pins are low)"));
Serial.println(F("Starting..."));
setID();
}
void loop() {
read_dual_sensors();
// all unreset
if (sm1<1000 and mask1) {sos+='1';mask1=false;}
if (sm2<1000 and mask2) {sos+='2';mask2=false;}
if (sos=="12") {digitalWrite(led1,1);digitalWrite(led2,0);mask1=true;mask2=true;sos="";
lcd.setCursor(1,0);
lcd.print("EXIT ");
}
if (sos=="21") {digitalWrite(led1,0);digitalWrite(led2,1);mask1=true;mask2=true;sos="";
lcd.setCursor(1,0);
lcd.print("ENTRANCE ");
}
delay(25);
}