I'm doing basic work learning about potentiometer using the UNO. I can read in values ok over analog, but I would like double precision output to the serial monitor. I'm able to get that, but it looks the same, regardless if I use float or double as data types.
Here are my questions:
which data type is preferred to use for small precision, such as 2.35?
is there a way to specify (or force) a precision (e.g. 6 decimal places)
Float and double are the same on AVRs - they aren't on the Due and some other fancier microcontrollers, but they are for AVRs.
A common practice is to do all the math as integers, and only at the end, display that as a decimal - for example if you want 3 decimal places, do all your math as integers a factor of 1000 higher, then divide by 1000.0 at the end (or if printing, print number/1000, the decimal, and then number %1000 - thus avoiding floats entirely). Floating point math bloats the program size and the 32-bit floats aren't quite precise enough for a lot of applications.
As I have understood that you wish to display the voltage of the wiper point (volt) of your potentiometer with: (a) 2-digit after the decimal point (b) 6-digit after the decimal point
A: If it would be my project, I would carry out the following steps to print the value on Serial Monitor with 4-digit after decimal point (without any floating point calculation. Ref: Post#1; flash consumption: 1874 bytes):
C: Observation (a) Integer math consumes 1874 bytes flash, and the execution time is: 498 us (b) Float math consumes 3334 bytes flash, and the execution time is: 513 us.
If the 10-bit value is the ADC value (say: 0x0200 = 512 for 2.50 Volt), then it is going to be multiplied by 5 (getting 2560) and then divided by 1023 (getting: 2.50 = 0x0010/0x0011). My brain is not going to work how to obtain back ~=2.5 without amplifying the raw response of the ADC and avoiding the float math.