Type conversion not working? ESP32 Wroom

For general Flowcode discussion that does not belong in the other sections.
MJU20
Posts: 238
http://meble-kuchenne.info.pl
Joined: Tue Dec 08, 2020 5:11 pm
Has thanked: 75 times
Been thanked: 50 times

Type conversion not working? ESP32 Wroom

Post by MJU20 »

In: https://flowcode.co.uk/forums/viewtopic ... 7540#p7540 BenR suggested to use Type Conversion to store a Float in EEPROM.

I'm using a ESP32 Wroom to do the trick but something is wrong..
My goal is to store a Float in a non-volatile memory (EEPROM) (see: https://flowcode.co.uk/forums/viewtopic ... 2573#p2573).

After nights of testing I don't get the results I want.
I decided to test the two needed tricks in different files and got that the EEPROM works, but the Type conversion is not doing it's work.

I've made this:
TypeConv1.jpg
TypeConv1.jpg (90.7 KiB) Viewed 1945 times
A random number is calculated.
This is set as Float and fetched as bytes.
These are printed on an display.

Simulated in FC9 I get as bytes: 0 128 196 67 but on the hardware all zero's...
So what am I doing wrong? Or is there a flaw in the Type conversion component?

mnfisher
Valued Contributor
Posts: 938
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 502 times

Re: Type conversion not working? ESP32 Wroom

Post by mnfisher »

This might be the right value - I certainly don't know what the binary value of a float is without doing something like this to check.

What value are you expecting / what are you trying to achieve here?
See something like https://www.google.com/url?sa=t&source= ... HHsHmN9xxe for details of the float format.

Martin

If you post code - please attach your program using the attachments tab below the message (full editor and preview first) to make it easier for others to check

chipfryer27
Valued Contributor
Posts: 1110
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 278 times
Been thanked: 397 times

Re: Type conversion not working? ESP32 Wroom

Post by chipfryer27 »

Hi

As Martin suggests it's always much easier to offer some help if we can see your flowchart as it is so very easy to "not see" something when debugging your own code :)

If your display is printing zero's it suggests it works but you are perhaps sending wrong info especially if it works with other print number commands. I haven't got one of those displays to check myself though.

I don't think you have a problem with the conversion. When you set the Float and obtain the resulting bytes remember Byte Zero is the least significant.

In the attached, I have set F0 to equal your temp of 393.6722 and this is the initial value.

I set this float value and convert into four bytes B3/B2/B1/B0

I then take these four bytes, set them and convert back into a new float value called F1

I also convert F0 and F1 into Strings so they can be printed (set to four decimal places).

Everything is then printed to the display before entering an endless loop. If you pause during simulation you will see each value in the Simulation Debugger. So if you want to store your Float into EEPROM as bytes your code should work fine as long as you remember to convert it back afterwards :)

Although I used a PIC in the example you can easily change to whatever you like.

Regards
Attachments
Conversion.fcfx
(18.81 KiB) Downloaded 51 times

mnfisher
Valued Contributor
Posts: 938
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 502 times

Re: Type conversion not working? ESP32 Wroom

Post by mnfisher »

:( Sorry - I missed the bit about all zeroes on hardware. In my defence it was the middle of the night :cry:

Please post your program....

Martin

chipfryer27
Valued Contributor
Posts: 1110
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 278 times
Been thanked: 397 times

Re: Type conversion not working? ESP32 Wroom

Post by chipfryer27 »

Some of my best work occurs in the middle of the night........

Doesn't say a lot for my day time abilities........ :lol:

MJU20
Posts: 238
Joined: Tue Dec 08, 2020 5:11 pm
Has thanked: 75 times
Been thanked: 50 times

Re: Type conversion not working? ESP32 Wroom

Post by MJU20 »

My night abilities are very bad (as you soon will learn), and my day abilities are even worse :D

I never use the simulation in Flowcode, for some reason I've learned that the behaviour in simulation is not always what they are on hardware.

The reason I posted the image is because I most of the time implement new things in projects that are already underway.
So I cleared all "not Type conversion" stuff out of this project and attach it here.
If there are odd things, this is because it used to be a part of a larger project.
Type conversion test MM.fcfx
(18.78 KiB) Downloaded 47 times
So this is a bit like the example that is posted on the Wiki for the Type conversion macro.
In simulation it works, but on the ESP32 it doesn't..

Thanks already!!!!

chipfryer27
Valued Contributor
Posts: 1110
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 278 times
Been thanked: 397 times

Re: Type conversion not working? ESP32 Wroom

Post by chipfryer27 »

Hi

Stepped through the program looking at variables. Your calculation for Temp returns 393.0 and not the 393.6722. Could this be your unexpected result? To fix have your calculation as Temp = 140541.0 / 357.0

This will give you a floating point answer you can shorten to 4 dp if required.

I notice when simulating that the numbers overlap each other so perhaps increase "y" to a difference of 12 or so, but this wouldn't be your major display issue.

Can you display other variables OK?

As you are getting zero's it is obviously connected just not getting the correct info. I assume you have checked all timings/ bauds etc?

Regards

MJU20
Posts: 238
Joined: Tue Dec 08, 2020 5:11 pm
Has thanked: 75 times
Been thanked: 50 times

Re: Type conversion not working? ESP32 Wroom

Post by MJU20 »

Hey Chipfryer,

Thanks for the suggestions.

The display is OK.
Just by adding a calculation that sets Byte1..4 to a certain value just before printing them to the display, I can acknowledge that the byte1..4 and the display are working ok.

After hours of testing, my idea is that there is something wrong with the Type conversion..

mnfisher
Valued Contributor
Posts: 938
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 502 times

Re: Type conversion not working? ESP32 Wroom

Post by mnfisher »

I see the problem..

You have Temp = 12345/ 243 (integer value converted to float)
Change to Temp = 12345.0 / 3576.0

Martin

MJU20
Posts: 238
Joined: Tue Dec 08, 2020 5:11 pm
Has thanked: 75 times
Been thanked: 50 times

Re: Type conversion not working? ESP32 Wroom

Post by MJU20 »

OK! Progress!!! Thanks guys..

But, strangely I need to test it again with my first program where Temp is a Float from the beginning.

I'm very glad with this help! Appreciate it enormously.
But this is what I mean with simulation vs hardware. The simulation gives results, the hardware doesn't.

Now back to my main program and test it ones again with what I believe must be a genuine Float..

Post Reply