Temperature Coversion Module

Coding and general discussion relating to user created compiler modules

Moderators: David Barker, Jerry Messina

Post Reply
User avatar
Darrel Taylor
Posts: 29
Joined: Wed Oct 04, 2006 4:44 pm
Location: California

Temperature Coversion Module

Post by Darrel Taylor » Thu Oct 05, 2006 8:49 pm

I am loving this compiler. David, if you weren't a guy, I'd kiss you. :oops:

Here's my first attempt at a Module. It's just a port of one of my previous PBP includes, but I did it in a couple hours, instead of a couple days like the PBP version took me. And it works much better too.

Code: Select all

{
****************************************************************
*  Name    : Temp.bas                                          *
*  Author  : Darrel Taylor                                     *
*  Date    : 10/5/2006                                         *
*  Version : 1.0                                               *
*  Notes   : Temperature Conversion Module for Swordfish       *
*            Converts between Fahrenheit, Celsius and Kelvin   *
*          : You can use either Integers or Floats             *
*            Integers are multiplied by 10 for 1 decimal place *
*            25.4 would be stored as 254, -100.0 is -1000      *
*          : Floats are obvious, 25.42 = 25.42                 * 
****************************************************************
* Functions: CtoF - Celsius to Fahrenheit                      *
*            FtoC - Fahrenheit to Celsius                      *
*            CtoK - Celsius to Kelvin                          *
*            KtoC - Kelvin to Celsius                          *
*            FtoK - Fahrenheit to Kelvin                       *
*            KtoF - Kelvin to Fahrenheit                       *
****************************************************************
}
Module Temp

//------[ Celsius to Fahrenheit ]-------------------------------
Public Function CtoF (C As Integer) As Integer
  Dim TempLI As LongInt
    TempLI = C * 18 / 10 + 320      
    Result = Integer(TempLI)
End Function
//------       
Public Function CtoF (C As Float) As Float       
    Result = C * 1.8 + 32
End Function

//------[ Fahrenheit to Celsius ]-------------------------------
Public Function FtoC (F As Integer) As Integer       
  Dim TempLI As LongInt
    TempLI =  (F - 320) * 10 / 18
    Result =  Integer(TempLI)
End Function
//------       
Public Function FtoC (F As Float) As Float
    Result =  (F - 32) / 1.8
End Function

//------[ Celsius to Kelvin ]-----------------------------------
Public Function CtoK (C As Integer) As Integer       
    Result = C + 2730
End Function
//------       
Public Function CtoK (C As Float) As Float       
    Result = C + 273.0
End Function

//------[ Kelvin to Celsius ]-----------------------------------
Public Function KtoC (K As Integer) As Integer       
    Result = K - 2730
End Function
//------       
Public Function KtoC (K As Float) As Float       
    Result = K - 273.0
End Function

//------[ Fahrenheit to Kelvin ]--------------------------------
Public Function FtoK (F As Integer) As Integer       
    Result = CtoK(FtoC(F))
End Function
//------       
Public Function FtoK (F As Float) As Float       
    Result = CtoK(FtoC(F))
End Function

//------[ Kelvin to Fahrenheit ]--------------------------------
Public Function KtoF (K As Integer) As Integer       
    Result = CtoF(KtoC(K))
End Function
//------       
Public Function KtoF (K As Float) As Float       
    Result = CtoF(KtoC(K))
End Function
Then to ues it.

As Integers

Code: Select all

Include "Temp.bas"

Dim  C As Integer
Dim  F As Integer
Dim  K As Integer

  C = 254   // 25.4 deg.
  F = Temp.CtoF(C)
  K = Temp.CtoK(C)
As Floats

Code: Select all

Include "Temp.bas"

Dim  C As Float
Dim  F As Float
Dim  K As Float
  
  C = 25.4   // 25.4 deg.
  F = Temp.CtoF(C)
  K = Temp.CtoK(C)
Best regards,
DT

TimB
Posts: 262
Joined: Wed Oct 04, 2006 7:25 am
Location: London UK

Post by TimB » Thu Oct 05, 2006 9:36 pm

Good stuff Darell

I hope you don't mind I placed it on the Wiki site under modules and took the liberty to add some text and rename the module.

