I am experimenting using the map() method in an ArduinoIDE sketch in an attempt to obtain a pixel position for a horizontal meter in the range 0-320 from an input value in the range 0-5. However, when I do this I see in the method that the input must be 'long' type. Here's my example code:
float voltage = 2.5; float meterPosition = map(voltage, 0, 5, 0, 320); tft.fillRect(0, 0, meterPosition, 10, TFT_GREEN); tft.setCursor(10, 70); tft.setTextSize(3); tft.println(voltage); tft.println(meterPosition);
But when I run this, despite the input value being 50% of the input range, I expected the meterPosition value to be 160, but it is 128, due I think because the values for this method must be of type 'long' and so the 'float' value is rounded and becomes a 'long' type (I think that's what is happening!!).
I am confused as to the function of the map() method if you are unable to map any range to any other range without using decimals as the input. I am obviously missing something here, so what do I need to change in my map() method code for it to work in my example please, if I am unable to use floats?