LCD Advanced

For general Flowcode discussion that does not belong in the other sections.
Post Reply
PAAbbott
Posts: 18
http://meble-kuchenne.info.pl
Joined: Mon Dec 28, 2020 7:24 am
Location: South Africa
Has thanked: 17 times
Been thanked: 4 times

LCD Advanced

Post by PAAbbott »

Good day Flow code members

For the project that I am working on I need to display a . decimal value on a i2c 4x20 LCD variable.

from my sensor it gives me distance measurement with a resolution of 0.2 meters so the variable changes as the distance decreases/increase so
the value of the variable changes for example 1.0 m, 1.2, 1.4, 1.6, ..... when i use print number and the variable i only get 1,2,3,....meter so the
point 2, .4, .6,..... how do i get that part to display on a lcd

I suspect this is some advanced operations of lcd displays

any help and pointers will be appreciated

kind regards
Andre

Steve-Matrix
Matrix Staff
Posts: 1269
Joined: Sat Dec 05, 2020 10:32 am
Has thanked: 168 times
Been thanked: 285 times

Re: LCD Advanced

Post by Steve-Matrix »

Hi Andre,

To print a floating point value to the LCD, first convert your value to a string variable (in a calculation icon, use the FloatToString$ function) and then display the string.

medelec35
Matrix Staff
Posts: 1464
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 514 times
Been thanked: 473 times

Re: LCD Advanced

Post by medelec35 »

I would like to add to Steve's post.
If you want to round the float to a number of decimal places e.g 2
Then you could use

Code: Select all

ResultString = FloatToString$ (ResultFloat,2)
Martin

PAAbbott
Posts: 18
Joined: Mon Dec 28, 2020 7:24 am
Location: South Africa
Has thanked: 17 times
Been thanked: 4 times

Re: LCD Advanced

Post by PAAbbott »

Good day Steve-Matrix & medelec35

Thank you for your response it is highly appreciated

I would like to know does this mean that all the variables involved with the calculation preceding this display has to be floats as well or only the answer of the calculation the calculation code is as follows

.Temp_LongDis_msb(This is a byte currently) = .D1(This is a byte currently) << 5 // shift msb to make space for Lsb

.Temp_LongDis_Lsb(This is a byte currently) = .D2(This is a byte currently) AND .Filter_OBJ_DistLong_Lsb (This is a byte currently) // filter relevant bits

.Temp_LongDis_Lsb = .Temp_LongDis_Lsb >> 3 // shift to accommodate msb

.Temp_LongDis_DATA(This is a long currently) = .Temp_LongDis_msb + .Temp_LongDis_Lsb // combine msb and Lsb to form data

.Output_Obj_Dist_Long (This is a long currently but needs to change to float )= (.Temp_LongDis_DATA * 0.2) - 500 // data translate to output value in meters

(D1 & D2 is bytes coming from my can bus sensor)

followed by

.test2 = FloatToString$ (.Output_Obj_Dist_Long,2) // converting float to string with two digits after the comma

then component macro

printstring (.test2)

The reason for this question is because when I tried changing the variable type (of .Output_Obj_Dist_Long )it would not allow me to select float
or am I missing some thing in the bigger picture of things

Thank you for your time and effort it is highly appreciated

kind regards

Andre

medelec35
Matrix Staff
Posts: 1464
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 514 times
Been thanked: 473 times

Re: LCD Advanced

Post by medelec35 »

Hi, Andre.
I'm not sure of the reason, but you can only change a variable to a float if it has not been used.
You could add a float, then go into the calculations and change

Code: Select all

 Output_Obj_Dist_Long
to your new Float variable.
Steve is in the middle of doing a new update, perhaps forcing a used variable from long to float or string etc. could be something to look at?
Of course, it may be done like that for a reason so may not be able to change it?
Martin

Steve-Matrix
Matrix Staff
Posts: 1269
Joined: Sat Dec 05, 2020 10:32 am
Has thanked: 168 times
Been thanked: 285 times

Re: LCD Advanced

Post by Steve-Matrix »

I think that changing the variable type does not check the consistency of every initialisation, calculation, parameters, macro returns, etc. Changing between integer types causes no problems because for a byte variable, even "my_byte=-12345" is a valid statement (even though the value set will be in the range 0-255).

But when you convert a float to an int, then things can go wrong with the consistency of your project. For example, if "x=1.67" is legal for a float, but not for an int. And if the float variable "x" were to change to an int, then what should "1.67" be changed to?

Whilst changes from int to float should be perfectly legal, the other way around would cause problems. But the internal mechanism for changing variable types is symmetrical and I cannot easily implement it so that ints can change to floats, but the opposite is prohibited.

Ultimately, I think it's safer to keep floats as distinct from ints. The same goes for strings and other non-int variable types.

Steve-Matrix
Matrix Staff
Posts: 1269
Joined: Sat Dec 05, 2020 10:32 am
Has thanked: 168 times
Been thanked: 285 times

Re: LCD Advanced

Post by Steve-Matrix »

Hi Andre,

With regard to you original question, here's your current code:

.Temp_LongDis_msb = .D1 << 5
.Temp_LongDis_Lsb = .D2 AND .Filter_OBJ_DistLong_Lsb
.Temp_LongDis_Lsb = .Temp_LongDis_Lsb >> 3
.Temp_LongDis_DATA = .Temp_LongDis_msb + .Temp_LongDis_Lsb
.Output_Obj_Dist_Long = (.Temp_LongDis_DATA * 0.2) - 500
.test2 = FloatToString$ (.Output_Obj_Dist_Long,2)
printstring (.test2)

I would remove the "msb" byte variable (assuming it is not used elsewhere) and use the "DATA" long variable instead. Then use a "float" in place of "Output_Obj_Dist_Long". Note I have also changed "500" to "500.0" so Flowcode and the compiler can be certain that this calculation involves floats (it is probably not needed here, but is often good practice to ensure any intermediate calculation steps don't assume int-only calculations).

.Temp_LongDis_DATA (long) = .D1 << 5
.Temp_LongDis_Lsb (byte) = .D2 AND .Filter_OBJ_DistLong_Lsb
.Temp_LongDis_Lsb = .Temp_LongDis_Lsb >> 3
.Temp_LongDis_DATA = .Temp_LongDis_DATA + .Temp_LongDis_Lsb
.Output_Obj_Dist_Float (float) = (.Temp_LongDis_DATA * 0.2) - 500.0
.test2 = FloatToString$ (.Output_Obj_Dist_Float,2)
printstring (.test2)

Post Reply