Shop OBEX P1 Docs P2 Docs Learn Events
HELP PLEASE with PBASIC — Parallax Forums

HELP PLEASE with PBASIC

JoNIKaHJoNIKaH Posts: 12
edited 2008-06-15 19:54 in BASIC Stamp
Hey guys, I need some help with PBASIC.
I posted something a awhile ago about my project and now i stumbled onto something else
I have a SCi-BOX that uses a thermistor to read the temperature.
This is the software part that I use to read the temperature (it came with PBASIC)
'{$STAMP BS2sx}
'{$PBASIC 2.5}
'************************************************
' Show temperature relative value on DEBUG terminal
'
' Connect temperature sensor at SENSOR0
'************************************************

' System I/O and constant , please do not make change
SD  PIN 13  ' Sci-BOX serial communication port
BAUD  CON  240  ' 9600bps constant
CMD  CON  $FE  ' SLCD command constant

' Custom I/O on this application
SLCD  PIN  7  ' Connect SLCD here!

' Variable defined
VALUE  VAR  Word  ' VALUE  as SENSOR0 variable

  PAUSE  1000  ' Delay 1s. for pheripheral initialize
  SEROUT SLCD,BAUD,[noparse][[/noparse]CMD,1]    ' Clear SLCD screen

DO
  LOW  SD : PAUSE 1 : HIGH  SD  ' Break signal
  SEROUT  SD,BAUD,[noparse][[/noparse]0]      ' Request SENSOR0
  SERIN  SD,BAUD,[noparse][[/noparse]VALUE.LOWBYTE,VALUE.HIGHBYTE]  ' Received data 2 byte to 1 word

  DEBUG "Read data = ",DEC4 VALUE,HOME

  SEROUT SLCD,BAUD,[noparse][[/noparse]CMD,2]    ' Cursor home
  SEROUT SLCD,BAUD,[noparse][[/noparse]"Read data = ",DEC4 VALUE]

  PAUSE 100        ' Delay 100ms.
LOOP        ' Do again





The problem is that it shows me a 3 digit number. So i did some experimenting and i found out the coresponding temperatures for the 3 digits number. But i only found them for 30,40,50 ...90 degrees celsius
And after this i came to a formula :
T=4.925*e(0.0031*N)
Where : T= temperature in celsius
e=exp function
N=the number it shows (it own temp reading)

