Int var1 = 0.5 + func(array); doesn't work to correct rounding?

I am converting a float to an integer and want the rounding to be correct. I am trying to do this by adding 0.5 before putting the value into the integer variable (averageFrontTemp) but for some reason I can't figure out, it just doesn't happen? It keeps rounding down.

Could anyone please tell me what might be wrong? Thanks.

Code excerpt:

averageFrontTemp = 0.5 + samplesToTemp(samplesFront);

Entire code:

// Thermistor Example #3 from the Adafruit Learning System guide on Thermistors
// https://learn.adafruit.com/thermistor/overview by Limor Fried, Adafruit Industries
// MIT License - please keep attribution and consider buying parts from Adafruit

#include <EEPROM.h> // Need this to store settings after power down

#define DEBUG 1 // value is 1 to enable debugging serial printing

#if DEBUG == 1
#define DEBUG(x) Serial.print(x)
#define DEBUGLN(x) Serial.println(x)
#else
#define DEBUG(x) 
#define DEBUGLN(x) 
#endif

// which analog pins to use with thermistors
#define THERMISTORPINFRONT A0
#define THERMISTORPINBACK A0 // A1
#define THERMISTORPINARMS A0 // A2
#define THERMISTORPINLEGS A0 // A3
// which high power pins to use to drive MOSFETS
#define HEATERMOSFETFRONT D5
#define HEATERMOSFETBACK D6
#define HEATERMOSFETARMS D2
#define HEATERMOSFETLEGS E4
// which digital pins to use for buttons
#define BUTTONSET D7 // D7
#define BUTTONMINUS D8 // D8
#define BUTTONPLUS D9 // D9
// resistance at 25 degrees C
#define THERMISTORNOMINAL 10000
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25
// how many samples to take and average, more takes longer
// but is more 'smooth'
#define NUMSAMPLES 5
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 3950
// the value of the resistor used to make a potential divider with the thermistor
#define SERIESRESISTOR 10000

// how frequently to read the thermistors and act on readings
const unsigned long temperatureResponseInterval = 5000;
const unsigned long thermistorReadShortInterval = 10;

// debounce setting for the buttons
unsigned long debounceDelay = 50;

// places to store average measured temperature
byte averageFrontTemp; // might make these arrays for the thermal runaway safety feature
byte averageBackTemp; // it will use a marker to keep track of which entry was the latest
byte averageArmsTemp; // reading and maybe? the remainder function to convert this into the 
byte averageLegsTemp; // oldest reading for the thermal runaway prevention

// places to store user-selected temperature settings
byte tempSettingFront = 45;
byte tempSettingBack = 45;
byte tempSettingArms = 45;
byte tempSettingLegs = 45;

// arrays to store thermistor readings prior to conversion to an average temperature
int samplesFront[NUMSAMPLES];
int samplesBack[NUMSAMPLES];
int samplesArms[NUMSAMPLES];
int samplesLegs[NUMSAMPLES];

// flags for timing of tasks
bool thermistorSamplingFlag = false;
bool thermistorSamplingCompleteFlag = false;
bool mosfetControlFlag = false;

// button states - need unsigned long lastDebounceTime = 0; for each switch
int buttonStateSet;
int lastButtonStateSet = HIGH;
int buttonStateMinus;
int lastButtonStateMinus = HIGH;
int buttonStatePlus;
int lastButtonStatePlus = HIGH;

void setup(void) {
  Serial.begin(9600);
  analogReference(EXTERNAL); // connect 3.3 V to AREF and to thermistor potential divider
  pinMode(HEATERMOSFETFRONT, OUTPUT);
  pinMode(HEATERMOSFETBACK, OUTPUT);
  pinMode(HEATERMOSFETARMS, OUTPUT);
  pinMode(HEATERMOSFETLEGS, OUTPUT);
  pinMode(BUTTONSET, INPUT_PULLUP);
  pinMode(BUTTONMINUS, INPUT_PULLUP);
  pinMode(BUTTONPLUS, INPUT_PULLUP);
}

void loop(void) {
  thermistorSamplingTimerTask();
  thermistorSamplingTask();
  thermistorSampleAveragingTask();
  mosfetControlTask();
}

// thermistor sampling is blocked when the sampling flag is false: this function sets the flag to
// true every temperatureResponseInterval seconds
void thermistorSamplingTimerTask() {
  static unsigned long previousMillis = 0;

  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis < temperatureResponseInterval) {
    return;
  }
  previousMillis = currentMillis;
  thermistorSamplingFlag = true;
  return;
}

