Implementing a linear function...


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2007
    Location
    Brazil
    Posts
    108

    Default Implementing a linear function...

    Hi everyone...

    I'm working on a temperature sensor and here is a table between signal versus temperature °C: (almost linear)

    TEMP VOLTS
    0______3.45
    10_____2.84
    20_____2.3
    30_____1.81
    40_____1.36
    49_____1.02

    I would like to show the temperature in a lcd display (from 0 to 49 by unit). What is 'the best' way to implement that?

    Thanks in advance..

    Sylvio
    Last edited by sirvo; - 25th July 2007 at 04:32.

  2. #2
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Your data is not "almost linear".

    Actually, there is no relation among the values at all.

    You may want to approximate the values and use Select Case.


    What is the sensor you are using?

    -----------------------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    "Almost" is a relative term, and I guess it depends on how much "Almost" your application can tolerate.

    From this excel graph you can see there's quite a bit of "curve" to it. But it may not be that bad.

    <img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1880&stc=1&d=118535245 1">

    Converting the formula excel came up with to this...
    Code:
    TempC  VAR WORD
    ADval  VAR WORD
    
    TempC  = ADval * 396
    TempC  = DIV32 1000
    TempC  = 67 - TempC  
    
    LCDOUT "Temp C = ", SDEC TempC,"  "
    You should get these results
    Code:
           8-bit
    Volts	A/D	Temp	PIC reads
    -----	---	-----	---------
    3.45	175	0	-2
    2.84	144	10	10
    2.3	117	20	21
    1.81	92	30	31
    1.36	69	40	40
    1.02	52	49	47
    It's off by 2 degrees at the top and bottom of the scale, 1 degree in the middle, and is very close in the areas of 10 and 40 deg.

    If that's not within the "Almost" for your program, then Sayzer's idea of a Stepped Linear Interpolation using Select or Lookup/down, will get you closer.

    HTH,
    Attached Images Attached Images  
    DT

  4. #4
    Join Date
    Jan 2007
    Location
    Brazil
    Posts
    108


    Did you find this post helpful? Yes | No

    Smile

    Quote Originally Posted by Darrel Taylor View Post
    "Almost" is a relative term, and I guess it depends on how much "Almost" your application can tolerate.

    From this excel graph you can see there's quite a bit of "curve" to it. But it may not be that bad.

    Converting the formula excel came up with to this...
    Code:
    TempC  VAR WORD
    ADval  VAR WORD
    
    TempC  = ADval * 396
    TempC  = DIV32 1000
    TempC  = 67 - TempC  
    
    LCDOUT "Temp C = ", SDEC TempC,"  "
    You should get these results
    Code:
           8-bit
    Volts	A/D	Temp	PIC reads
    -----	---	-----	---------
    3.45	175	0	-2
    2.84	144	10	10
    2.3	117	20	21
    1.81	92	30	31
    1.36	69	40	40
    1.02	52	49	47
    It's off by 2 degrees at the top and bottom of the scale, 1 degree in the middle, and is very close in the areas of 10 and 40 deg.

    If that's not within the "Almost" for your program, then Sayzer's idea of a Stepped Linear Interpolation using Select or Lookup/down, will get you closer.

    HTH,
    It is not necessary in my application a 'perfect' result OF temperature. As the excel show, it is ''almost'' linear and it is a good solution. Maybe it will be necessary to get a 'perfect result' measuring another signal in this application and I think that this signal is not 'almost' linear. Then I will try the lookup/down....Thanks Sayzer by helping.

    Darrel, thanks for the attention and solution. The result is very satisfatory and I will use it.

    Now, I got another doubt. What would be the difference if you change the TempC = DIV32 1000 for this: TempC = TempC / 1000 ?

    Thank you very much..!

    Regards

    Sylvio

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    What would be the difference if you change the TempC = DIV32 1000 for this: TempC = TempC / 1000 ?
    At the low end of the temperature scale the A/D value can be up to 175 (0 deg.)
    If you multiply that times 396, the result is 69,300. That's over 65,535, so the result of the normal divide would give an incorrect answer.
    It has to be DIV32.
    <br>
    DT

Similar Threads

  1. Single button function
    By DynamoBen in forum mel PIC BASIC Pro
    Replies: 40
    Last Post: - 4th April 2020, 18:33
  2. ARCSIN function
    By mjphillips1981 in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 5th March 2009, 20:19
  3. Hash function
    By Ioannis in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 7th September 2007, 10:09
  4. ATAN2(,) function
    By vk2tds in forum PBP Wish List
    Replies: 0
    Last Post: - 25th November 2005, 02:52
  5. Random function - How to limit it ??
    By martarse in forum mel PIC BASIC Pro
    Replies: 18
    Last Post: - 30th November 2004, 14:05

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts