Celcius to Fahrenheit with 20M

jpyle1

New Member
Hello to all. I have been playing with Stan's 20M (with the DS18B20) and it works great in Celcius. Hardware works perfectly. I have been attempting to get the output in Fahrenheit by adding a single line of code for conversion ----- b0= b0/5*9+32 -(multiply Celcius bt 1.8 and add 32)---- and it does display kind of "close" to the tested temp. I have used it after the first b0 read, then first line in the b1 section and again in the b2 section as well. Readings are not correct in that I get "68" in a 72deg room, "50" on an ice cube, and "86" on a 99deg coil. With my limited capabilities, I still struggle with the code. Even with Stan's Excellent line-by-line explainations, and knowing conversion the formula, Here is the code. Please, someone point me in the reght direction. Thanks, JP
(Stan,it will not let me upload the whole piece--Too many images??--
I just put up the first read section in as that is all I changed--Sorry)


'PICAXE-20M,7 seg LED & DS18B20 demo. Stan.SWAN + PICAXE "Forum" 3 May 2008
'Uses single 7 seg display,sequencing digits so 24°C temp="2" then "4" etc

'---------------------------------------------------------------------------
temp20m:readtemp 7,b0 'DS18B20 temp reading at 20M input 7
if b0>128 then gosub negtemps 'Sub zero temps value correction
b0= b0/5*9+32 - MY ONLY ADDED LINE -
b1= b0/10 'divide orig temp to get tens value
b2= b0//10 'divide orig temp so remainder yields units value
if b1=0 then units 'suppress "0" if temps between ±9°C,so "4" & not "04"
 

westaust55

Moderator
jpyle,

Firstly some general infrmation:
PICAXE BASIC only works with Integers (whole numbers). ASlo maths are computed in the order they are given in an assignment statement so generally always better to do the multiplications first and divisions later.

Next, in line with what Steve G has indicated the correct formula is:

b0 = b0 *9 / 5 +32

note "18/10" is the same as "9/5" but uses more characters and a couple of bytes more memory space in the program.
 

moxhamj

New Member
Steve Gs formula is deceptively simple. Integer maths generally means you lose accuracy and a way around that is to multiply by bigger numbers before doing divisions. So a*9/5 would, in general, be better as a*900/500. Or a*1800/1000. But this can then be reduced back to a*18/10 in this particular instance. In general terms, make the number as big as possible but not so big as to overflow more than 65535. Then do the division.

I find the way to work out these numbers is to work out the biggest number that will be multiplied, work out a multiplier number so it comes in just under 65535, then work out the divisor, and then put some random numbers in to test the accuracy and compare with what the accuracy would have been with small integers.

Steve G has already done this for you!

Of course, if you really want to impress your friends with your technical prowess, display the temperature in Kelvin! At least there are no complicated formulas for when the number goes under zero.
 

jpyle1

New Member
Thank you all. I thought it must be something simple that I was/wasnot doing. (I only have three brain cells now,and the middle one sputters alot). I will implement this and get it working, or move to a country that uses Celcius, I guess. Thanks again, JP
 

jpyle1

New Member
Another question?

I corrected the code as per instructed, and the pos temp tracks with 2 other thermometers spot on. from 36deg (fridge) or so to 98deg (elect heater). However, I do have a very good freezer that shows -11deg with other two thermos---this one displays two rapid "-" minus marks and then anounces it is 80deg (neg). My freezer is not THAT good. The data sheet says the range is –55°C to +125°C (–67°F to +257°F). I am using 3 AA batteries with the 4.7k in place (I do not have it wired for paracitic power). Datasheet says it can be powered from data line. Power supply range is 3.0V to 5.5V . Any thoughts here would be appreciated.
I just thought about the code--I used b0= b0*9/5+32. Could the difference in resolution from that to -- b0=b0*18/10+32 or even b0=b0*1800/1000+32 make a difference on the negative side? I am going to try changing resolution first. I believe I will wire for paracitic-powered to see if there is any difference if this does not work.
(Kind of thinking out loud here, and that usually gets me in trouble!) Thanks again, JP

By the way, it has probably been covered several times here, but I thought I would mention that the Rev-Ed 08 Proto board programs the 08, 08M, 14M, and the 20M as they all use the same pins. Makes it nice and easy when using all of them.
 

