Mini Lidar hex to dec

I bought myself a mini lidar and the code seems to work but I don't understnad the following code:

unsigned int t1 = Serial.read(); //Byte-3
unsigned int t2 = Serial.read(); //Byte-4
t2 <<= 8;
t2 += t1;
Lidar_afstand = t2;

You're forming a sixteen bit value from two eight bit values.

   unsigned int t1 = Serial.read(); //Byte-3
    unsigned int t2 = Serial.read(); //Byte-4
    t2 *= 256;
    t2 += t1;
    Lidar_afstand = t2;

yes, not a very common way of coding it, more common Lidar_afstand = (t2<<8) |  t1;

I bought myself a mini lidar and the code seems to work but I don't understand the following code:

unsigned int t1 = Serial.read(); //Byte-3 contains the 'lower byte' of the distance in HEX
unsigned int t2 = Serial.read(); //Byte-4 contains the 'higher byte' of the distance in HEX
t2 <<= 8; // this shifts the byte to the left 8 positions...?
t2 += t1; // adds the higher byte to the shifted lower byte...?
Lidar_distance = t2;

according to the specs now t2 contains a hexadecimal number (in mm)
I have no idea why not decimal, but anyway

I want a decimal value

Can I convert this t2 into a decimal number? and how?

I can't find a simple 'HEX to DEC function anywhere on the web

(No idea why those makers of the lidar give a decent example or library... grrrrrr)

That's just to misguide you :slight_smile: they mean they rebuilt the full 2 byte integer in t2

just print t2 and you'll see it in decimal

Serial.print(t2); // in decimal or Serial.print(t2, DEC); but DEC not needed as this is the default 
Serial.print(t2, HEX); // in hexadecimal
Serial.print(t2, BIN); // in base 2 (binary)

T1 and T2 are 8-bit binary numbers. Once combined into Lidar_distance you have a 16-bit binary number. You can then display the number in decimal:

Serial.print(Lidar_distance);

(No idea why those makers of the lidar give a decent example or library... grrrrrr)

No idea why you started a new thread with the same question when you already had an answer.

Grrrr.

Threads merged - do not cross-post.

according to the specs now t2 contains a hexadecimal number (in mm)

It also contains a binary number, also in mm

Amazing

AWOL:
It also contains a binary number, also in mm

Amazing

:slight_smile: