Rounding temperature on SSD1306 LCD

Like many I have a temperature sensor connected to a Nodemcu and want to additionally display the temperature on a SSD1306 LCD

I have read many posts on rounding, but none seems to work for me, or I just don't understand how to apply it to my sketch.

This is the code I have which displays a number with 2 decimals (27.86)
I would lite it to display only one (27.8) if rounded (27.9), it would be even better :slight_smile:

void displayTemp(){
  float t = mySensor.readTempC();
  display.clear();

  display.setTextAlignment(TEXT_ALIGN_CENTER);
  display.setFont(Syncopate_Bold_30);
  display.drawString(66, 22, String(t) + "c");

I have tried:

int t = mySensor.readTempC();

but that just cuts all decimals.

I have also seen

readTempC= round(readTempC* 100.0) / 100.0;

but I get an error.

And some others, but none works.

I bet it's something very simple, but I can't seem to do it.

Thanks

Add half the resolution.

This is my first project, I don't know how to add half the resolution.

Wat do I need to edit to achieve that?

but I get an error.

Are we supposed to guess what that error might be? Hint: post the actual error message.

Please post ALL your code, so we can see where the other errors are.

If you want resolution to be one decimal place, that is 0.1. Half of that is 0.05.
7.54 rounded to one decimal is 7.5. 7.56 rounded to one decimal is 7.6.
7.54 + 0.05 = 7.55, prints 7.5.
7.56 + 0.05 = 7.61, prints 7.6.

@ jremington: Below is all the code (I don't remember exactly what i did because I tried all sorts of code, but it was a compiling error)

Also @ jremington: I'm posting in forums for 20 years, and I posted a lot of info, clearly not what you were expecting, but I cannot know what I don't know. So I don't know what is useful to you. I want to solve this and will provide all the info necessary to get this working and am not expecting you to guess it.

@ DKWatson: I guess I'm doing something wrong, from your help I changed the code and put in the additional line "t = round(t+ 0.05);" but only get 27.00 from the reading 27.32 and I would like to have 27.3

void displayTemp(){
  float t = mySensor.readTempC();
  t = round(t+ 0.05);
  display.clear();

  display.setTextAlignment(TEXT_ALIGN_CENTER);
  display.setFont(Orbitron_Bold_12);
  display.drawString(66, 1, "- SALA -"); 

  display.setTextAlignment(TEXT_ALIGN_CENTER);
  display.setFont(Lato_Black_36);
  display.drawString(66, 18, String(t) + "º"); //66, 22 meio
void setup(){
Serial.begin(115200);
Wire.begin(D3, D4); //changed pins from D1 and D2 to D3 and D4 (first nr SDA, second SLC)
  Wire.setClock(100000); //changed pins from D1 and D2 to D3 and D4 (first nr SDA, second SLC)
{
  delay(10);

// Connect to WiFi access point.
Serial.println(); Serial.println();
Serial.print("Connecting to router: ");
Serial.println(WLAN_SSID);

WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
  delay(1000);
  Serial.println("Connecting...");
}
Serial.println();

Serial.println("Connection sussesfully established");
Serial.println("Local IP: "); Serial.println(WiFi.localIP());

//Settings BME280
  mySensor.settings.commInterface = I2C_MODE;
  mySensor.settings.I2CAddress = 0x76;
	
	mySensor.settings.runMode = 3; //Normal mode
	mySensor.settings.tStandby = 0;
	mySensor.settings.filter = 0;
	mySensor.settings.tempOverSample = 1;
  mySensor.settings.pressOverSample = 1;
	mySensor.settings.humidOverSample = 1;

  
  Serial.print("Program Started\n");
  Serial.println("Starting BME280... result of .begin(): 0x");
  
  //Calling .begin() causes the settings to be loaded???
  Serial.println(mySensor.begin(), HEX);

    Serial.println("");

	delay(10);  //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up.

 //LCD
   // Initialising the UI will init the display too.
  display.init();
  display.flipScreenVertically();

}}

void displayTemp(){
  float t = mySensor.readTempC();
  t = round(t+ 0.05);
  display.clear();

  display.setTextAlignment(TEXT_ALIGN_CENTER);
  display.setFont(Orbitron_Bold_12);
  display.drawString(66, 1, "- SALA -"); 

  display.setTextAlignment(TEXT_ALIGN_CENTER);
  display.setFont(Lato_Black_36);
  display.drawString(66, 18, String(t) + "º"); //66, 22 meio

//LCD
}

void loop(){
  displayTemp();
  display.display();
  delay(5000);

{
  MQTT_connect(); //not in original code, if not included, "reading failed"

  if(! mqtt.ping()) {
    mqtt.disconnect();
  }

	Serial.print("Temperature: ");
	Serial.print(mySensor.readTempC(), 2);
	Serial.println(" degrees C");

	Serial.print("Pressure: ");
	Serial.print(mySensor.readFloatPressure()/100.0, 2);
	Serial.println(" Pa");

  Serial.print("%RH: ");
  Serial.print(mySensor.readFloatHumidity(), 2);
  Serial.println(" %");
 
	Serial.println();
	
	delay(5000);
// PUBLISH TEMP and HUMIDITY
 int humidity_data = (int)mySensor.readFloatHumidity();  //int da 20
 float temperature_data = (float)mySensor.readTempC(); //float da 20.35

if (temperature_data<70){   //only publish temperature if reading is lower than 70
if (temperature_data>0){   //only publish temperature if reading is higher than 0
if (humidity_data<90){   //only publish humidity if reading is lower than 90
  
}
if (! pub_tempsala.publish(temperature_data))
  Serial.println(F("Failed to publish temperature"));
else
  Serial.println(F("Temperature published!"));
 
//if (! pub_humidity.publish(humidity_data))
//  Serial.println(F("Failed to publish humidity"));
//else
//  Serial.println(F("Humidity published!"));

 delay(5000);
 ///////////////////////////////////
}}
}
//LCD
}

// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;

// Stop if already connected.
if (mqtt.connected()) {
return;
}

Serial.println("Connecting to MQTT Broker... ");
Serial.println();

uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}

}
}