krypton_john

Senior Member
Thank you all. I thought it must be something simple that I was/wasnot doing. (I only have three brain cells now,and the middle one sputters alot). I will implement this and get it working, or move to a country that uses Celcius, I guess. Thanks again, JP
LOL! I have at least 10 neurons, but they aren't connected!
 

jpyle1

New Member
Negative is not minus

Still do not know what is going on, but going below 32deg it flashes 2 "-" and seems to track in a linear fashon with -10"f" being about "--80" and 0"f" around "--70" or so, 10= is about "--60". At 31+ it starts right back up the scale. It must have to do with the coding for the transition from pos to neg. I will try a couple of things for a bit longer. Thanks, JP
 

westaust55

Moderator
If you read PICAXE manual 2 page 128 (see under “information”) you will see that the value received is a signed number.

PICAXE BASIC cannot handle negative numbers but if the most significant bit is set (ie a number greater than 128) then the number has to be adjusted to get the correct negative number.

So received values of 0 to 127 represent the actual temperature.
Then for the negative numbers the manual states that “negative values appear as 128 + numeric value”

That is a bit vague but 128 + (-55) = 73 which would be confused with actual 73 degC, so I take it that they mean the absolute part of the negative values so -55 is represented as 128 + 55 = 183.

so try the lines:
Code:
If b0 > 127 then
bo = bo - 128
endif
 
Last edited:

jpyle1

New Member
Thanks, I will give it a go with your code. I had read the section that you pointed to here, but I have not fully grasped it (or did not remember it). Thanks, Westaus55
 

westaust55

Moderator
A thought,

You will also need some indication whether the value is negative. This part code would be better

Code:
Init:  Symbol sign = b5
temp20m: sign = 0
               readtemp 7,b0 'DS18B20 temp reading at 20M input 7

               if b0 > 127 then
                  b0 = b0 – 128
                  sign = 1
               endif

               b0= b0 * 9 /5 +32 
               b1= b0/10 'divide orig temp to get tens value
               b2= b0//10 'divide orig temp so remainder yields units value
       
              if sign = 1 then output a “-“ first

              if b1=0 then units 'suppress "0" if temps between ±9°C,so "4" & not "04"
 

vk6bgn

New Member
Thank you all. I thought it must be something simple that I was/wasnot doing. (I only have three brain cells now,and the middle one sputters alot). I will implement this and get it working, or move to a country that uses Celcius, I guess. Thanks again, JP
Jpyle,

Try to get your project to convert to Celsius rather then relocating to a country that uses it. It’s much easier!!!

I relocated from Los Angeles to sunny Perth, Western Australia 13 years ago. Wow, what a learning curve. Both speak English, but lots of new slang words and terms to learn! But absolutely great people!

Example: First 12 hours in Australia, watching the weather report on TV. Man on TV says it 38 degrees outside…. seems cold, but dreadfully hot out side!!! Then he goes on to say the outside barometric pressure is 1007 Hectopascals! I thought, what the heck is a Hectopascal? Then weatherman goes onto say the Fremantle Doctor will be in about 4PM. I couldn’t figure why a Doctor is going to be in the TV studio so I asked Aussie wife, she says Fremantle Doctor it’s local slang name for the cool “Sea Breeze”! ((Hum, this is about the time when I figured it’s going to be a fun but long learning adventure))

Example: First day on the new job, new boss asks me how may Bar’s does the pressure gauge read? I thought, a Bar is a place you drink at! Has nothing to do with pressure! (Yep, it’s pressure again!)

Example: First week on the new job…. boss asks me to cut a piece of pipe 1025 millimeters long. Before I even took my tape measure out of my tool bag, I told him that there are no numbers on my tape measure that go that high! I said to him with a smile, I only have a 12 foot tape with numbers that only go up as high as 144 inches. His response was, “go buy a proper tape measure after work”!

Example: Second week on the job. Boss asks me “how’s my tucker”? Hum, I thought I wish I owned a 1948 fully restored Tucker! But really, I couldn’t figure out what he was asking me so I said, “what’s a tucker”? He says” it’s the food I was eating at Smoko! (New Word, Tucker = food) He was asking me how my food tasted. (Also, new word, Smoko = is usually a 10-15 minute morning work break, usually around 9 to 9:30am)

Example: First month in Australia, I was trying to soak up a bit of Australian sport so I started to watch a Cricket game on TV. I realized after three days the teams appeared to be the same two teams. So asked Aussie wife how many games these two teams are going to play? She says it’s the same game?!?!?!? I’s a bit lost by her response so I watched it for another 2 days. And guess what! I took 5 days out of my life to watch this game and nobody won!!! Nobody won!!!

Example: First 3 months in Australia. Started to get over being so frustrated with all the different types of pipe treads, conduit threads, bolt threads etc. etc. UNF, UNC, Metric fine, Metric course, BSP, BSPT, British Standard Wentworth etc. etc. Oh I hate bolts and bolt threads!!! There seems to be every imaginable bolt type and thread here in Oz! Don’t you just hate when you’ve just climbed 6 flights of stairs and you realize the hand full of 12 mm Metric nuts are not going to fit the hand full of ½ inch UNC bolts cause your work colleagues put a hand full of ½ bolts in the 12mm bolt bin! Don’t ya just hate that! And then I realize I’m in Oz now, so I only carry a Metric set of “Spanners” in my tool bag! Also, 6 flights up, none of my Metric spanners properly fit the UNC bolts I never wanted!!!

(New word, Spanners = wrenches!) So, now a days, I find it much easier to just carry two 12 inch Cresent™ wrenches! Oh, they are not Cresent wrenches anymore!!! They are known as “300mm shifters”!!! But isn’t a shifter the part that protrudes out of the top of an automobile transmission and into the vehicle somewhere between the two front seats???? (New word, Shifter = Cresent Wrench)

The clincher!

Example: Second day on the job. Work colleague says to me, “after work lets go to the pub and drink some piss”. Mam-o-man, that sounded so horribly foul! But in reality, my work colleague was just asking me politely to go have a beer with him.

And this is only the beginning! :)
 
Last edited:

manuka

Senior Member
jpyle: It's all my fault for neglecting a °F of course, BUT you should indeed take this as a metrication incentive! Aren't the US,Liberia & Burma now the only 3 non metric countries? Glad it's behaving for you otherwise, & thanks to the brains trust for the imperial workarounds. Trivial fact of the day is that "40 below" is the same on both scales. (i.e -40°C = -40°F ).

Interestingly metric UK still has a real fruit salad of metric & imperial units, with ye olde mile still going strong on signs & speedos etc. Fuel however is sold in litres - no doubt to make the unit cost seem cheaper...

Stan in near totally metricated NZ.
 
Last edited:

westaust55

Moderator
Speaking of -40 degrees (either scale will do :) ) I am currently working on a study project for a plant in deepest coldest Siberia where such temperatures a reached. Brrrrrr :eek:
 

moxhamj

New Member
Another trivial fact - the 1/2 inch imperial BSP pipe thread used in the UK/Oz is the same as the 1/2 inch US NPT. But none of the other threads fit. Unless you use one of those 12 inch shifters!
 

moxhamj

New Member
Rural property. Need to understand irrigation and pumps etc. Vascular surgery and plumbing use very similar skills. General practice and car repairs are rather similar too - people come in with a set of symptoms and you have to work out what is wrong. HamRadioAddict - I don't understand hectopascals and Bars are a bit rusty. But if you gave me the figures in millimetres of mercury, then I would understand! Don't laugh, coz all my picaxe pressure sensor projects do read out in mmHg.
 
Last edited:

jpyle1

New Member
After all the re-coding, it still does the same thing. It displays 2 "-" in rapid (quarter second?) order and then tell me my -10deg F. freezer is at 79 to 80.
I may have to find a hobby that I can FULLY grasp! And measurement is not the only thing that needs changing in the US. Thanks to all--JP
 

Dippy

Moderator
What's the hole pitch of your NZ (made in China) breadboard Stan?

Yes, UK still in miles. A mile... mille.
1000 paces at marching speed for a Roman Soldier wasn't it? (can't be bothered to Google).
Can't get more metric than that eh?

But on June 1st (or as I prefer to say , the 1st of June) the UK is switching from driving on the left over to driving on the right.
 

