DS3231 temperature conversion from degree C to F

Hi
Could somebody please explain or show me how to convert this sketch to show degrees F on LCD instead of degees C
Thanks Ian

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <ds3231.h>
#define BUFF_MAX 128

//****************************************Define I2C LCD Display *********************************

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal_I2C lcd(0x27,16,2);
//************************************ END LCD DISPLAY *******************************************

uint8_t time[8];
char recv[BUFF_MAX];
unsigned int recv_size = 0;
unsigned long prev, interval = 1000;

void setup()
{
Serial.begin(9600);
Wire.begin();
DS3231_init(DS3231_INTCN);
memset(recv, 0, BUFF_MAX);
Serial.println(“GET time”);

//LCD Setup******
lcd.begin(16,2); // initialize the lcd
// Switch on the backlight
// lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
// lcd.setBacklight(LED_ON);
lcd.backlight();
//END LCD Setup*****

Serial.println(“Setting time”);
// setTheTime(“450115708062019”); // ssmmhhWDDMMYYYY set time once in the given format

}

void loop()
{
char tempF[6];
float temperature;
char buff[BUFF_MAX];
unsigned long now = millis();
struct ts t;
// show time once in a while
if (now - prev > interval){
DS3231_get(&t); //Get time
temperature = DS3231_get_treg(); //Get temperature (tempF × 9/5) + 32
dtostrf(temperature, 5, 1, tempF);

lcd.clear();
lcd.setCursor(0,0);

lcd.print(t.mday);

printMonth(t.mon);

lcd.print(t.year);

lcd.setCursor(0,1); //Go to second line of the LCD Screen
lcd.print(t.hour);
lcd.print(":");
if(t.min<10)
{
lcd.print(“0”);
}
lcd.print(t.min);
lcd.print(":");
if(t.sec<10)
{
lcd.print(“0”);
}
lcd.print(t.sec);

lcd.print(’ ');
lcd.print(tempF);
lcd.print((char)223);
lcd.print(" ");
prev = now;
}

}

void setTheTime(char *cmd)
{
struct ts t;

// ssmmhhWDDMMYYYY set time

t.sec = inp2toi(cmd, 0);
t.min = inp2toi(cmd, 2);
t.hour = inp2toi(cmd, 4);
t.wday = inp2toi(cmd, 6);
t.mday = inp2toi(cmd, 7);
t.mon = inp2toi(cmd, 9);
t.year = inp2toi(cmd, 11) * 100 + inp2toi(cmd, 13);
DS3231_set(t);
Serial.println(“OK”);
}

void printMonth(int month)
{
switch(month)
{
case 1: lcd.print(" January “);break;
case 2: lcd.print(” February “);break;
case 3: lcd.print(” March “);break;
case 4: lcd.print(” April “);break;
case 5: lcd.print(” May “);break;
case 6: lcd.print(” June “);break;
case 7: lcd.print(” July “);break;
case 8: lcd.print(” August “);break;
case 9: lcd.print(” September “);break;
case 10: lcd.print(” October “);break;
case 11: lcd.print(” November “);break;
case 12: lcd.print(” December “);break;
default: lcd.print(” Error ");break;
}
}

temperature = DS3231_get_treg(); returns the temperature in C.

to convert C to F, multiply by 9, divide by 5, and add 32 - just do this on the next line and you should get the value in F

temperature=temperature*9/5+32;

Did you try to write a function for this?
(tempF × 9/5) + 32

Hi

Thanks to both of you guys....tried both way and all is now good

This is not a Magic Formula:
ctof.png

Using the principles of co-ordinate geometry, we get:

(C - 0)/(F - 32) = (100 - 0)/(212 - 32)

==> F - 32 = (180/100)*C

==> F = (9/5)*C + 32

Example: Given that C = 100
==> F = (9/5)*100 + 32
==> F = 180 + 32 = 212 (ok!).

ctof.png

Hi

LOL..you guys are way too smart for me to keep up with all this.

Can any of you explain how to change sketch to read out a 12 hour time with am/pm instead of 24 hr

thx in advance

Ian

Hum, if the hour read is > 12 then you subtract 12 from the hour and make AMPM = true ???