temperature, floats and byte´s (compare?)

Hello everybody!

Im trying to get my head around a part of my project, the temperature sensors to be precise..

I will be using LM75A temperature sensors over i2c bus when they eventually arrive from china, but im trying to get some code ready to test until then.

Now, i found this neat looking library, on github (LINK), but im not sure how to use it properly..

it boils down to this; i want to read a sensor, compare the result with a pre-set max-value and if its over the value, execute a command (in my case; turn off the voltage regulator thats too hot with a FET).

from what i gather from the .cpp file, the returned value is a float, when asked with a "lm75a.getTemperature()", and the value is already in °Celcius too.

The reference page on arduino.cc isnt too clear, but it suggests some very strange behaviors when doing math with floats and they should be avoided since they're slower as well..
But does that also include a comparator?

The returned value (from what i understand and guesstimate) would be something like 19.120°C in a room-temperature environment, and i would like to compare that float with a byte since i wont be needing hundreds of °C, say 80(°C) tops..

So could i do this straight up with a
"
byte byteTempLIMIT = 65; // temperature in °C for thermal cutoff
float floatTemp = lm75a.getTemperature();
if (floatTemp > byteTempLIMIT){ chillRoutine(); }
"
or would it demand more programmer wodoo?
If i cant compare bytes and floats, i will convert the float to a byte and then compare the two, but i would like to keep it simple, therefore avoid this step if possible.. :slight_smile:

You're trying to solve a problem that doesn't need solving... the LM75/A device and library mentioned implement an output that can go high or low when exceeding a setpoint and a hysteresis. Use the hardware and software already there, don't reinvent the wheel.

Read the datasheet, investigate the "O.S" pin.

You cannot compare floats and byte integers - the compiler will convert the byte into a float value automatically.

The "real trouble" is when you are checking if two floats are equal, which they almost never are. Less-than or greater-than are not a problem.

Well, i am aware that the chip has a output pin for temperature interrupts, however i failed to realize its use when i threw in the temp sensors as a afterthought into my project PCB design, and the order is already processed..

I already had the power, ground and i2c bus available nearby on the PCB and i figured why not since the temp sensors were already in the mail from china.

Also, i already have the arduino controlling each voltage regulator via mosFETs, allowing software on/off of each group, meaning that adding a temperature trigger shouldn't interfere with my current code too much.

Ive never used floats before and ive been advised not to use it on arduino, but im kind of without options here..
Its ether floats or no temp sensors.. :confused:

having said that; ive tried some code to try to visualize it a bit (hence; code for deveopment, not for actual use), but floats are somewhat strange to me..

Is there a way to filter out the rest of the decimals if i only want 1(or 2)?
To translate a 12,3456789 into 12,3 or 12,34?

And could i possibly convert a float into a byte by removing ALL of the decimal numbers?

The code below does compile and appears to me to fulfill the required function, but im no programmer (yet), so its probably a cumbersome way of doing things..
The code also depends on some global values being defined witch are not in the code snippet.

const byte tempLimit[4] = { 50, 75, 75, 75 }; contains each max °C value and is the only relevant value container not initially set to zero.

EDIT:
Oh, and i forgot to mention, the routine in the code snippet below is ment to be called once a second or so, to minimize i2c bus traffic.
This means each sensor only gets read once every 4 seconds, but im fine with that.. :slight_smile:
/EDIT

void TempSensors(){
  thisTempSensor++; // select the next temperature sensor (LM75A)
  if (thisTempSensor > 4){ thisTempSensor = 1; } // if overflow, goto 1
  byte thisLimit = thisTempSensor--;        // thisLimit is 0-index, thisTempSensor is 1-index
  float thisThermRead = 0;        // prepare for probe
  switch(thisTempSensor){              // read currently selected sensor
    case 1:{  // battery
      lm75aBatt.wakeup();
      thisThermRead = lm75aBatt.getTemperature();  // returns float temperature in °C
      lm75aBatt.shutdown();
    }break;
    case 2:{ // Main system power provider
      lm75aMain.wakeup();
      thisThermRead = lm75aMain.getTemperature();  // returns float temperature in °C
      lm75aBatt.shutdown();
    }break;
    case 3:{ // auxiliary systems
      lm75aAux.wakeup();
      thisThermRead = lm75aAux.getTemperature();  // returns float temperature in °C
      lm75aAux.shutdown();
    }break;
    case 4:{ // hiPWR LED power output
      lm75aEL.wakeup();
      thisThermRead = lm75aEL.getTemperature();  // returns float temperature in °C
      lm75aEL.shutdown();
    }break;
  } // end switch
  
  float temperatureLimit = tempLimit[thisLimit]; // read in the byte temperature limit as float
  if (thisThermRead > temperatureLimit){    // if current temp is over the limit;
    bitSet(actionFlags, thisTempSensor);  // set the actionflags container with the corresponding flagg.
  }
} // end TempSensors

Is there a way to filter out the rest of the decimals if i only want 1(or 2)?

No, you cannot control the internal representation of a float variable, only its printed representation.

Use serial.print(variable,n) to print n places past the decimal point.

Floats work fine on an Arduino, you just need to learn how to use them.

}break;

The break comes before the curly bracket in switch/case.