Serial Print Potentiometer to 2 Decimal Places

Hi,

I have seen lots of posts around this topic but I can't seem to find an answer for my problem.

I want to serial print (and later lcd print) the output of a Potentiometer to two decimal places. I have mapped the range to a percentage range (0-100) and then I have tried to float the value but this just gives me the values with two decimals places of zero i.e. 46.00%. I have then tried to map it to 10,000 and then divide by 100 hoping that this will resolve my problem but it has not.

Does anyone know of a way to do this at all?

int pot = A0;

void setup() {
  // put your setup code here, to run once:

pinMode(pot, INPUT);

Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:

 int sensorValueAct = analogRead(pot);
  map(sensorValueAct, 0, 1023, 0, 100);
  int PercentageAct = map(sensorValueAct, 0, 1023, 0, 10000);
  int PercentageActRound = ((PercentageAct/100)); //Convert Pot_Motor to % and Round for Display
Serial.print(float(PercentageActRound));
Serial.println(" % ");
delay(300);

}

  int PercentageActRound = ((PercentageAct/100)); //Convert Pot_Motor to % and Round for DisplayThis calculation will return an integer because you are dividing by 100
Try this instead

void loop()
{
  int sensorValueAct = analogRead(pot);
  map(sensorValueAct, 0, 1023, 0, 100);
  int PercentageAct = map(sensorValueAct, 0, 1023, 0, 10000);
  float PercentageActRound = (PercentageAct / 100.00); //Convert Pot_Motor to % and Round for Display
  Serial.print(PercentageActRound);
  Serial.println(" % ");
  delay(300);
}

Casting an integer as a float does not add back the decimal places lost by doing integer math. If you want decimal places you have to do the math in float:

const int pot = A0;

void setup() {
 // pinMode(pot, INPUT);  // Not needed for analogRead()
Serial.begin(9600);
}

void loop() {
  float PercentageAct = (analogRead(pot) / 1024.0) * 100.0;  // Percentage
  float PercentageActRound = PercentageAct + 0.005; // Round to 2 decimal digits or Display
  Serial.print(PercentageActRound);
  Serial.println(" % ");
  delay(300);
}

After all this time I still get sad because people use floats just to have a decimal place :frowning: It's such a pity Arduino has no decent support for fixed point math build in.

Hi all,

Both solutions worked as I needed so thanks for your input and also for the information about the float function.

Cheers!!

So I have implemented the code below into my sketch and the pressure output moves in 0.15 PSI increments.

Is it possible to have the outcome to be more accurate with the pressure range of the sensor (0, 145.038 PSI/ 0-10 bar) with a 5v input?

  int ExPressureValueAct = analogRead(Pressure_Sensor_In);
  int PercentageAct = map(ExPressureValueAct, 0, 1023, 0, 145038);
  float PercentageActFloat = (PercentageAct / 1000.00);
      lcd.setCursor(9, 1);
      lcd.print(PercentageActFloat);
      lcd.print(" PSI ");
lcd.print(PercentageActFloat, 3);

You would need an ADC with more resolution than the Arduino's 10 bit, 1023, a 12 bit, 4095 increments would give you 0.0375 PSI increments, or a 16 bit, 65535, 0.0022 PSI. (Theoretically).

@outsider,

Thanks, I thought I was bit restricted. Thanks for the clarification. I just got a 16 bit ADC, hopefully this resolves my problem.

lparry92:
@outsider,
Thanks, I thought I was bit restricted. Thanks for the clarification. I just got a 16 bit ADC, hopefully this resolves my problem.

Hi lparry. What is the specified percentage accuracy of your pressure transducer? Is it really 0.01% or better?

A very common mistake people make with ADCs is to obsess over the number of bits and the displayed (or apparent) accuracy, without giving enough thought to the actual transducer accuracy and noise level limitations.