Go to the site and click on edit at the bottom to rename or modify as you feel it requires.

User avatar
Darrel Taylor
Posts: 29
Joined: Wed Oct 04, 2006 4:44 pm
Location: California

Post by Darrel Taylor » Thu Oct 05, 2006 10:03 pm

Yup, probably a good idea to change the name to keep it from conflicting with a "Temp" variable.

Thanks Tim.
Best regards,
DT

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Fri Oct 06, 2006 2:57 pm

A great little module Darrel. Lite and well focussed...

As Tim has already pointed out, please feel free to use the Swordfish Wiki to post code snippets, projects, modules etc.

http://www.sfcompiler.co.uk/wiki

One of the things I have noticed with other forums is that really useful bits of code (like this post) can get lost over time. The idea of the wiki is to try and structure content, so that gems like this are easily available to other users. You don't have to register, it's all open and easy to edit (it is moderated though)

If you don't want to use the wiki - no problem!

Just post code here on the forum and if it's relevant, someone else will post it for you. Use both forum and wiki if you wish - choose whatever you prefer...

The most important thing is that contributions are made. Thanks again.

User avatar
Darrel Taylor
Posts: 29
Joined: Wed Oct 04, 2006 4:44 pm
Location: California

Post by Darrel Taylor » Fri Oct 06, 2006 5:53 pm

Thanks David!

Hopefully that was the first of many to come.

Maybe I'm not looking in the right place, but I can't seem to find a way to add new items to the wiki.

I can edit existing articles, but can't figure out how to create something new.
Best regards,
DT

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Fri Oct 06, 2006 6:12 pm

An overview of editing and creating pages can be found here

http://www.sfcompiler.co.uk/wiki/pmwiki ... ngOverview

But if you want a quick guide, read on...

Go to the WKI sandbox (practice area)

http://www.sfcompiler.co.uk/wiki/pmwiki ... ikiSandbox

then select edit. At the bottom of the edit window, type

[[DTTestPage | My Test Page]]

then save (don't forget to enter the author name)

You will now see 'My Test Page' with a little question mark. Just click on the link to create a new page called 'DTTestPage'. Add some text, then press save.

That's all there is to it, have fun...

User avatar
Darrel Taylor
Posts: 29
Joined: Wed Oct 04, 2006 4:44 pm
Location: California

Post by Darrel Taylor » Fri Oct 06, 2006 6:37 pm

:D Excellent!

As well as learning a new compiler, it looks like I need to learn how to Post things again too.

I agree, everything gets lost in the garbage when you have to post examples in a forum. Wiki seems like a much better idea.

Thanks for the info, time to go play. 8)
Best regards,
DT

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Fri Oct 06, 2006 7:48 pm

I see you have been busy playing with the wiki! I've approved the link to the image so you can take a look...

If you think you will be using the wiki, I can add you to the list of people who have external link approval. Just email me with a preferred UserName and Password.

You will appreciate that because the wiki is 'open', links need to be approved - else it will get spammed to death...

User avatar
mytekcontrols
Posts: 1
Joined: Sat Dec 09, 2006 9:55 pm
Location: California
Contact:

Nice module...

Post by mytekcontrols » Tue Dec 12, 2006 7:35 am

Darrell,
Nice module, I have a new project in the works that could use this. I think I'll also mimic what you've done here for temperature, and do the same for pressure (psi,bar,pascal). Of course that presumes you don't beat me to the punch, something you use to do to me in the past :) .

Michael

User avatar
Darrel Taylor
Posts: 29
Joined: Wed Oct 04, 2006 4:44 pm
Location: California

Post by Darrel Taylor » Tue Dec 12, 2006 12:46 pm

Hi Michael,

That's great. Glad to know you can make some use of it.

And I just happen to have a use for a Pressure units Converter too. But I'll leave that one up to you this time. :)

I'm also going to need a Flow converter (GPM, GPH, GPD, LPM, LPH, LPD, etc).

And of course there are so many other linear conversions, I wonder if it would be better to have a standard "Linear Converter", Y=mX+b kind of thing, with a list of values for the different units.

Or something similar, haven't really thought it through enough to know if it would work????
Best regards,
DT

Post Reply