How to show correct value from ADC input

I have to measure a voltage. In this example I see this  int Voltage = (rawRFlevel * 5.0 ) / 1024.0; // scale the ADCwhere 1023 is used as the divider, but in other examples I see 1024 being used.

The reason for asking is that I get two diferent results using this code{code} int newPercent;
TFTscreen.setTextColor(TEXTCOLOR, BACKCOLOR);
TFTscreen.setTextSize(3);

rawRFlevel = analogRead(RFlevel);
TFTscreen.setCursor(0, 0);
int Voltage = (rawRFlevel * 5.0 ) / 1024.0; // scale the ADC
TFTscreen.print(Voltage, 3);

TFTscreen.setCursor((screenWidth / 2), screenHeight / 2);

newPercent = int((rawRFlevel / 1024.0) * 100.0); // Get a percentage for the reading[/code]

To test this I connected a potentiometer to AN0 so I can slide between 0V and +5V.

So with the slider at the 0V position, Voltage=0 and newPercent shows also 0.
If I move the slider to the +5V position I get a reading 10 for Volate and aproxx. 2219 for the newPercentage value.

Moving the slider halfway Voltage shows 20 and newPercent shows 1210.

As this makes no sense to me, so what maybe wrong??

Regards

Harry H.

sketch_oct17b.ino (3.37 KB)

The correct value is 1024, but 1023 will make no practical difference.

Do you think you can start using code tags when you post code?

so what maybe wrong??

Your arithmetic?

TheMemberFormerlyKnownAsAWOL:
The correct value is 1024, but 1023 will make no practical difference.

But it does in speed :slight_smile:

But than again, that's irrelevant here because all the floats...

TFTscreen.print(Voltage, 3);

There should not be a 2nd argument to .print() for an integer, unless you want to print in something other than base 10. Your output is likely being printing in base 3, but I'd have to run it on an arduino to actually see if it implements that correctly.

int num = 7;

void setup()
{
   Serial.begin(115200);
   Serial.println(num, 3);
}

void loop()
{

}

Prints 21, so base 3.