How to compare float value

Hi everybody.

I'm trying to compare 2 floats value, I've read a couple of forums and I understand that since my values are floats, they have a lots of number after the comma so that's why they can't be compared, let's say that I want to have a value of 2 number after the comma, how do I achieve that?

Right now here's my code, I'm checking an ADC and compare to an old value, if there a change I update a TFT screen.

// Print the result from the ADC
  // Voltage Out
  float voltage2 = readAdc(2);
  float ADCVOUT = (((voltage2 / 5) * MaxVoltagePSU) * 2.44);
  if  (ADCVOUT != adcvoutOLD) {
    float adcvoutOLD = ADCVOUT;

That's it, any help would be appreciated.

if  (abs(ADCVOUT - adcvoutOLD) >= 0.01) {

The ADC reading is an integer. Why not compare that instead?

1 Like

Here is the problem with your code logic. Only compute the voltage value at the time you print it. Keep the raw ADC values as they are read as integers and compare the integers.
There is no comma or any other ASCII character in float. That is put there by the print logic.

You know what.... YOU ARE RIGHT!!!!
That's should be easier and then convert that to my float!!!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.