"t = round(t+ 0.05);" but only get 27.00 from the reading 27.32 and I would like to have 27.3

The round( ) function in C returns the nearest integer value of the float/double/long double argument passed to this function. For example, if the decimal fraction is from ”.1 to .4″, it returns the integer value less than the argument, or from “.5 to .9″, it returns the integer value greater than the argument.

There are various ways of doing what you want, for example to convert the number into a properly formatted string (character array) using sprintf(), snprintf(), or dtostrf(). Study the function documentation before attempting to use it.

Example:

void setup(){
  char buf[15];
  float x=26.508508;
  dtostrf(x,-10,1,buf);  //"-" omits leading blanks
  Serial.begin(9600);
  Serial.println(buf);  //prints 26.5
}
void loop(){}

Also @ jremington: I'm posting in forums for 20 years, and I posted a lot of info, clearly not what you were expecting, but I cannot know what I don't know. So I don't know what is useful to you.

The actual error message is useful to everyone interested in helping you. A vague statement like "I get an error" is usually not useful, and tends to irritate people.

This is a very basic fact that should be taken into consideration when posting help requests to strangers.

  display.drawString(66, 18, String(t) + "º"); //66, 22 meio

Perhaps you should read the documentation on the String class, and see what it does with the float argument, and what additional arguments you might pass to the constructor to change the default behavior.

Or, STOP USING THE String CLASS, and learn how to use the dtostrf() function to create a string instead.

downloaders:
@ DKWatson: I guess I'm doing something wrong, from your help I changed the code and put in the additional line "t = round(t+ 0.05);" but only get 27.00 from the reading 27.32 and I would like to have 27.3

Who said anything about using round()? I said simply add half the resolution, t += 0.05.