From 66f61a69db69fce897e21f56fbf27f27eab7ccdb Mon Sep 17 00:00:00 2001 From: Ollo Date: Wed, 22 Mar 2023 20:50:25 +0100 Subject: [PATCH] Reset sensor on bad values --- esp32test/Esp32DeepSleepTest/src/main.cpp | 39 ++++++++++++++++------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/esp32test/Esp32DeepSleepTest/src/main.cpp b/esp32test/Esp32DeepSleepTest/src/main.cpp index 85f24e4..69588b9 100644 --- a/esp32test/Esp32DeepSleepTest/src/main.cpp +++ b/esp32test/Esp32DeepSleepTest/src/main.cpp @@ -11,16 +11,7 @@ VL53L0X tankSensor; -void setup() -{ - Serial.begin(115200); - pinMode(OUTPUT_SENSOR, OUTPUT); - tankSensor.setTimeout(500); - - digitalWrite(OUTPUT_SENSOR, HIGH); - Serial.println("Nodemcu ESP32 Start done"); - - tankSensor.setTimeout(500); +void initializeTanksensor() { Wire.begin(SENSOR_TANK_SDA, SENSOR_TANK_SCL, 100000UL /* 100kHz */); tankSensor.setTimeout(500); tankSensor.setBus(&Wire); @@ -48,11 +39,23 @@ void setup() tankSensor.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, 18); tankSensor.setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, 14); tankSensor.setMeasurementTimingBudget(200000); + tankSensor.startContinuous(); } else { Serial.println("Sensor init failed"); } } +void setup() +{ + Serial.begin(115200); + pinMode(OUTPUT_SENSOR, OUTPUT); + + digitalWrite(OUTPUT_SENSOR, HIGH); + Serial.println("Nodemcu ESP32 Start done"); + + initializeTanksensor(); +} + void loop() { delay(500); @@ -61,9 +64,21 @@ void loop() { if (!tankSensor.timeoutOccurred()) { uint16_t distance = tankSensor.readRangeSingleMillimeters(); - Serial.print("Distance"); - Serial.println(distance); + if (distance == 8191) { + Serial.println("Reset due to 8.191 meter"); + tankSensor.stopContinuous(); + Wire.end(); + delay(100); + initializeTanksensor(); + } else { + Serial.print("Distance"); + Serial.println(distance); + } } else { Serial.println("Timeout"); + tankSensor.stopContinuous(); + Wire.end(); + delay(100); + initializeTanksensor(); } }