How do I code the conversion from Celsius to Fahrenheit?

/*
 * Arduino weather station with ST7735 color TFT display (128x160 pixel) and
 *   BME280 barometric pressure, temperature & humidity sensor.
 * This is a free software with NO WARRANTY.
 * https://simple-circuit.com/
 */
 
#include <Adafruit_GFX.h>      // include Adafruit graphics library
#include <Adafruit_ST7735.h>   // include Adafruit ST7735 TFT library
#include <Adafruit_BME280.h>   // include Adafruit BME280 sensor library
#include <SPI.h>
#define TFT_RST   7      // TFT RST pin is connected to arduino pin 8
#define TFT_CS    9      // TFT CS  pin is connected to arduino pin 9
#define TFT_DC    8     // TFT DC  pin is connected to arduino pin 10
// initialize ST7735 TFT library
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
 
// define device I2C address: 0x76 or 0x77 (0x77 is library default address)
#define BME280_I2C_ADDRESS  0x76
 
Adafruit_BME280  bme280;  // initialize Adafruit BME280 library
 
void setup(void)
{
  tft.initR(INITR_BLACKTAB);     // initialize a ST7735S chip, black tab
  tft.fillScreen(ST7735_BLACK);  // fill screen with black color
  tft.drawFastHLine(0, 30,  tft.width(), ST7735_WHITE);   // draw horizontal white line at position (0, 30)
 
  tft.setTextColor(ST7735_WHITE, ST7735_BLACK);  // set text color to white and black background
  tft.setTextSize(1);                 // text size = 1
  tft.setCursor(4, 0);               // move cursor to position (4, 0) pixel
  tft.print("ARDUINO + ST7735 TFT");
  tft.setCursor(19, 15);              // move cursor to position (19, 15) pixel
  tft.print("WEATHER STATION");
 
  // initialize the BME280 sensor
  if( bme280.begin(BME280_I2C_ADDRESS) == 0 )
  {  // connection error or device address wrong!
    tft.setTextColor(ST7735_RED, ST7735_BLACK);   // set text color to red and black background
    tft.setTextSize(2);         // text size = 2
    tft.setCursor(5, 76);       // move cursor to position (5, 76) pixel
    tft.print("Connection");
    tft.setCursor(35, 100);     // move cursor to position (35, 100) pixel
    tft.print("Error");
    while(1);  // stay here
  }
 
  tft.drawFastHLine(0, 76,  tft.width(), ST7735_WHITE);  // draw horizontal white line at position (0, 76)
  tft.drawFastHLine(0, 122,  tft.width(), ST7735_WHITE);  // draw horizontal white line at position (0, 122)
  tft.setTextColor(ST7735_RED, ST7735_BLACK);     // set text color to red and black background
  tft.setCursor(25, 39);              // move cursor to position (25, 39) pixel
  tft.print("TEMPERATURE =");
  tft.setTextColor(ST7735_CYAN, ST7735_BLACK);  // set text color to cyan and black background
  tft.setCursor(34, 85);              // move cursor to position (34, 85) pixel
  tft.print("HUMIDITY =");
  tft.setTextColor(ST7735_GREEN, ST7735_BLACK);  // set text color to green and black background
  tft.setCursor(34, 131);              // move cursor to position (34, 131) pixel
  tft.print("PRESSURE =");
  tft.setTextSize(2);                 // text size = 2
 
}
 