void thermistorSamplingTask() {
  static unsigned long previousMillis = 0;
  static byte index = 0;

  if (thermistorSamplingFlag == false) {
    return;
  }

  unsigned long currentMillis = millis();
  // at the "thermistorReadShortInterval
  if (millis() -  previousMillis < thermistorReadShortInterval) {
    return;
  }
  previousMillis = millis();

  // take NUMSAMPLES samples
  samplesFront[index] = analogRead(THERMISTORPINFRONT);
  samplesBack[index] = analogRead(THERMISTORPINBACK);
  samplesArms[index] = analogRead(THERMISTORPINARMS);
  samplesLegs[index] = analogRead(THERMISTORPINLEGS);
  index = (index + 1) % NUMSAMPLES; // this resets index to 0 when array contains NUMSAMPLES values

  if (index == 0) { // when index is reset to 0, it's because the array is full and complete
    thermistorSamplingFlag = false; // sampling can now stop for a while
    thermistorSamplingCompleteFlag = true; // this lets the next stage of the loop begin
    return;
  }
}

void thermistorSampleAveragingTask() {

  // check whether array is full - this flag is set to true when the array is full
  if (thermistorSamplingCompleteFlag == false) {
    return;
  }
  // do the averaging here
  averageFrontTemp = 0.5 + samplesToTemp(samplesFront); // adding 0.5 prevents it always rounding down when turning float to int
  DEBUG("Front: ");
  DEBUG(averageFrontTemp);
  DEBUGLN("°C");
  averageBackTemp = 0.5 + samplesToTemp(samplesBack);
  DEBUG("Back: ");
  DEBUG(averageBackTemp);
  DEBUGLN("°C");
  averageArmsTemp = 0.5 + samplesToTemp(samplesArms);
  DEBUG("Arms: ");
  DEBUG(averageArmsTemp);
  DEBUGLN("°C");
  averageLegsTemp = 0.5 + samplesToTemp(samplesLegs);
  DEBUG("Legs: ");
  DEBUG(averageLegsTemp);
  DEBUGLN("°C");

  thermistorSamplingCompleteFlag = false; // reset this flag so sampling can begin again
}

// function for turning list of resistance measurements into temperature reading
byte samplesToTemp(int samplesZone[]) {
  float average = 0;

  // average all the samples out
  for (byte index = 0; index < NUMSAMPLES; index++) {
    average += samplesZone[index];
  }
  average /= NUMSAMPLES;

  DEBUG("Average analog reading ");
  DEBUGLN(average);

  // convert the value to resistance
  average = 4095 / average - 1;               // value 4095 is for LGT8F's 12-bit ADC; 1023 for Arduino
  average = SERIESRESISTOR / average;
  DEBUG("Thermistor resistance ");
  DEBUGLN(average);

  float steinhart;
  steinhart = average / THERMISTORNOMINAL;     // (R/Ro)
  steinhart = log(steinhart);                  // ln(R/Ro)
  steinhart /= BCOEFFICIENT;                   // 1/B * ln(R/Ro)
  steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
  steinhart = 1.0 / steinhart;                 // Invert
  steinhart -= 273.15;                         // convert absolute temp to C

  DEBUG("Temperature ");
  DEBUG(steinhart);
  DEBUGLN(" *C");
  return steinhart;
}

void mosfetControlTask() {

  // check flag to see if we're ready to set MOSFET outputs
  if (mosfetControlFlag == false) {
    return;
  }

  // compare temperature readings to thermostat settings and switch MOSFETs accordingly
  if (averageFrontTemp < tempSettingFront) digitalWrite(HEATERMOSFETFRONT, HIGH);
  else digitalWrite(HEATERMOSFETFRONT, LOW);
  if (averageBackTemp < tempSettingBack) digitalWrite(HEATERMOSFETBACK, HIGH);
  else digitalWrite(HEATERMOSFETBACK, LOW);
  if (averageArmsTemp < tempSettingArms) digitalWrite(HEATERMOSFETARMS, HIGH);
  else digitalWrite(HEATERMOSFETARMS, LOW);
  if (averageLegsTemp < tempSettingLegs) digitalWrite(HEATERMOSFETLEGS, HIGH);
  else digitalWrite(HEATERMOSFETLEGS, LOW);
}

I'm far from a coding expert but I believe a float to int simply truncates. If you need rounding you could do it manually or use one of the Arduino round functions.

1 Like

Looks like this function doesn't return float...
It returns byte.

In other words, it's already truncated when the function returns the value.

1 Like

Yes, I see now; the problem was in a different part of the code than where I was fiddling. Thanks very much.