Hello. I´m facing a problem with decimal places. About the Battery voltage, Realdash is showing 4.0V instead of 4.7V that would be the correct information. I´ve tried everything changing the parameters of the variable from int, to float and double on Arduino but nothing works.

Hello danielgotz2023
Post your sketch, well formated, with comments and in so called code tags "< code >" and schematic to see how we can help.
Have a nice day and enjoy coding in C++.
Please edit your post to add code tags. "< code >" button on post editor.
My Arduino Sketch for Realdash is very big, so I just informed the code related to the battery voltage which is the only parameter that I´m facing the problem.
bateria_mapeada is the parameter that reads the Battey Voltage.
Code:
int bateria_mapeada;
void ReadAnalogStatuses()
{
**// read analog pins (0-7) **
// 0 and 949 are the number of bits related to 0 and 16
// 0 and 16 is the batery voltage
bateria_mapeada = map(analogPins[2],0,949,0,16);
for (int i=0; i<7; i++)
{
analogPins[i] = analogRead(i);
}
// write first CAN frame to serial
SendCANFrameToSerial(3200, frameData);
// build 2nd CAN frame, Arduino digital pins and 2 analog values
memcpy(frameData, &digitalPins, 2);
memcpy(frameData + 2, &analogPins[0], 2);
memcpy(frameData + 4, &analogPins[1], 2);
memcpy(frameData + 6, &bateria_mapeada, 2);
I´ve tried to change to float and double the parameter "bateria_mapeada" but nothing works.
The documentation on the map() function may hold a clue:
The
map()function uses integer math so will not generate fractions, when the math might indicate that it should do so. Fractional remainders are truncated, and are not rounded or averaged.
Instead of mapping to 0..16, you could always map to 0..160 and then divide the result by 10 to get 1DP.
Please use code tags when posting code.
Thanks! Now is working.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.