I've been experimenting with the Example Program for VL53L4CD on Arduino Uno to measure distances. While the program is functional, it doesn't quite meet my requirements. Specifically, I'm aiming to display distance values with at least two decimal places. I've attempted to modify both the header file and the main program to use floating-point numbers, but unfortunately, it didn't yield the desired outcome.
Could anyone advise me on how to achieve this? Is it possible to obtain decimal precision with this setup, and if so, what steps should I take to implement it successfully?
I'd prefer to have the distances displayed in millimeters. I'm aware that the minimum resolution for distances is 1 millimeter. However, the displayed values seem to be rounded. For instance, when the actual distance is 2.71 millimeters, it appears as 3 millimeters on SM.
i changed the valuetype to float in headerfile and also in mainprogram. There was a "?" for distance in serial monitor.
distance=? like here. i cant upload anything so i have to paste it here. sorry
#include <Arduino.h>
#include <Wire.h>
#include <vl53l4cd_class.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <stdlib.h>
#define DEV_I2C Wire
#define SerialPort Serial
#ifndef LED_BUILTIN
#define LED_BUILTIN 13
#endif
#define LedPin LED_BUILTIN
// Components.
VL53L4CD sensor_vl53l4cd_sat(&DEV_I2C, A1);
/* Setup ---------------------------------------------------------------------*/
void setup()
{
// Led.
pinMode(LedPin, OUTPUT);
// Initialize serial for output.
SerialPort.begin(115200);
SerialPort.println("Starting...");
// Initialize I2C bus.
DEV_I2C.begin();
// Configure VL53L4CD satellite component.
sensor_vl53l4cd_sat.begin();
// Switch off VL53L4CD satellite component.
sensor_vl53l4cd_sat.VL53L4CD_Off();
//Initialize VL53L4CD satellite component.
sensor_vl53l4cd_sat.InitSensor();
// Program the highest possible TimingBudget, without enabling the
// low power mode. This should give the best accuracy
sensor_vl53l4cd_sat.VL53L4CD_SetRangeTiming(200, 0);
// Start Measurements
sensor_vl53l4cd_sat.VL53L4CD_StartRanging();
}
void loop()
{
uint8_t NewDataReady = 0;
VL53L4CD_Result_t results;
uint8_t status;
char report[64];
do {
status = sensor_vl53l4cd_sat.VL53L4CD_CheckForDataReady(&NewDataReady);
} while (!NewDataReady);
//Led on
digitalWrite(LedPin, HIGH);
if ((!status) && (NewDataReady != 0)) {
// (Mandatory) Clear HW interrupt to restart measurements
sensor_vl53l4cd_sat.VL53L4CD_ClearInterrupt();
// Read measured distance. RangeStatus = 0 means valid data
sensor_vl53l4cd_sat.VL53L4CD_GetResult(&results);
snprintf(report, sizeof(report), "Status = %3u, Distance = %.2f mm, Signal = %6u kcps/spad\r\n",
results.range_status,
results.distance_mm,
results.signal_per_spad_kcps);
SerialPort.print(report);
}
//Led off
digitalWrite(LedPin, LOW);
}
Starting...
Status = 0, Distance = 16 mm, Signal = 444 kcps/spad
Status = 0, Distance = 14 mm, Signal = 444 kcps/spad
Status = 0, Distance = 15 mm, Signal = 447 kcps/spad
Status = 0, Distance = 14 mm, Signal = 444 kcps/spad
Status = 0, Distance = 16 mm, Signal = 443 kcps/spad
it is when i change it in header file.This is the headerfile. i´ve tried to change
uint16_t distance_mm; to float distance_mm; and the #define VL53L4CD_RESULT_DISTANCE ((uint16_t)0x0096) to float.
Maybe this is dated information, but it seems %f might not be implemented on Arduino's less-than-complete compiler... Try the multi-line equivalent to your snprintf();