Shop OBEX P1 Docs P2 Docs Learn Events
newbie needs help with temperature sensor data calculations with bs2 — Parallax Forums

newbie needs help with temperature sensor data calculations with bs2

fsbfsb Posts: 24
edited 2009-10-13 22:07 in BASIC Stamp
I have built a device to measure the difference between sky temperature (using the mlx90614 IR thermometer) and ambient (ground) temperature (using the DS1620 thermometer chip).· This value (DeltaT) correlates with clear or cloudy skies, and enables automatic control of my observatory for astronomical imaging.· (The other components in the attached program are for detection of ambient light and wireless transmission of the data to a base computer).·

Everything works great when the ambient (ground) temperature is above 0 degrees Celsius.· However, when the
ambient temperature falls below 0 degrees celsius (ie, becomes negative), I get a nonsensical value for DeltaT.
I suspect this is related somehow to how the BS2 performs mathematical operations with negative numbers, and likely is related to the tC value obtained from the DS1620, but I cannot, despite trying, figure out a way to correct this.

I have attached a copy of the BS2 program.· I though it would be easy to calculate DeltaT as difference in degrees Kelvin since IR thermometer data output is degrees Kelvin, and converted tC from DS1620 to Kelvin (or so I thought)

Here are example values:

Delta T············· sky temp··············· ambient temp········ (temps in degree celsius)
-35.5················· -33························ 2
-35.5·················· -35························0
-6587.7··············· -35······················· -1
-6586.6·················-35························-2

Again, I am a complete newbie and I am sure the program has lots of flaws. I basically cobbled it together from
the various parallax sample programs.

Any help would be appreciated!

Thanks
Frank

Comments

  • FranklinFranklin Posts: 4,747
    edited 2009-10-12 01:04
    Check your bytes and words make sure you are not overflowing. I didn't take a look at the code but that is what the numbers seem to point to.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • Carl HayesCarl Hayes Posts: 841
    edited 2009-10-12 07:43
    I note that you call (that is, GOSUB) convertTemperatures and then you call delta. Since there is no RETURN in convertTemperatures, it will have executed the code in delta anyway; don't need to do it twice. Have you got a missing RETURN?

    But that's not the problem. It looks to me as if the actual problem is may be that you are using DEBUG to display signed data, but your DEBUG statements assume unsigned data. That's OK if the data are really unsigned, but when they're actually signed it will give wacky displays of good data that happen to be negative.

    Alternatively it may be that you're doing unsigned arithmetic with signed numbers (treating the sign bit as a binary digit whose value is 32768). Since you do a lot of multiplying and dividing in order to use part of each datum as a "tenths" value, that sign-bit-used-as-value can honk up the arithmetic too, only when a number is negative and you're using it in arithmetic.

    I would add 273 (or 2730, or 27 -- your code isn't easy to read) to make sure every number is always positive except DELTA. That ought to cure it.

    The code would be easier to understand and debug if you used names like SkyTempK, AirTempC, DeltaTempTenths, and so forth. I never achieved certainty about which temp was which, and what represented tenths of degrees, etc.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i@arrl.net
  • Carl HayesCarl Hayes Posts: 841
    edited 2009-10-12 13:51
    Considered it a bit more.· I'm now fairly certain the problem is that you're dividing a signed integer by 10.

    Suppose the air temperature is a positive number, say 30, representing 3.0 degrees.· You divide it by 10 as part of the process of displaying it.· You get 3 as the integer part, and 0 as the tenths, and all is well.

    Now suppose the air temperature is a negative number, say -20, representing -2.0 degrees.· As a 16-bit signed number, that is 1111 1111 1111 1110, which as an unsigned 16-bit number represents 65534.

    So you take that 16-bit number and divide it by 10 to separate the integer part and the tenths, and you get 6553 as the quotient and 4 as remainder, which is 6553.4 degrees.· If the sky temperature is -32.2, the difference is 6586.6 degrees.

    In sum, you can't divide negative numbers on the Stamp.· For a workaround, see page 113 of the Stamp manual.




    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i@arrl.net
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-10-13 03:33
    In the convertTemperatures subroutine...
    change,
       [s]DeltaT = Kelvin - (tC/10 + 273)   ' Stamp can't divide negative tC by 10[/s]
    to 
      DeltaT = Kelvin - (tK/10)    ' you had already computed tK, a positive number, can be divided by 10
    




    Also, in the display, Delta is an integer, so no need to display tenths, much simpler:
    change
       [s](DeltaT.BIT15 * 13 + " "), DEC(ABS DeltaT),  ".",  DEC1(ABS DeltaT)[/s]
    to
       SDEC DeltaT     ' debug display signed integer
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • fsbfsb Posts: 24
    edited 2009-10-13 22:07
    Thank you everyone for your advice.· Hopefully I will have time this weekend to test it out.
    I will follow up once I have done that.

    Again Thanks for your time
    fsb
Sign In or Register to comment.