How to change integer to float?

Thanks David

This is working but I don't get the full range.

I've changed the 2nd line to:

int valueTenths = map (analogRead(A0), 0.1, 280, -400, +400);

and the best I can get is: -31.8 ─► +40.0

Thanks for the help; much appreciated.

so you miss about 10% of the range at the lower end!

Have you measured the input voltage with a multimeter to see the actual input range?
(at the moment all is connected).
what values do you get?

I connected a multi-turn pot in place of the sensor.

How connected?
something like this?

GND --------potmeter---------VCC
               ^
               |
              ADC

Exactly.

map does not deal with floating point so what you wrote really is

int valueTenths = map (analogRead(A0), 0, 280, -400, +400);

if analogRead does not return more than 280 (instead of 1023) you might want to change the analog reference to really get the full scale of the ADC.

You started the thread by making the Original Post so you are the Original Poster and.. both you and the first post are 'the OP'. It's more fun that way.

FYI OP; post 27 mentions changing the Analog Reference. Then the analog value won't compare to 5V but whatever V you feed the ARef pin. You will get better answers that way.

Whatever may be the VREF voltage for the ADC, the analogRead() will aways return 1023 when VIN is equal to VREF. Keeping the VREF at 5V, the user can physically limit the VIN voltage at 0V to 1.37V so that the ADC value is: 0 to 280.

The difificulty I am having with is to compute a VREF for which the analogRead() would return 280 for full scale VIN. If you ask me to compute VIN for which analogRead() would return 280 given that VREF = 5V (or 1.1V to 5V), I can do this calculation.

This is true, my question/point was about the real max voltage that is presented to the ADC. If the actual range OP gets is 0 to 280 then OP could consider a better VREF

You need to change Aref, as explained in post#20.

Feed the reference ~1.4V cause

5V * 280 / 1024 = 1.367etc volts.

And then 1023 will represent ~1.4V instead of 5V.

Not doing the math but try a voltage divider 280R (or 560 or 1120) to ground and 1024R (or 2048 or 4096) to the Ref pin.

A Redboard Uno R3 has a buil-in 1.1volt reference; Use it.

Default Aref (5volt) restricts you to a 0.9V - 0.1V = 0.8volt range from 5volt
= 0.8/5 * 1024 = ~164 A/D values.
Switching to 1V1 Aref in setup() changes that to .08/1.1 * 1024 = ~744 A/D values.
Almost the 800 values you need.

Sorry missed that . Totally right

The comment is wrong!

1.
AREF (not Aref) is the physical pin-21 (Fig-1) of the ATmega328P MCU through which external voltage (1.1V to 5.0V) is fed to the VREF pin of the internal ADC (Fig-2).


Figure-1:

2.
Internal 1.1V is connected with the VREF pin of the ADC (Fig-2) by excuting the following code:

analogReference(INTERNAL); //set Vref of ADC to internal 1.1V 


Figure-2:

3.
Hope, it helps.

Is there any range of this external voltage? Can I feed 0.256V at AREF-pin to get a resolution of 0.25 mV/LSBit?

I remember reading something in the datasheet that 1volt is the recommended minimum.

That's it!
My Vref = 3.3

(31/1023) * 3.3 = 0.1

(280/1023) * 3.3 =280

int valueTenths = map (analogRead(A0), 31, 280, -400, +400)

The only issue is my resolution is only 0.3º Why?

Because you are converting an input range of 250 to an output range of 801. A change of 1 in the input will cause a change of a bit over 3 in the output, and integer math drops any fractional part.

That is why you should use a lower reference voltage, so the input extends over more of the range of the ADC.

How about if I add an averaging function.

Would you have the code for this?

Thanks for the help.

Getting confused.
So you don't use a Redboard (Uno), as you told us in post#1.