If I put this code snippet into a brand new empty sketch then its working.
But If I put your example into my project sketch (it uses can bus (mcpCan) and OLED display (adafruit sh1106) then its not working. The valueStr is null.
Could the Low memory warning cause this?
End of the build I got this message:
Sketch uses 23162 bytes (71%) of program storage space. Maximum is 32256 bytes.
Global variables use 1832 bytes (89%) of dynamic memory, leaving 216 bytes for local variables. Maximum is 2048 bytes.
Low memory available, stability problems may occur.
What is the format of the number on the can bus? The code you posted appears to be dividing by 10 to convert an integer representation of a float to an actual float. If that is the case, you do not need to use String or float to do the printing, it can all be done using integers.
Which library are you using for the display? Adafruit's libraries for OLED displays typically allocate the memory for the display buffer at runtime, so it does not show up in the ram usage from the compiler. If that is the case, you are definitely out of memory.
I tried to free up some dynamic memory (less global variables) and the conversion is working.
The problem is I need to store the can bus message variables into global variables because I don't know when I need them.
What are those messages like? Maybe you can deduplicate the data and only store the part that varies in a compact form.
If I understand the issue correctly, the data processing part and the display part each work alright on their own, but they won't work together. Is that it?
Car ECU data. AFR, RPM, EGT, CLT, TPS, etc... They are coming in 8 byte packages and some them uses 2 some of them uses only 1 byte. For example: 1512 can id package's first byte (0 index) holds the AFR info, sencond and third (0,1) the TPS info, and soo on.
Yes. The can data reader and the gfx part is working as expected. But If I try to use both of them, it causes error.
Btw, I checked the CAN data and only uses 128 bytes (as global variables) and the UNO (test board) has 1697. So I think the OLED and CAN bus library uses the memory
I checked their codes and I freed up some global variable and I have 351 free bytes after full rebuild. (It was only 120 before). And the display and CAN bus reading are working (conversion also).
It has to be a memory problem,then, Try to trim the fat from the OLED library: use less fonts (if you are using more than one right now), or even keep only the glyphs that you are actually using in your application, e.g. all the digits and just the letters to form the words you are actually displaying; get rid of unused symbols (&, £, !, {, }, @, things like that). If it isn't already done, drop Strings and their dynamic memory usage.