Celsius to Fahrenheit conversion

Hi all I am not a programmer and I need just a little help. This is the only thermostat sketch I could find to work with the components I have. Would someone please help change this from Celsius to Fahrenheit?

It wont let me paste the whole code so I have attached the .ino

I appreciate your help

Home_Thermostat.ino (10.8 KB)

byte celsius = 26;
byte fahrenheit = ((celsius * 9) + 3) / 5 + 32;

Done as integer including rounding.

1 Like

septillion:

byte celsius = 26;

byte fahrenheit = ((celsius * 9) + 3) / 5 + 32;




Done as integer including rounding.

260C turns out as ((26*9) + 3)/5 + 32 = 79.40F?

00C turns out as ((0*9) + 3)/5 + 32 = 32.60F ? Why not 320F ?

just to catch the corner cases...

#define fah(x) ((((((x << 1) * 9) + 5) >> 1) / 5) + 32)

Done as integer including rounding.

@GolamMostafa, no it does not :wink: Turns it into 79°F and 32°F. Which seem fine to me :slight_smile:

@DKWatson, I hate to use macros what can be done better with functions with C++... But yeah, you can make the rounding better by doing
fahrenheit = ((celsius * 18) + 5) / 10 + 32;

@septillion

int fahr(int x) {return ((((((x << 1) * 9) + 5) >> 1) / 5) + 32);}

better for you? I just timed them. Both the function call and the macro take 3 clks.

septillion:
@GolamMostafa, no it does not :wink: Turns it into 79°F and 32°F. Which seem fine to me :slight_smile:

@DKWatson, I hate to use macros what can be done better with functions with C++... But yeah, you can make the rounding better by doing
fahrenheit = ((celsius * 18) + 5) / 10 + 32;

You are not only fine; you are super fine. The output turns out exactly 790F and 320F for 260C and 00C.

void setup()
{
  Serial.begin(9600);// put your setup code here, to run once:
  byte celsius = 26;
  byte fahrenheit = (celsius * 9) / 5 + 32;
  Serial.print(fahrenheit, DEC);
}

void loop() 
{
  
}

When the data types in the above codes are changed to unsigned char, I am getting 78 and 32 instead of 79 and 32. byte and unsigned char are not referring to the identical data types?
//-----------------------------------------------------------------------------------------------------------------

septillion:

byte celsius = 26;

byte fahrenheit = ((celsius * 9) + 3) / 5 + 32;




Done as integer including rounding.

In reality, 3 is not a magic number; but, apparently it is so! I am going to throw it tomorrow morning among 150 pupils and hopefully there will be some one much better than I.

GolamMostafa:
You are not only fine; you are super fine. The output turns out exactly 790F and 320F for 260C and 00C.

what's this zero to exponent to get the degree symbol? I've that on my keyboard, here are a few ones I don't use and can give away for free to copy and paste

32° 79° 26° 0°

:slight_smile:

I really appreciate the fast replies. I really am not a programmer and have no idea how to change the sketch. What I would like the sketch to do is display Fahrenheit instead of Celsius.
I don't know how to do that.

Will it take a whole re-write or can it be done with what there is.

Thanks again

gcarlyle22:
I really appreciate the fast replies. I really am not a programmer and have no idea how to change the sketch. What I would like the sketch to do is display Fahrenheit instead of Celsius.
I don't know how to do that.

Will it take a whole re-write or can it be done with what there is.

Thanks again

As stated, septillion did the hard part in the first reply. You need to change one line of your code from:

 te = dht.readTemperature();

to:

te = ((dht.readTemperature() * 9) + 3) / 5 + 32;

This DOES mean that you need to learn to modify code using the IDE. :slight_smile:

Or may be just read the doc of the library...

  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

lol :smiley:

septillion:
... :smiley:

Perfect!

Wot???? You meen der is no utoob vid 4 dis? Wtf?

Delta_G:
You overestimate these kids these days. They can't read for understanding, that would require thinking. It's just like everything else they have, if someone isn't there to hand it to them then they just won't have it.

I think hanging round here has given you a jaundiced view of youth. There are still some kids who read stuff and think things through. But by definition they're the ones we don't see here asking basic questions and failing to understand/believe the answers. They're just writing programs and making stuff work themselves. I do wish there were a few more of them though.

Steve

Again thank you. te = ((dht.readTemperature() * 9) + 3) / 5 + 32; worked perfectly.

As I said I am not a programmer and I am too old to start learning it nor do I want to.

I remember the old BBS systems where you could go and get help and people would actually help without dogging on you.

Then the internet and forums came out and in the beginning people would still help again not flaming a person for asking for help.

If I were a programmer then I might have know there was a help file in the library. Half the time when you get a file it's just the file nothing else. So why would a person look for a help file. I guess bad programming.

When you all started programming I guarantee you had to ask for help and the person you asked didn't say MAYBE IT's OVER YOUR HEAD. They helped you.

It is over my head that's why I asked. But please don't worry I will not be asking for anymore help from here.

gcarlyle22:
... I am not a programmer and I am too old to start learning it nor do I want to.
...

But you DID learn! Now you know that you have to do some math to the Celsius number to turn it into a Fahrenheit number.

I learned something too! An even easier fix to your code problem is to change the same line, but it's even simpler:

te = dht.readTemperature(true);

All the clues are in this thread. Can you figure out where it came from? If so, you are becoming a programmer. :wink:

Delta_G:
Maybe you need to spend some time learning the basics before you get to coding your own thermostat.

Which one of the following two is a basic (elementary and simple) to begin with?

OR

byte celsius = 26;
byte fahrenheit = ((celsius * 9) + 3) / 5 + 32;

gcarlyle22:
As I said I am not a programmer and I am too old to start learning it nor do I want to.

I'm sorry, but now you've hit a nerve. If what you say is true, then why are you here?

When I turned 60 I started teaching myself Python. Why, because it was there. I am not a programmer, but I've been doing it for almost 50 years and I still enjoy it and helping others. Nobody's dogging on you or flaming you. We are treating you with the same respect as others who enter this forum seeking advice. If you want candy-coated platitudes, this is the wrong forum. PC stands for program counter, not politically correct.

You're never too old, ask Gandhi.