Shop OBEX P1 Docs P2 Docs Learn Events
Math! — Parallax Forums

Math!

ArchiverArchiver Posts: 46,084
edited 2004-04-07 17:06 in General Discussion
First off, thanks for everyone's reply on my DIG problem...got that working
well. Now, what I'm trying to do is rather complex (for me!)

I need my bs2p to crunch this:

TEMP = ((2.294 + (255 - ADCVAL) * (1.5) / 255) - 2.294) / (0.01 / 1.8) - 40

TEMP is going to be an output temperature, and it can be an integer value,
I'm not worried about 78.2 or something silly like that. 78 is
fine. ADCVAL is a number from 0-255, coming from an ADC0831. Is there
a better way to do this? I'm using an LM335AZ Precision temp sender with
the ADC...which are both fairly common chips, and I have all the code to
SHIFTIN from the chip....just need to convert the 0-255 to a useable
temperature. I know the above formula will do what I want, because I have
that line in a VB program I wrote a while back that uses both of those
chips....

Thanks!!
Kris Hain

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2004-04-07 01:24
    Hi Kris,

    To get enough accuracy you will have to keep your numbers and the
    intermediate results as high as possible, but not higher than 65,536.
    In
    your specific case you could try the following calculation:

    Temp1=(255-ADCVAL)*150/255 ' times 100 for accuracy, 2.294 equals
    out

    Temp2= 180 ' inversion of the division: 1/(0.01/1.8)

    And then:

    TEMP= Temp1*Temp2/100-40

    In fact this multiplies part of your calculation by 100 and inverts
    the
    division for Temp2. At the end the multplied part is devided by 100
    again.
    It should work this way.

    Regards,

    Klaus




    --- In basicstamps@yahoogroups.com, "Kristoffer M. Hain" <haink@k...>
    wrote:
    > First off, thanks for everyone's reply on my DIG problem...got that
    working
    > well. Now, what I'm trying to do is rather complex (for me!)
    >
    > I need my bs2p to crunch this:
    >
    > TEMP = ((2.294 + (255 - ADCVAL) * (1.5) / 255) - 2.294) / (0.01 /
    1.8) - 40
    >
    > TEMP is going to be an output temperature, and it can be an integer
    value,
    > I'm not worried about 78.2 or something silly like that. 78 is
    > fine. ADCVAL is a number from 0-255, coming from an ADC0831.
    Is there
    > a better way to do this? I'm using an LM335AZ Precision temp
    sender with
    > the ADC...which are both fairly common chips, and I have all the
    code to
    > SHIFTIN from the chip....just need to convert the 0-255 to a
    useable
    > temperature. I know the above formula will do what I want, because
    I have
    > that line in a VB program I wrote a while back that uses both of
    those
    > chips....
    >
    > Thanks!!
    > Kris Hain
  • ArchiverArchiver Posts: 46,084
    edited 2004-04-07 01:48
    --- In basicstamps@yahoogroups.com, "Kristoffer M. Hain" <haink@k...>
    wrote:
    > TEMP = ((2.294 + (255 - ADCVAL) * (1.5) / 255) - 2.294) / (0.01 /
    1.8) - 40

    This formula looks odd to me. Did you intend to have the 1.5
    parenthesized or did you intend something different?

    Secondly, unless I'm missing something the formula looks like:

    ((2.294 + X) - 2.294) / Y - 40

    What's the point of adding and then subtracting 2.294?

    Lastly, it might make sense for you to do the operations with a scale
    factor of 100 in order to avoid loosing precision with integer
    arithmetic. Also, you need to pay close attention to the order of
    the operations. Try to arrange things to force the multiplication
    operations to be done before the division operations. However, you
    also need to make sure that you don't overflow the maximum positive
    value of an integer with a full scale input value. If this becomes a
    problem, you can intersperse some division operations to avoid
    overflow. Additionally, if performance is a consideration, you
    should combine some of the operations. For example, X / (0.01 / 1.8)
    can be replaced with X * 180. Look for similar simplifications.
  • ArchiverArchiver Posts: 46,084
    edited 2004-04-07 02:38
    You could make it easy on yourself by first reducing your function.

    Mike


    --- In basicstamps@yahoogroups.com, "Kristoffer M. Hain" <haink@k...>
    wrote:
    > First off, thanks for everyone's reply on my DIG problem...got that
    working
    > well. Now, what I'm trying to do is rather complex (for me!)
    >
    > I need my bs2p to crunch this:
    >
    > TEMP = ((2.294 + (255 - ADCVAL) * (1.5) / 255) - 2.294) / (0.01 /
    1.8) - 40
    >
    > TEMP is going to be an output temperature, and it can be an integer
    value,
    > I'm not worried about 78.2 or something silly like that. 78 is
    > fine. ADCVAL is a number from 0-255, coming from an ADC0831.
    Is there
    > a better way to do this? I'm using an LM335AZ Precision temp
    sender with
    > the ADC...which are both fairly common chips, and I have all the
    code to
    > SHIFTIN from the chip....just need to convert the 0-255 to a
    useable
    > temperature. I know the above formula will do what I want, because
    I have
    > that line in a VB program I wrote a while back that uses both of
    those
    > chips....
    >
    > Thanks!!
    > Kris Hain
  • ArchiverArchiver Posts: 46,084
    edited 2004-04-07 02:46
    Hi Kris,

    Start by simplifying the expression to the form, Y = S * X + I.

    Is the formula you wrote done with standard precedence, or with the
    left to right precedence of the Stamp?


    For example, looking briefly at the formula with standard precedence,
    the first 2.294 would cancel out with the second one. With standard
    precedence the output range is TEMP = {-40 to +230} for an input of
    ADCVAL={255 to 0}. Is that right?

    -- Tracy




    >First off, thanks for everyone's reply on my DIG problem...got that working
    >well. Now, what I'm trying to do is rather complex (for me!)
    >
    >I need my bs2p to crunch this:
    >
    >TEMP = ((2.294 + (255 - ADCVAL) * (1.5) / 255) - 2.294) / (0.01 / 1.8) - 40
    >
    >TEMP is going to be an output temperature, and it can be an integer value,
    >I'm not worried about 78.2 or something silly like that. 78 is
    >fine. ADCVAL is a number from 0-255, coming from an ADC0831. Is there
    >a better way to do this? I'm using an LM335AZ Precision temp sender with
    >the ADC...which are both fairly common chips, and I have all the code to
    >SHIFTIN from the chip....just need to convert the 0-255 to a useable
    >temperature. I know the above formula will do what I want, because I have
    >that line in a VB program I wrote a while back that uses both of those
    >chips....
    >
    >Thanks!!
    >Kris Hain
  • ArchiverArchiver Posts: 46,084
    edited 2004-04-07 11:20
    Klaus -

    I think this is what I need! I'll try it tonight!

    Thank you!!!


    >Hi Kris,
    >
    >To get enough accuracy you will have to keep your numbers and the
    >intermediate results as high as possible, but not higher than 65,536.
    >In
    >your specific case you could try the following calculation:
    >
    >Temp1=(255-ADCVAL)*150/255 ' times 100 for accuracy, 2.294 equals
    >out
    >
    >Temp2= 180 ' inversion of the division: 1/(0.01/1.8)
    >
    >And then:
    >
    >TEMP= Temp1*Temp2/100-40
    >
    >In fact this multiplies part of your calculation by 100 and inverts
    >the
    >division for Temp2. At the end the multplied part is devided by 100
    >again.
    >It should work this way.
    >
    >Regards,
    >
    >Klaus
    >
    >
    >
    >
    >--- In basicstamps@yahoogroups.com, "Kristoffer M. Hain" <haink@k...>
    >wrote:
    > > First off, thanks for everyone's reply on my DIG problem...got that
    >working
    > > well. Now, what I'm trying to do is rather complex (for me!)
    > >
    > > I need my bs2p to crunch this:
    > >
    > > TEMP = ((2.294 + (255 - ADCVAL) * (1.5) / 255) - 2.294) / (0.01 /
    >1.8) - 40
    > >
    > > TEMP is going to be an output temperature, and it can be an integer
    >value,
    > > I'm not worried about 78.2 or something silly like that. 78 is
    > > fine. ADCVAL is a number from 0-255, coming from an ADC0831.
    >Is there
    > > a better way to do this? I'm using an LM335AZ Precision temp
    >sender with
    > > the ADC...which are both fairly common chips, and I have all the
    >code to
    > > SHIFTIN from the chip....just need to convert the 0-255 to a
    >useable
    > > temperature. I know the above formula will do what I want, because
    >I have
    > > that line in a VB program I wrote a while back that uses both of
    >those
    > > chips....
    > >
    > > Thanks!!
    > > Kris Hain
    >
    >
    >
    >To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    >from the same email address that you subscribed. Text in the Subject and
    >Body of the message will be ignored.
    >
    >Yahoo! Groups Links
    >
    >
    >
    >
  • ArchiverArchiver Posts: 46,084
    edited 2004-04-07 11:20
    Tracy -

    You're right on the below. The original formula was cobbled
    together from 2 different things....and I should have simplified it years
    ago, but if it's not broke...

    Thanks for your help!


    >Hi Kris,
    >
    >Start by simplifying the expression to the form, Y = S * X + I.
    >
    >Is the formula you wrote done with standard precedence, or with the
    >left to right precedence of the Stamp?
    >
    >
    >For example, looking briefly at the formula with standard precedence,
    >the first 2.294 would cancel out with the second one. With standard
    >precedence the output range is TEMP = {-40 to +230} for an input of
    >ADCVAL={255 to 0}. Is that right?
    >
    > -- Tracy
    >
    >
    >
    >
    > >First off, thanks for everyone's reply on my DIG problem...got that working
    > >well. Now, what I'm trying to do is rather complex (for me!)
    > >
    > >I need my bs2p to crunch this:
    > >
    > >TEMP = ((2.294 + (255 - ADCVAL) * (1.5) / 255) - 2.294) / (0.01 / 1.8) - 40
    > >
    > >TEMP is going to be an output temperature, and it can be an integer value,
    > >I'm not worried about 78.2 or something silly like that. 78 is
    > >fine. ADCVAL is a number from 0-255, coming from an ADC0831. Is there
    > >a better way to do this? I'm using an LM335AZ Precision temp sender with
    > >the ADC...which are both fairly common chips, and I have all the code to
    > >SHIFTIN from the chip....just need to convert the 0-255 to a useable
    > >temperature. I know the above formula will do what I want, because I have
    > >that line in a VB program I wrote a while back that uses both of those
    > >chips....
    > >
    > >Thanks!!
    > >Kris Hain
    >
    >
    >To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    >from the same email address that you subscribed. Text in the Subject and
    >Body of the message will be ignored.
    >
    >Yahoo! Groups Links
    >
    >
    >
    >
  • ArchiverArchiver Posts: 46,084
    edited 2004-04-07 17:06
    > I need my bs2p to crunch this:
    >
    > TEMP = ((2.294 + (255 - ADCVAL) * (1.5) / 255) - 2.294) / (0.01 / 1.8) - 40
    >
    > TEMP is going to be an output temperature, and it can be an integer value,
    > I'm not worried about 78.2 or something silly like that. 78 is
    > fine. ADCVAL is a number from 0-255, coming from an ADC0831. Is there
    > a better way to do this? I'm using an LM335AZ Precision temp sender with
    > the ADC...which are both fairly common chips, and I have all the code to
    > SHIFTIN from the chip....just need to convert the 0-255 to a useable
    > temperature. I know the above formula will do what I want, because I have
    > that line in a VB program I wrote a while back that uses both of those
    > chips....

    IIRC multiplication & division happens before addition & subtractions so:

    TEMP = ((((255 - ADCVAL) * 1.5 / 255) + (2.294 - 2.294)) / (0.01 / 1.8)) -
    40

    2.294 - 2.294 == 0, 0.01 / 1.8 == 1/180.0 & 1.5 / 255 == 1 / 170 therefore:

    TEMP = ((((255 - ADCVAL) / 170)) / (1 / 180)) - 40

    dividing by 1/180 is the same a multiplying by 180:

    TEMP = ((255 - ADCVAL) / 170 * 180) - 40

    And 180 / 170 == 18/17 therefore:

    TEMP = ((255 - ADCVAL) * 18 / 17) - 40

    Checking both formulas with ADCVAL == 0 & == 255:

    Original formula with ADCVAL == 0:
    TEMP = ((2.294 + (255 - 0) * 1.5 / 255) - 2.294) / (0.01 / 1.8) - 40
    TEMP = ((2.294 + (255) * 1.5 / 255) - 2.294) / (0.005555) - 40
    TEMP = ((2.294 + 382.5 / 255) - 2.294) / 0.005555 - 40
    TEMP = ((2.294 + 1.5) - 2.294) / 0.005555 - 40
    TEMP = (3.794 - 2.294) / 0.005555 - 40
    TEMP = 1.5 / 0.005555 - 40
    TEMP = 270 - 40
    TEMP = 230

    Reduced formula with ADCVAL == 0:
    TEMP = ((255 - 0) * 18 / 17) - 40
    TEMP = (255 * 18 / 17) - 40
    TEMP = (4590 / 17) - 40
    TEMP = 270 - 40
    TEMP = 230

    Original formula with ADCVAL == 255:
    TEMP = ((2.294 + (255 - 255) * (1.5) / 255) - 2.294) / (0.01 / 1.8) - 40
    TEMP = ((2.294 + (0) * 1.5 / 255) - 2.294) / (0.005555) - 40
    TEMP = ((2.294 + (0) - 2.294) / (0.005555) - 40
    TEMP = (0) / (0.005555) - 40
    TEMP = 0 - 40
    TEMP = -40

    Reduced formula with ADCVAL == 255:
    TEMP = ((255 - 255) * 18 / 17) - 40
    TEMP = (0 * 18 / 17) - 40
    TEMP = (0) - 40
    TEMP = -40

    --
    Enjoy,
    George Warner,
    Schizophrenic Optimization Scientists
    Apple Developer Technical Support (DTS)
Sign In or Register to comment.