westaust55

Moderator
Please post your complete code so we can look for any problems therein.

Also add a DEBUG command after the line b2 = . . . .
This will bring up a pop-up screen on your PC and you can watch the displayed values.
Let us know what values you see in the debug window for sign (or whatever you called it), b0, b1 and b2.

Maybe even change the readtemp command to use say variable b5
then calculate a value into variable b0 depending on b5. something like:

Code:
if b5 < 128 then 
 b0 = b5
sign = 0
else
b0 = b5-128
sign = 1
endif
 
Last edited:

moxhamj

New Member
Re But on June 1st (or as I prefer to say , the 1st of June) the UK is switching from driving on the left over to driving on the right. - ...which is expected to cause confusion so it is being phased in over a month.

Jpyle - could you maybe put some debugs in the program - eg right after you do the sensing and give us the raw figures. Then we can work out the math (maths). (posted same time as westaust55)
 

manuka

Senior Member
UK to the left! April Fool Dippy. Reminds me of a near collision I almost had in Norway once when forgotting which country I was in.
 

marcos.placona

Senior Member
Ohh god, I was really scared when I read this sentence

"But on June 1st (or as I prefer to say , the 1st of June) the UK is switching from driving on the left over to driving on the right."

It took me sometime to find the "just kidding" message :D, as I googled all over the place before I actually scrolled down the thread

UPDATE: A good and very interesting explanation about why a quarter of the countries still drive on the left:

http://www.2pass.co.uk/goodluck.htm
 
Last edited:

Dippy

Moderator
If you were a motorcyclist you just drive in the middle.
If you are a cyclist you just drive where it annoys most people, left, right, middle, pavement (sidewalk), shopping mall and over my foot.
 

eclectic

Moderator
My few braincells are coated with Coryza at the moment.
(British Summer -Ha!)

This code is hideously UN-optimised, but it sort of works.

