Greetings!
I have this code that reads the data from a VL53L0X sensor, and converts it to mm and displays it on an OLED display. Everything works fine, but I would like to convert the mm output to a decimal value that increases the resolution of the output value. So, instead of the display reading 53mm, it would have an increased resolution that would display something like 53.2 instead, depending on the sensor's output. Code is below. Thanks in advance for any help, and I apologize for any formatting issues or forum faux pas since this is my first post.
/* This example shows how to get single-shot range
measurements from the VL53L0X. The sensor can optionally be
configured with different ranging profiles, as described in
the VL53L0X API user manual, to get better performance for
a certain application. This code is based on the four
"SingleRanging" examples in the VL53L0X API.
The range readings are in units of mm. */
#include <Wire.h>
#include <VL53L0X.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <MedianFilter.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
VL53L0X sensor;
MedianFilter test(10, 0);
// Uncomment this line to use long range mode. This
// increases the sensitivity of the sensor and extends its
// potential range, but increases the likelihood of getting
// an inaccurate reading because of reflections from objects
// other than the intended target. It works best in dark
// conditions.
//#define LONG_RANGE
// Uncomment ONE of these two lines to get
// - higher speed at the cost of lower accuracy OR
// - higher accuracy at the cost of lower speed
//#define HIGH_SPEED
//#define HIGH_ACCURACY
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
void setup()
{
Serial.begin(9600);
Wire.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
sensor.init();
sensor.setTimeout(250);
#if defined LONG_RANGE
// lower the return signal rate limit (default is 0.25 MCPS)
sensor.setSignalRateLimit(0.1);
// increase laser pulse periods (defaults are 14 and 10 PCLKs)
sensor.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, 18);
sensor.setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, 14);
#endif
#if defined HIGH_SPEED
// reduce timing budget to 20 ms (default is about 33 ms)
sensor.setMeasurementTimingBudget(20000);
#elif defined HIGH_ACCURACY
// increase timing budget to 200 ms
sensor.setMeasurementTimingBudget(200000);
#endif
// Clear the buffer.
display.clearDisplay();
display.setRotation(2);
display.display();
display.setTextColor(WHITE);
}
void displayDistance( int val){
display.clearDisplay();
display.setTextSize(3);
display.setCursor(0,0);
display.print(val);
display.setTextSize(2);
display.setCursor(75,0);
display.print("mm");
display.display();
delay(10);
}
void loop()
{
int o,r = sensor.readRangeSingleMillimeters();
test.in( r );
o = test.out();
Serial.print(o);
if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
Serial.println();
displayDistance( o );
}