Page 1 of 3

Type conversion not working? ESP32 Wroom

Posted: Sat Jul 23, 2022 11:49 pm
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 2005 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?

Re: Type conversion not working? ESP32 Wroom

Posted: Sun Jul 24, 2022 1:43 am
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

Re: Type conversion not working? ESP32 Wroom

Posted: Sun Jul 24, 2022 7:23 am
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

Re: Type conversion not working? ESP32 Wroom

Posted: Sun Jul 24, 2022 8:15 am
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

Re: Type conversion not working? ESP32 Wroom

Posted: Sun Jul 24, 2022 8:20 am
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:

Re: Type conversion not working? ESP32 Wroom

Posted: Sun Jul 24, 2022 11:52 am
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 50 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!!!!

Re: Type conversion not working? ESP32 Wroom

Posted: Sun Jul 24, 2022 12:31 pm
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

Re: Type conversion not working? ESP32 Wroom

Posted: Sun Jul 24, 2022 1:04 pm
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..

Re: Type conversion not working? ESP32 Wroom

Posted: Sun Jul 24, 2022 1:22 pm
by mnfisher
I see the problem..

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

Martin

Re: Type conversion not working? ESP32 Wroom

Posted: Sun Jul 24, 2022 1:49 pm
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..