Basically, from 0'C to -18'C, I've counted Upwards from 0'F
(Pedantically, should be - 17.7777 'C)
From - 19'C to -40'C, I've counted downwards.
(I think?)

e.
Code:
'rough C - F converter
symbol celsius = b12
symbol negflag = b13
for b0 = 126 to 178

w5 = b0
if w5 >127 then
w5 = w5 - 128
sertxd ("-")
negflag = 0
endif

celsius = w5


if negflag = 0 and celsius <=18 then
	w5 = w5 * 180 /100 
	w5 = 32 - w5
endif

if negflag = 0 and celsius >18 then
	w5 = w5 -18
	w5 = w5 * 180 /100 
endif

b1 = w5/10
b2 = w5//10

If negflag = 0 and celsius <=18 then
sertxd (#celsius," 'C    ","plus   ", #b1," ",#b2," 'F",cr,lf)
else sertxd (#celsius," 'C    ","neg  ",#b1," ",#b2," 'F",cr,lf)
endif
next
 
Last edited:

kevrus

New Member
The way that I understand it, it's HGV vehicles that are to drive on the right at first (as many of them are from Europe which already drive on that side...) as well as learners, so they can be tought to drive on the right, all others will still drive on the left!
 

westaust55

Moderator
eclectic Celcius to Fahrenheit

jpyle,

I believe eclectic has, without pointing it out how, solved part of the problems. By way of explanation,

the formula first suggested was along the lines b0 = degC *9 / 5 + 32

BUT . . . .

b0 is a byte variable (max value = 255) and by the time degC = 29 degrees degc * 9 is greater than 255 so we have overflow and errors. The problem is even worse if using the 18/10 or 180/100 values as overflow occurs at even lower temps.

So eclectic has correctly used a word variable (max value = 65535) w5. So no overflow error and good accuracy, within the limits of PICAXE BASIC.

Again, lets see your program if you are still having problems and someone can help solve the bugs more easily.
 

vk6bgn

New Member
….. and then there were the crazy things like getting off a one way flight from North America to Australia and suddenly every tooth in my mouth with a metal filling in it seemed to ache for about two weeks. Possibly the Earths magnetic field or something? No aspirin Dr. Acula, the pain is gone now.

Then I couldn’t stand to watch much TV for months and months cause the TV screen seemed to flicker so bad! Especially when there was a lot of the color white on the screen. Possibly the difference between 30 frames per second NTSC format and 25 Frames per second PAL format???

And then there was driving on the other side of the road! And what crazy Civil Engineer ever invented the Roundabout!!!!

Lesson learned only a couple years ago. Why does a ½ inch UNC bolt have 13 threads per inch and a ½ inch BSW bolt have 12 threads per inch! Why!

And then there's the day my wife comes home with a new bathroom scale that has a calibrated read out in "Stones"!!! What measurement is Stones!!! Why is life so difficult! :(

I could write a book on it but enough is enough. Back to my own PICAXE project.

“The Addict”
 

marcos.placona

Senior Member
We also use stones in UK. I find weight measures such as 163 lb. very funny. Sounds like you can't have an idea of what it really is.

Talking about NTSC x PAL. You can't forget about the differences between PAL I, PAL M, PAL S etc...

For example when I moved from UK to Belgium I happily brought my Argos TV, which has the PAL-M standard. I've always seen PAL-M, but never really bothered about what could that mean.

Here I discovered people were using PAL-S. Which means I have image and sound and no colour (back to the 60's)

I then remembered about something related to VCR's I heard a while ago.

Apparently the VCR's just use your TV as a monitor, but all the encoding stuff is done by the VCR, so it'd be just a matter of having a VCR bought in the country you are (i.e using their standard) and you should be fine.

Off I went to try to find a VCR at the local cash converters, as no store in town would have a VCR to sell.

Indeed it worked, but I had to switch the channels through the VCR, and increase/decrease the volume via TV rc, till I finally bought a TV (now a LCD one) that can decode any PAL standard.

I'd start to have problems again if I went to the States (NTSC), or moved to France (SECAM (wtf?))

So here's my advice for those moving to other countries. Always buy a VCR if you find one :D
 

eclectic

Moderator
Trying to think, through a haze of Cold germs.

Almost as an aside now,
referring to post #30

There are THREE formulae at work here.

ABOVE 0'C
F = ((C*9)/5) + 32

-1'C - -18'C
If we call the -C value positive, then
F = 32 - ((C * 9) /5)

BELOW -18'C (actually 17.777)

-F = - (((C+18)*9) * 5)

Secondly, byte and word.
The Picaxe *9/5 STILL works reasonably accurately.
(See slightly amended messy code)

e (and oe)

Awaiting evening, then I can try the Scottish Cold-cure.

Code:
'rough C - F converter
symbol celsius = b12
symbol negflag = b13
for b0 = 126 to 178

w5 = b0
if w5 >127 then
w5 = w5 - 128
sertxd ("-")
negflag = 0
endif

celsius = w5


if negflag = 0 and celsius <=18 then
	w5 = w5 * 9/5 
	w5 = 32 - w5 '*******
endif

if negflag = 0 and celsius >18 then
	w5 = w5 -18
	w5 = w5 * 9/5 '******* 
endif

b1 = w5/10
b2 = w5//10

If negflag = 0 and celsius <=18 then
sertxd (#celsius," 'C    ","plus   ", #b1," ",#b2," 'F",cr,lf)
else sertxd (#celsius," 'C    ","neg  ",#b1," ",#b2," 'F",cr,lf)
endif
next
 

jpyle1

New Member
Westaus55 Sorry about the time lag. I shut down the computer at about 2:30 AM here.
About 2 hrs after my brain shut down, I think. I have some business to attend to, but I will be back with code in a few hours. I Really appreciate yours, and everyones help here.
I had thought there may be an overflow problem, but I had not located it (through hazy eyes) last night
eclectic, I will give your code a go as soon as I get back. Thanks, JP
 

mega

New Member
This link may help those that believe that there are a few countries that are NOT metric...

http://www.unc.edu/~rowlett/units/index.html

Personally, I cannot believe why the U.S. has not embraced Metric, it IS their official system, but no, they persist in the Fred Flintstone system... I hate having to go back and get another sized tool because it is an American piece of equipment, I think the world will breath a sigh of relief when America finally decides to embrace metric.....

Having said that, this is a good thread to give an example of maths with a Picaxe... I'll keep checking this thread......

Cheers
 
Top