Lidar-Lite v2 Show result just in Meters?

Love the sensor but I am struggling to alter the output and would appreciate some help please

Serial.println(myLidarLite.distance()); which shows number of centimetres eg 135

Is there a function (sorry not sure if that is the correct terminology?) that could just give me a result of

eg
Serial.println(myLidarLite.distanceM()); 1

Serial.println(myLidarLite.distanceCM()); 35

I tried reading the info in this file but it was well above my skill level!

github.com/PulsedLight3D/LIDARLite_v2_Arduino_Library/blob/master/LIDARLite/LIDARLite.cpp

Thanks in advance for any help

Hi,

This can be achieved by using integer division ( / ) and modulo ( % ) operations on the distance.

For example, to obtain the meters’ digits, you could simply perform a division by 100, such as: int meters = (135 / 100);

To obtain the centimeters’ digits (< 100), you would use a modulo by 100, such as: int meters = (135 % 100);

Please find attached to this post an example of this for Arduino.

Sincerely,
print_meters_and_centimeters.zip (732 Bytes)

Hi Sébastien,

Thank you so much for this, It will be the weekend before I get the chance to have a play with the code (and try and work out how it does what it does!) ha ha.

Thanks again, kind regards
[font=Helvetica Neue, Helvetica, Arial, sans-serif][size=4][highlight=#ffffff]
[/highlight][/size][/font]

Hi Sébastien,

Sorry first chance I have had to reply. Could you dumb this down some more for me please, I have tried for a few hours to adapt your example but still no joy.

I have a uno connected to i2c to an oled to display, code below

Sorry if this is very basic to some (most) people and I should have provided my code to begin with.

So no function already built in to provide the results in meters & centimetres, just centimetres only Serial.println(myLidarLite.distanceContinuous());

Any help would be very appreciated !

(A) My current working CM code that I understand (I am a beginner) (gets the data and puts it into 2 nice boxes on the oled, I just want meter as output)

#include "U8glib.h"
#include <Wire.h>
#include <LIDARLite.h>

LIDARLite myLidarLite;

U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE); // I2C / TWI 

void setup(void) {
Serial.begin(115200);
myLidarLite.begin();
}
void loop() {
Serial.println(myLidarLite.distanceContinuous());  

// picture loop
u8g.firstPage();  
do {
u8g.setContrast(128 );
  //   u8g.drawRFrame(0, 0, 84, 17, 3);   //Data1 frame
  //    u8g.drawRFrame(51, 16, 33, 10, 3); //Data1 label frame

u8g.drawRFrame(0, 0, 126, 64, 3);  //Outside Frame
u8g.drawRFrame(0, 0, 126, 30, 3);  //Title label frame
u8g.drawRFrame(0, 31, 126, 33, 3);  //Data frame
u8g.setFont(u8g_font_10x20);
//    u8g.drawStr(55, 24, "Data1");
u8g.drawStr(18, 18, "Distance");
u8g.setFont(u8g_font_10x20);
u8g.setPrintPos(2, 15);
//    u8g.print(thisMicros - lastMicros);
u8g.setPrintPos(22, 52);
u8g.print(myLidarLite.distance());
u8g.print(" cm");
} while( u8g.nextPage() );

delay(1000);
}

(B) Not working Meter code

Many many attempts but now I seem to be stuck with “I was not declared in this scope” but is this not declaring it? for(int i = 0; i < 11; i++); (I think this line means keep looping up to 11 times but that’s it sorry)

#include "U8glib.h"
#include <Wire.h>
#include <LIDARLite.h>

LIDARLite myLidarLite;

U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE); // I2C / TWI 

// Returns the number of integral meters from a distance in centimeters

int getMeters(int distanceInCM)
{
return (distanceInCM / 100);
}

// Returns the number of centimers (< 100) from a distance in centimeters
int getCM(int distanceInCM)
{
return (distanceInCM % 100);
}

void setup(void){
Serial.begin(115200);
myLidarLite.begin();
}
void loop() {
for(int i = 0; i < 11; i++);

Serial.print(getMeters(myLidarLite.distance));

// picture loop
u8g.firstPage();  
do {
u8g.setContrast(128 );
  //   u8g.drawRFrame(0, 0, 84, 17, 3);   //Data1 frame
  //    u8g.drawRFrame(51, 16, 33, 10, 3); //Data1 label frame

u8g.drawRFrame(0, 0, 126, 64, 3);  //Outside Frame
u8g.drawRFrame(0, 0, 126, 30, 3);  //Title label frame
u8g.drawRFrame(0, 31, 126, 33, 3);  //Data frame
u8g.setFont(u8g_font_10x20);
//    u8g.drawStr(55, 24, "Data1");
u8g.drawStr(18, 18, "Distance");
u8g.setFont(u8g_font_10x20);
u8g.setPrintPos(2, 15);
//    u8g.print(thisMicros - lastMicros);
u8g.setPrintPos(22, 52);
u8g.print(myLidarLite.distance());
u8g.print(" cm");
} while( u8g.nextPage() );

delay(1000);
}

Hi,

Please provide your code as attachments in the future. This makes it easier to read (the forum is only good for short code, like a few lines). If you do post code in the forum, please place code] and /code] (without the spaces after ‘****’) tag around them.

You code has a mistake at the line where you perform the Serial.print of the distance in meters:

Serial.print(getMeters(myLidarLite.distance)); This code treats distance as an attribute/variable and not a function, which it is. Simply add () and then the code will compile properly.

With modification:

Serial.print(getMeters(myLidarLite.distance()));

Sincerely,