But I have no ideea how to implement this in the pbasic programming. I found out that it has no ideea what "e" or "exp" is (while Turbo pascal knows about it. ANd that it doesn`t work with decimals
And i`m desperate cause it took me hours to get to that and i found out its useless.
If you have any ideea on how i can get it to show a certain reading starting from what i know (its values for 20,30...90) I will thank you untill I die lol :P)
Thanks for reading

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-06-14 06:20
    First of all, Stamps do not have floating point arithmetic. All of the arithmetic is integer on values from -32768 to 32767 or from 0 to 65535. "e" is the Exponential function (see en.wikipedia.org/wiki/Exponential_function for a description).

    The best way to handle this is to use a spreadsheet (like Excel) or some PC programming language (like Turbo Pascal) to compute a table of values for T given some N. Look through the "math" section here: www.emesystems.com for ideas. Towards the end is a worked example for converting a thermistor reading to Celsius.
  • JoNIKaHJoNIKaH Posts: 12
    edited 2008-06-14 09:12
    What I need is to compare a reading with 2 predefined temperatures and give comands to a cooler or a heater.
    The problem is that the only way i can convert a specific reading in the BStamp is with "e". But BStamp doesn`t like "e".
    After comparing I need to display it and then display another message (COOLING or HEATING) and give the commands. Here is how i defined the temperatures. Will test it later to see if it works.
    
    IF     430<VALUE<444 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 20",DEC2 T," gr.C "]
    IF     444<VALUE<457 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 21",DEC2 T," gr.C "]
    IF     457<VALUE<470 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 22",DEC2 T," gr.C "]
    IF     470<VALUE<484 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 23",DEC2 T," gr.C "]
    IF     484<VALUE<498 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 24",DEC2 T," gr.C "]
    IF     498<VALUE<511 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 25",DEC2 T," gr.C "]
    IF     424<VALUE<537 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 26",DEC2 T," gr.C "]
    IF     537<VALUE<551 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 27",DEC2 T," gr.C "]
    IF     551<VALUE<565 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 28",DEC2 T," gr.C "]
    IF     565<VALUE<583 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 29",DEC2 T," gr.C "]
    IF     565<VALUE<583 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 30",DEC2 T," gr.C "]
    IF     583<VALUE<593 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 31",DEC2 T," gr.C "]
    IF     593<VALUE<603 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 32",DEC2 T," gr.C "]
    IF     603<VALUE<613 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 33",DEC2 T," gr.C "]
    IF     613<VALUE<623 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 34",DEC2 T," gr.C "]
    IF     623<VALUE<634 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 35",DEC2 T," gr.C "]
    IF     634<VALUE<644 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 36",DEC2 T," gr.C "]
    IF     644<VALUE<656 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 37",DEC2 T," gr.C "]
    IF     656<VALUE<678 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 38",DEC2 T," gr.C "]
    IF     678<VALUE<690 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 40",DEC2 T," gr.C "]
    IF     690<VALUE<696 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 41",DEC2 T," gr.C "]
    IF     696<VALUE<702 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 42",DEC2 T," gr.C "]
    IF     702<VALUE<708 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 43",DEC2 T," gr.C "]
    IF     708<VALUE<714 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 44",DEC2 T," gr.C "]
    IF     714<VALUE<721 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T =45",DEC2 T," gr.C "]
    IF     721<VALUE<729 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 46",DEC2 T," gr.C "]
    IF     729<VALUE<737 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 47",DEC2 T," gr.C "]
    IF     737<VALUE<746 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 48",DEC2 T," gr.C "]
    IF     746<VALUE<754 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 49",DEC2 T," gr.C "]
    IF     754<VALUE<762 THEN
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 50",DEC2 T," gr.C "]
    
    


    of course i have to insert a loop also and the rest of the program. But its the only way I could define the temperature. VALUE = the reading I get with the thermistor.
    Another problem is that this converting formula differs from thermistor to thermistor and so on, therefor there are different ways to convert it.
    Another way I found is to work on small intervals and eliminate the exponential, using a plain function (sorry my english and math definitions aren`t that good. I`m from Romania ;P ), but again I have to use decimals and Basic Stamp doesn`t like it also.
  • JoNIKaHJoNIKaH Posts: 12
    edited 2008-06-14 09:18
    Another problem
    It only allows 16 if-then statements per loop. Why ? How am I going to do all that in a single loop ? I don`t know how to use subroutines [noparse]:([/noparse]
  • Mike GreenMike Green Posts: 23,101
    edited 2008-06-14 14:07
    The statement "IF A < B < C THEN" doesn't mean what you think it does. It means "IF (A < B) < C THEN" so first the Stamp compares A and B. If A < B then the result is -1. If A >= B then the result is 0. It then compares this result (-1 or 0) to C. I don't think this is what you want. You probably want "IF (B >= A) AND (B <= C) THEN".

    You may have problems with this program fitting in memory. All these IF statements and SEROUT statements take a lot of memory. It's much better to use a table of values that you create with DATA statements, then use a loop and READ statements to look up values in the table.

    Something like:
    table   data  word 430, word 444, word 457, word 470, word 484, word 498, word 511, word 537
              data  word 551, word 565, word 583, word 593, word 603, word 613, word 623, word 634
              data  word 644, word 656, word 678, word 690, word 696, word 702, word 708, word 714
              data  word 721, word 729, word 737, word 746, word 754, word 762
              data  word -1 ' marks end of table
    
       read table, word lower
       i = table + 2
    tryNext:
       read i, word upper
       if upper = -1 then goto notFound
       if (lower <= value) and (value <= upper) then goto foundIt
       lower = upper
       i = i + 2
       goto tryNext
    notFound:
       serout sled, baud, [noparse][[/noparse]"Invalid value"]
       goto doItAgain
    foundIt:
       serout sled, baud, [noparse][[/noparse]"T ", dec3 value, "/ ", dec2 i, " gr.C"]
       goto doItAgain
    
    


    "i" needs to be declared as a byte or word and "lower", "upper", and "value" need to be declared as words.
  • ercoerco Posts: 20,259
    edited 2008-06-14 15:57
    Jonikah:
    As usual, Mike is 110% right.

    I'm just throwing out a simple alternate path, because many times simpler is better, especially since you say you're not proficient in PBasic. You don't need a Sci-Box to read temperature. You can get thermistors dirt cheap at an electronics supplier (I'll send you one free if you can't find one) and let any Stamp (even my beloved BS-1) read it directly. It's just a variable resistor that you can read with the RCTIME command and a capacitor, just like a photocell (plenty of examples in WAM or Robototicsw/BB). You'll get an integer value (0-255 byte or 0-32K word) for the temperature that varies with the temperature. You can calibrate the temp manually and do a curve-fitting equation for the temp range you need IF you need to display them on LCD screen. If you don't really need the LCD screen and you can live with two preset temperatures to switch your heater or cooler on, your work is greatly simplified. Of course, if that's the case, you don't even need a Basic Stamp, you could build a dedicated circuit and go fully analog with a $1 comparator like an LM339. Check out this thread: http://forums.parallax.com/showthread.php?p=690772. Depending on your situation, you might be able to use it.

    A friend of mine has to generate his own power at a remote location near Gorda, CA. He has several surplus massive underground steam turbines that generate power and provide hot water for·his campground. At some point, the fancy (orig $5+) computerized control system (also surplus) that acted as a thermostat to open & close his hot water bypass valve failed. I replaced·the whole control panel·with $10 of Radio Shack parts (lm339 comparator, thermistor, pot & relay) and it has worked flawlessly for 8 years now. Simpler is usually better.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"If you build it, they will come."
  • JoNIKaHJoNIKaH Posts: 12
    edited 2008-06-14 17:33
    Mike , thank you a lot, tomorrow i`ll input everything in the BS editor and let you in on the results.
    Erco : Thanks, also but I have to use a SCi-BOX. THe whole project is around one.
    It works like this :
    1. I define the lowest temperature I want in the room
    2. I define the highest temperature I want in the room
    Both of these are done using a menu . After this I have to get the temperature from the thermistor, convert it, compare it with the low and the high and give the proper comman to the cooler or the heater. And so on ...
    I did calibrate my thermistor. I went to 90degC and then back to the room temp. And i took the values at each 10 deg. I didn`t knew I wouldn`t be able to use exp or even decimals.
    This is what I have done so far without Mike`s addition.
    '{$STAMP BS2sx}
    '{$PBASIC 2.5}
    BAUD  CON  240
    CMD  CON  $FE
    SLCD PIN  7
    SWITCH1 PIN 0
    SWITCH2 PIN 1
    SWITCH3 PIN 2
    
    SD  PIN 13
    Tmin VAR Word
    Tmax VAR Word
    T VAR Word
    VALUE VAR Word
    INCALZIRE VAR Word
    RACIRE  VAR Word
    PAUSE  1000
    SEROUT SLCD,BAUD,[noparse][[/noparse]CMD,1]
    
    
      SEROUT SLCD,BAUD,[noparse][[/noparse]CMD,2]    ' Cursor home
      Tmin=0
      Tmax=Tmin
      SEROUT SLCD,BAUD,[noparse][[/noparse]"Tmin = ",DEC2 Tmin," gr.C          ","                Tmax = ",DEC2 Tmax, " gr.C"]
    
    
    
     'Reglare Tmin si Tmax'
     DO
        IF SWITCH1=1     THEN
        Tmin=Tmin+1
        Tmax=Tmin
        PAUSE 250
        SEROUT SLCD,BAUD,[noparse][[/noparse]CMD,1]
        SEROUT SLCD,BAUD,[noparse][[/noparse]CMD,2]
      SEROUT SLCD,BAUD,[noparse][[/noparse]"Tmin = ",DEC2 Tmin," gr.C          ","                Tmax = ",DEC2 Tmax, " gr.C"]
    
        ELSEIF SWITCH2=1 THEN
        Tmax=Tmax+1
    
        PAUSE 250
        SEROUT SLCD,BAUD,[noparse][[/noparse]CMD,1]
        SEROUT SLCD,BAUD,[noparse][[/noparse]CMD,2]
      SEROUT SLCD,BAUD,[noparse][[/noparse]"Tmin = ",DEC2 Tmin," gr.C          ","                Tmax = ",DEC2 Tmax, " gr.C"]
        ENDIF
        IF (SWITCH3=1) THEN EXIT
      LOOP
    
       PAUSE  1000
      SEROUT SLCD,BAUD,[noparse][[/noparse]CMD,1]
    
    DO
       PAUSE  1000
      SEROUT SLCD,BAUD,[noparse][[/noparse]CMD,1]
    
    
      LOW  SD : PAUSE 1 : HIGH  SD
      SEROUT  SD,BAUD,[noparse][[/noparse]0]
      SERIN  SD,BAUD,[noparse][[/noparse]VALUE.LOWBYTE,VALUE.HIGHBYTE]
    
      DEBUG "Read data = ",DEC4 VALUE,HOME
    
    
    
    IF     (VALUE>500)AND(value<520) THEN
    T=27
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 27",DEC2 T," gr.C "]
    ENDIF
    IF     (VALUE>520)AND(value<540) THEN
    T=28
    SEROUT SLCD,BAUD,[noparse][[/noparse]"T = 28",DEC2 T," gr.C "]
    ENDIF
    
    
    
       IF T<Tmin THEN
       SEROUT SLCD,BAUD,[noparse][[/noparse]CMD,1]
       SEROUT SLCD,BAUD,[noparse][[/noparse]CMD,2]    ' Cursor home
       SEROUT SLCD,BAUD,[noparse][[/noparse]"T = ",DEC2 T," gr.C          ","                RACIRE"]
           ELSE
     IF T>Tmax THEN
          SEROUT SLCD,BAUD,[noparse][[/noparse]CMD,1]
          SEROUT SLCD,BAUD,[noparse][[/noparse]CMD,2]
          SEROUT SLCD,BAUD,[noparse][[/noparse]"T = ",DEC2 T," gr.C          ","                RACIRE"]
      ENDIF
      ENDIF
    
    
    
    
    LOOP
    
    


    the last part was the experimental one with comparing a value and showing something on the display
    I have to use this many spaces " " because again I couldn`t find a way to write something on the second line of my 16x2 LCD.
    THe SCi-BOX manual doesn`t say too many things about it.
  • JoNIKaHJoNIKaH Posts: 12
    edited 2008-06-15 19:54
    What is gotodoitagain ?
Sign In or Register to comment.