// main loop
void loop()
{
  char _buffer[8];
  // read temperature, humidity and pressure from the BME280 sensor
  float temp = bme280.readTemperature();    // get temperature in °C
  float humi = bme280.readHumidity();       // get humidity in rH%
  float pres = bme280.readPressure();       // get pressure in Pa
 
  // print temperature (in °C)
  if(temp < 0)    // if temperature < 0
    sprintf( _buffer, "-%02u.%02u", (int)abs(temp), (int)(abs(temp) * 100) % 100 );
  else            // temperature >= 0
    sprintf( _buffer, " %02u.%02u", (int)temp, (int)(temp * 100) % 100 );
  tft.setTextColor(ST7735_YELLOW, ST7735_BLACK);  // set text color to yellow and black background
  tft.setCursor(11, 54);
  tft.print(_buffer);
  tft.drawCircle(89, 56, 2, ST7735_YELLOW);  // print degree symbol ( ° )
  tft.setCursor(95, 54);
  tft.print("C");
 
  // 2: print humidity
  sprintf( _buffer, "%02u.%02u %%", (int)humi, (int)(humi * 100) % 100 );
  tft.setTextColor(ST7735_MAGENTA, ST7735_BLACK);  // set text color to magenta and black background
  tft.setCursor(23, 100);
  tft.print(_buffer);
 
  // 3: print pressure (in hPa)
  sprintf( _buffer, "%04u.%02u", (int)(pres/100), (int)((uint32_t)pres % 100) );
  tft.setTextColor(0xFD00, ST7735_BLACK);  // set text color to orange and black background
  tft.setCursor(3, 146);
  tft.print(_buffer);
  tft.setCursor(91, 146);
  tft.print("hPa");
 
  delay(1000);    // wait a second
 
}
 
// end of code.

This is the conversion formula (Celsius * 9 / 5 + 32)
I'm not very proficient at coding, so I don't know how to code the conversion. Any and all help is greatly appreciated.
Thanks in advace!

Try ((Celsius * 9.0 / 5.0) + 32)

As formalyAWOL says...

float tempF =  (tempC * 9.0 / 5.0) + 32.0;

pcbbc:
As formalyAWOL says...

float tempF =  (tempC * 9.0 / 5.0) + 32.0;

Where do I put the conversion? Do I mark out the original line?
I realize that I need to make other changes to the code such as "C" to "F" and will do so once the conversion works.

Again Thanks to all who replied!

It depends on what you want to do with the result. Do you wish to simply replace the Celsius output value with a Fahrenheit output value?

I want the results to display in Fahrenheit so yes I want to replace the Celsius output with Fahrenheit!

Where in the code do I put "float tempF = (tempC * 9.0 / 5.0) + 32.0;"?

When I replaced the original line with the one above I get and error that tempC was not defined

Malx56:
When I replaced the original line with the one above I get and error that tempC was not defined

Please post your best attempt

Malx56:
Where in the code do I put "float tempF = (tempC * 9.0 / 5.0) + 32.0;"?

In between where you obtain tempC, and where you use tempF.

The was the first homework assignment when my business partner and I took a "C" class at a community college in the Seattle area.

I am surprised that you can't figure it out.

Paul

C * 1.8 + 32
(eliminates one math operation)

1 Like

runaway_pancake:
C * 1.8 + 32
(eliminates one math operation)

Don't you think the compiler can do arithmetic?

TheMemberFormerlyKnownAsAWOL:
Don't you think the compiler can do arithmetic?

9/5 is a derivation (reduced fraction) of (bpF - fpF)/(bpC - fpC)+32 where bp, fp are boiling point and freezing point of water, (212-32)/(100-0)+32. So to really put compiler math to work, you can start from first principles. :wink:

TheMemberFormerlyKnownAsAWOL:
Don't you think the compiler can do arithmetic?

Does it boil it down then or with 9/5 do a multiplication and then a division (two operations)?

Compiling both ways shows 444 bytes are taken up.

float degCin = 50.0;
float Fdeg;
void setup() {
  // put your setup code here, to run once:
}
void loop() {
  Fdeg = 9.0 * degCin / 5.0 + 32.0;
}
float degCin = 50.0;
float Fdeg;
void setup() {
  // put your setup code here, to run once:
}
void loop() {
  Fdeg = 1.8 * degCin + 32.0;
}

runaway_pancake:
Does it boil it down then

Yes, at 100C or 212F. :slight_smile:

But seriously, modern compilers optimize all constant expressions by evaluating them at compile time, including floats and doubles.

1 Like

To those that replied without reading my question I stated that I have NO formal programming experience! I am teaching myself!! So everything isn't clear to me yet! SO after trying what I think could work and searching Google which can be more confusing. I am still at a lost, but will try the suggestions provided. SO to Paul_KD7HB if your not going to be helpful Don't ass- u-me that it's that it should be that simple to figure out for everyone! Too me your response is just useless!

To those that provided suggestions THANKS! I'll give them a try and report the results!

THANKS!

Malcolm W.