same calculation diffrent results

johnlong

Senior Member
Hi folks
I am experiancing some diffrence between the test program and the results of the programed picaxe 28x2
they both use the same calculation adapted from the original that Hippy helped me with.
Both codes read the preloaded eeprom for the Air and leaf temperatues and obtain the correct SVP (saturated vapour pressure) result
as obtained from this site http://cronklab.wikidot.com/calculation-of-vapour-pressure-deficit
expanded by me up to 50'C. Both codes generate correct results for the SVP (+/- 0001 livable)
however the the test code gives correct results for any temperature for the VP (vapour pressure)
the 28x2 is way out both are using the same calculation ?
Using the VPD calculator of Arizona collage of agriculture as a refrance https://cals.arizona.edu/vpdcalc/
Apologies in advance this may come over 2 posts as I am posting both codes and screen shots
notes on the images the fist is the data from running 28x2 and 20x2 via the drf wireless link showing the results of the 2 sensors attached to the 20x2
a htu21d-f for the air tempreture (symbol Dry) and the %RH the leaf tempreture is from an inferred temp sensor (symbol leafM)
code programed to the 28x2. The next 2 show the values from the test program giving the correct results for temp's of 20 and 24 at a RH% of 58
Code:
#picaxe 28X2

     '  0        1      2       3       4       5       6 degC
Data(  6,11 ,  6,57 ,  7,06 ,  7,58 ,  8,13 ,  8,72 ,  9,35 )
       ' 7        8       8      10      11       12     13
  Data( 10,02 , 10,73 , 11,48 , 12,28 , 13,12 , 14,02 , 14,97 )
  '      14       15     16       17     18      19       20
  Data( 15,98 , 17,05 , 18,18 , 19,37 , 20,64 , 21,97 , 23,38 )
  '      21       22    23     24      25       26      27
  Data( 24,86 , 26,43 ,28,09, 29,83 , 31,67 , 33,61 , 35,65  )
  '      28       29     30      31      32      33      34
  Data( 37,79 , 40,05 , 42,42 , 44,92 , 47,54 , 50,29 , 53,18 )
  '      35      36      37      38      39      40     41
  Data( 56,21 , 59,40 , 62,73 , 66,23 , 69,90 , 73,74, 77,76 )
  '      42     43     44     45     46      47      48
  Data( 82,08, 86,49, 91,11, 95,93, 100,98, 106,25, 111,75 )
  data ( 117,50, 123,50)'49,50
symbol TD4=b4 symbol TD3=b3 symbol TD2=b2 symbol TD1=b1 symbol TD0=b0 'reusable variables b0 to b4
'PICAXE VARIABLES FOR I2C CLOCK and DISPLAY changable taken from readings in main prog RUN
symbol themode=b5
symbol sec =b6
symbol hour=b7
symbol mins=b8  'Read write variables to the DS1307 using the DS1307 as the
symbol day=b9   'Timer for the Data Logging and Display
symbol month=b10
symbol year=b11
symbol date=b12
symbol control=$10'RTC control bit
;I2c address for DS1307
symbol tiktok= %11010000

'********************************
'Set point values determned by operator via nextion
symbol photo=b15     'light sensor determin day or night non changeable
symbol Vdiff=w12'b24,b25
symbol Leaf=w13 symbol leafM=b27 symbol leafl=b26 'b27,b26 leaf tempreature and svp result
'Sensor results
symbol air=w14 symbol airM=b29 symbol airL=b28
symbol RH=b36 '%RH
symbol result=w15 'b31,b30 VPD calculation
symbol Dry     = b33  ;air tempreture deg C
symbol SVP= w17 'b35,b34 data location for SVP tempreture values

'***********************
symbol volt=w23 symbol voltM=b47 symbol voltL=b46

'***********************
'Radio communication
'*******************
Symbol Qlf= $FE
symbol RX=A.1
symbol TX=A.2
symbol DRF=T9600
symbol minptr=102' for timing loop
'*******************
#Define ToBin(bcd) bcd / 16 * $FA + bcd
#Define ToBcd(bin) bin / 10 * 6 + bin
'*********************************
Hi2csetup i2cmaster,%11010000, i2cslow, i2cbyte

sec=30:mins=6:hour=15:day=7:date=28:month=8:year=19
pre_launch:

        sec=ToBcd(sec)
        mins = ToBcd(mins)
        hour= ToBcd(hour)
        day = ToBcd(day)
        date = ToBcd(date)
        month = ToBcd(month)
        year = ToBcd(year)
    hi2cout 0,(sec,mins,hour,day,date,month,year,control)
    pause 100
    hi2cin 0,(sec,mins,hour,day,date,month,year)
    sec=toBin(sec):mins=ToBin(mins):hour=ToBin(hour):day=ToBin(day):date=Tobin(date):month=ToBin(month):year=ToBin(year)
    sertxd("date ",#date," month ",#month," year ","20",#year,cr,lf)
 
main:
do
 
    hi2cin 0,(sec,mins,hour)
    sec=toBin(sec):mins=ToBin(mins):hour=ToBin(hour)
    sertxd("hour ",#hour," mins ",#mins," sec ",#sec,cr,lf)
    poke minptr,mins
    high B.7

    serout TX,DRF,(85,85,85,85,Qlf,"Hello")'wake up slave call


do
    gosub TH
    sertxd("b0 on release ",#b0,cr,lf)
loop until b0="Y"

gosub V_Cal
gosub display
do 'slave reads sensors ready for transmitting
     
hi2cin 1,(mins)
mins=ToBin(mins)

        for b0 = 0 to 9
            pause 100
            next b0
        peek minptr, @bptr'poke in sub dis
     
loop until mins<>@bptr
 
loop

TH:    'Connection for the DRF1276DM to the picaxe 28X2
'picaxe A.1(TXD)--------Pin 4 (RXD) DRF
'picaxe A.2(RXD)--------Pin 5 (TXD) DRF
     
      'serout TX,DRF,(85,85,85,85,Qlf,"Hello")'wake up slave call
      ;serout TX,DRF,(Qlf,fail)
    serin[40000],RX,drf,(Qlf),voltM,voltL,dry,RH,airM,airL,leafm,photo,b0
    b2=0:b2=b2+voltM:b2=b2+voltL:b2=b2+dry:b2=b2+RH:b2=b2+airM:b2=b2+airL:b2=b2+leafM:b2=b2+photo
    if b0=b2 then
        sertxd("read matches ",#b0," w1 ",#b2,cr,lf)
        b0="Y"
    serout TX,DRF,(Qlf,b0)
    hsersetup   B9600_8, %01
    elseif b0<>b2 then
        sertxd("read does not match ",#b0," b2 ",#b2,cr,lf)
        b0="N"
        serout TX,DRF,(Qlf,b0)
    endif
    return
display:
sertxd(" Air= ",#airM,".",#airL," ","RH= ",#RH," ","Photo= ",#photo," ","volt= ",#voltM,".",#voltL," ",cr,lf)
        bintoascii result,b50,b51,b52,b53,b54
        if b50="0" then
            b50=" "
            endif
        sertxd(" svp (from result)=",b50,b51,".",b52,b53,b54," ",cr,lf)
        bintoascii leaf,b50,b51,b52,b53,b54
        if b50="0" then
            b50=" "
            endif
        sertxd(" svp (from leaf)=",b50,b51,".",b52,b53,b54," ",cr,lf)
         bintoascii vdiff,b50,b51,b52,b53,b54
        if b50="0" then
            b50=" "
            endif
        sertxd(" vpd=",b50,b51,".",b52,b53,b54,cr,lf)
     
return
V_Cal:'*********** VPD CALCULATIONS ************
     
     
        sertxd("Air temp= ",#dry," ",lf)
        svp = dry * 2 : Read svp, result
          svp = svp   + 1 : Read svp, svp
        sertxd( " eeprom for result= ",#result," eeprom for svp= ",#svp,cr,lf)
        result = rh * result
          svp = rh * svp
        result = svp /  100 + result
          result = svp // 100 / 50 + result
     
     
        sertxd("leaf temp= ",#leafM,lf)
        svp = leafm * 2 : Read svp, leaf
          svp = svp   + 1 : Read svp, svp
         sertxd( " eeprom for leaf= ",#leaf," eeprom for svp= ",#svp,cr,lf)
          leaf=leaf*100+svp

        if result>leaf then
    vdiff=result-leaf
    elseif result<leaf then
        vdiff=leaf-result
        endif
        return
test code
Code:
Symbol tempC = b0
Symbol rh    = b1

symbol vp   = w1 ; b3:b2
symbol svp   = w2 ; b5:b4
symbol Avpd = w5 ;b7:b6
symbol vpd=w6
'Data(  6,11 ,  6,57 ,  7,06 ,  7,58 ,  8,13 ,  8,72 ,  9,35 )
  'Data( 10,02 , 10,73 , 11,48 , 12,28 , 13,12 , 14,02 , 14,97 )
  'Data( 15,98 , 17,05 , 18,18 , 19,37 , 20,64 , 21,97 , 23,38 )
  'Data( 24,86 , 26,43 ,28,09, 29,83 , 31,67 , 33,61 , 35,65  )
  'Data( 37,79 , 40,05 , 42,42 , 44,92 , 47,54 , 50,29 , 53,18 )
  'Data( 56,21 , 59,40 , 62,73 , 66,23 , 69,90 , 73,74 )
  'Data( 77,76, 82,38 )
Data(  6,11 ,  6,57 ,  7,06 ,  7,58 ,  8,13 ,  8,72 ,  9,35 )'0,1,2,3,4,5,6 degC
  Data( 10,02 , 10,73 , 11,48 , 12,28 , 13,12 , 14,02 , 14,97 )'7,8,9,10,11,12,13
  Data( 15,98 , 17,05 , 18,18 , 19,37 , 20,64 , 21,97 , 23,38 )'14,15,16,17,18,19,20
  Data( 24,86 , 26,43 ,28,09, 29,83 , 31,67 , 33,61 , 35,65  )'21,22,23,24,25,26,27
  Data( 37,79 , 40,05 , 42,42 , 44,92 , 47,54 , 50,29 , 53,18 )'28,29,30,31,32,33,34
  Data( 56,21 , 59,40 , 62,73 , 66,23 , 69,90 , 73,74, 77,76 )'35,36,37,38,39,40,41
  Data( 82,08, 86,49, 91,11, 95,93, 100,98, 106,25, 111,75 )'42,43,44,45,46,47,48
  data ( 117,50, 123,50)'49,50
TestProgram:
  For rh = 58 To 58
    For tempC = 20 To 20
    SerTxd( #tempC, " C ", TAB, #rh, "%RH ",cr, LF ) 
      Gosub CalculateVpd
   
    Next
    SerTxd( CR, LF )
  Next

  CalculateVpd:
    'calculation for air vapour pressure
  svp = tempC * 2 : Read svp, vp
  svp = svp   + 1 : Read svp, svp
         sertxd( " eeprom for vp= ",#vp," eeprom for svp= ",#svp,cr,lf)

          vp = rh * vp
         svp = rh * svp
        vp = svp /  100 + vp
          vp = svp // 100 / 50 + vp
        bintoascii vp,b5,b6,b7,b8,b9
          if b5="0" then
          b5=" "
          endif
           sertxd( "calculated air pressure result= ",b5,b6,".",b7,b8,b9,cr,lf)
        'calculation for leaf saturated vapour pressure
        svp = tempC * 2 : Read svp, avpd
          svp = svp   + 1 : Read svp, svp
         sertxd( " eeprom for avpd= ",#avpd," eeprom for svp= ",#svp,cr,lf)
          avpd=avpd*100+svp
    bintoascii avpd,b5,b6,b7,b8,b9
     if b5="0" then
      b5=" "
      endif
    sertxd( " calculated saturated pressure= ",b5,b6,".",b7,b8,b9,cr,lf)


       vpd=avpd-vp
    bintoascii vpd,b5,b6,b7,b8,b9
     if b5="0" then
      b5=" "
      endif
    sertxd( " VPD of the air= ",b5,b6,".",b7,b8,b9,cr,lf)

  Return
the gosub from the 20x2 showing the data layout and checksum format
Code:
send:
    b0=0:b0=b0+batth:b0=b0+battl:b0=b0+TemperatureMSB:b0=b0+humidity:b0=b0+whole:b0=b0+fract:b0=b0+whole1:b0=b0+lamp
    
    serout TX,drf,(Qlf,battH,battL,TemperatureMSB,Humidity,whole,fract,whole1,lamp,b0)
    serin[6000], RX,drf,(Qlf),b0
    
        
    
    return
regards john
 

Attachments

Last edited:

hippy

Technical Support
Staff member
There obviously shouldn't be any differences between a program running in the simulator and when the same is running on a PICAXE chip, so off-hand I am not sure what the issue would be.

I will take a look later to see what I can discover. Will hopefully be able to give you some sort of answer before the end of today.
 

johnlong

Senior Member
Thanks for the quick reply have added the gosub for the data transmission of the 20x2 to see if that sheads any light
regards jo
 

hippy

Technical Support
Staff member
Just to be clear; this is an issue where the results from a real PICAXE are different to results from simulating, that's it's not an issue of divergence from what the reference material may say the result should be ?

Because I can't see any difference when I test on a 28X2 ...
Code:
#Picaxe 28X2
#Terminal 9600

Symbol tempC = b0
Symbol rh    = b1

symbol vp    = w1 ; b3:b2
symbol svp   = w2 ; b5:b4
symbol avpd  = w3 ; b7:b6
symbol vpd   = w4 ; b9:b8

Data(   6,11 ,   6,57 ,  7,06 ,  7,58 ,   8,13 ,   8,72 ,  9,35  ) ;  0, 1, 2, 3, 4, 5, 6 degC
Data(  10,02 ,  10,73 , 11,48 , 12,28 ,  13,12 ,  14,02 ,  14,97 ) ;  7, 8, 9,10,11,12,13
Data(  15,98 ,  17,05 , 18,18 , 19,37 ,  20,64 ,  21,97 ,  23,38 ) ; 14,15,16,17,18,19,20
Data(  24,86 ,  26,43 , 28,09 , 29,83 ,  31,67 ,  33,61 ,  35,65 ) ; 21,22,23,24,25,26,27
Data(  37,79 ,  40,05 , 42,42 , 44,92 ,  47,54 ,  50,29 ,  53,18 ) ; 28,29,30,31,32,33,34
Data(  56,21 ,  59,40 , 62,73 , 66,23 ,  69,90 ,  73,74 ,  77,76 ) ; 35,36,37,38,39,40,41
Data(  82,08 ,  86,49 , 91,11 , 95,93 , 100,98 , 106,25 , 111,75 ) ; 42,43,44,45,46,47,48
data( 117,50 , 123,50                                            ) ; 49,50

TestProgram:
  Do
    For tempC = 0 To 50 Step 10
      For rh = 0 To 90 Step 15
        Gosub CalculateVpd
        BinToAscii vpd, b15,b14,b13,b12,b11
        If b15 = "0" Then
          b15 = " "
        End If
        SerTxd( #tempC, "C", TAB, #rh, "%", TAB, b15,b14,".",b13,b12,b11, CR, LF ) 
      Next
    Next
    SerTxd( "=====", CR, LF )
    Pause 10000
  Loop

CalculateVpd:
  ; Calculation for air vapour pressure
  svp = tempC * 2 : Read svp, vp
  svp = svp   + 1 : Read svp, svp
  vp  = rh * vp
  svp = rh * svp
  vp  = svp /  100 + vp
  vp  = svp // 100 / 50 + vp
  ; Calculation for leaf saturated vapour pressure
  svp  = tempC * 2 : Read svp, avpd
  svp  = svp   + 1 : Read svp, svp
  avpd = avpd * 100 + svp
  vpd  = avpd - vp
  Return
Results for both simulation and 28X2 are, with a few checked against the Arizona reference, PICAXE discrepancy in brackets ...
Code:
0C    0%     0.611 : 0.611
0C    15%    0.519
0C    30%    0.428
0C    45%    0.336 
0C    60%    0.244 : 0.244
0C    75%    0.153
0C    90%    0.061
10C   0%     1.228
10C   15%    1.044 : 1.044
10C   30%    0.860
10C   45%    0.675
10C   60%    0.491
10C   75%    0.307 : 0.307
10C   90%    0.123
20C   0%     2.338
20C   15%    1.987
20C   30%    1.637 : 1.637
20C   45%    1.286
20C   60%    0.935
20C   75%    0.584
20C   90%    0.234 : 0.234
30C   0%     4.242
30C   15%    3.606 : 3.609 (-0.003)
30C   30%    2.969
30C   45%    2.333 : 2.335 (-0.002)
30C   60%    1.697
30C   75%    1.060
30C   90%    0.424
40C   0%     7.374 : 7.383 (-0.009)
40C   15%    6.268
40C   30%    5.162
40C   45%    4.056
40C   60%    2.950 : 2.953 (-0.003)
40C   75%    1.843
40C   90%    0.737 : 0.738 (-0.001)
50C   0%    12.350
50C   15%   10.497 : 10.497
50C   30%    8.645
50C   45%    6.792
50C   60%    4.940
50C   75%    3.087 : 3.087
50C   90%    1.235
The environmental circumstances where a 'vpd' of 0.654 would be the result given by the PICAXE are ...
Code:
5C    25%    0.654
6C    30%    0.654
9C    43%    0.654
16C   64%    0.654
33C   87%    0.654
The only thing I can imagine which would be creating a difference is the receiving code not getting correct values as input, the typical 'garbage in, garbage out' scenario, or there being something in the code which is actually altering or corrupting the results in some circumstances, perhaps a clash of variables so correct data is being overwritten with different values.

That's potentially the case with your test program where 'b5' used in your BINTOASCII conversions conflict with 'svp' defined as 'w2', 'b5:b4'. Also your 'TestProgram:' falls through into 'CalculateVpd:' but I'm assuming the test program isn't really the issue.

I would say that whatever the issue is it's not down to the PICAXE or the calculations. We haven't visited those for a while. We did check them against data we had, other variations on determining 'vpd', but hadn't seen the Arizona calculator. It's good to know we get quite good matches with that
 

hippy

Technical Support
Staff member
Having re-read your original post it seemed you might have been saying that VPD was correct but some other value was differing between simulation and PICAXE. Tested that and all seem consistent, same for 28X2 and 20X2 ...
Code:
        Gosub CalculateVpd
        SerTxd( #tempC, "C", TAB, #rh, "%" ) 
        ; VP
        BinToAscii vp, b15,b14,b13,b12,b11
        SerTxd( TAB, b15,b14,".",b13,b12,b11 )
        ; SVP
        BinToAscii svp, b15,b14,b13,b12,b11
        SerTxd( TAB, b15,b14,".",b13,b12,b11 )
        ; AVPD
        BinToAscii avpd, b15,b14,b13,b12,b11
        SerTxd( TAB, b15,b14,".",b13,b12,b11 )
        ; VPD
        BinToAscii vpd, b15,b14,b13,b12,b11
        SerTxd( TAB, b15,b14,".",b13,b12,b11 )
        SerTxd( CR, LF )
Code:
Temp  RH    VP          SVP         AVPD        VPD
0C    0%    00.000      00.011      00.611      00.611
0C    15%   00.092      00.011      00.611      00.519
0C    30%   00.183      00.011      00.611      00.428
0C    45%   00.275      00.011      00.611      00.336
0C    60%   00.367      00.011      00.611      00.244
0C    75%   00.458      00.011      00.611      00.153
0C    90%   00.550      00.011      00.611      00.061
10C   0%    00.000      00.028      01.228      01.228
10C   15%   00.184      00.028      01.228      01.044
10C   30%   00.368      00.028      01.228      00.860
10C   45%   00.553      00.028      01.228      00.675
10C   60%   00.737      00.028      01.228      00.491
10C   75%   00.921      00.028      01.228      00.307
10C   90%   01.105      00.028      01.228      00.123
20C   0%    00.000      00.038      02.338      02.338
20C   15%   00.351      00.038      02.338      01.987
20C   30%   00.701      00.038      02.338      01.637
20C   45%   01.052      00.038      02.338      01.286
20C   60%   01.403      00.038      02.338      00.935
20C   75%   01.754      00.038      02.338      00.584
20C   90%   02.104      00.038      02.338      00.234
30C   0%    00.000      00.042      04.242      04.242
30C   15%   00.636      00.042      04.242      03.606
30C   30%   01.273      00.042      04.242      02.969
30C   45%   01.909      00.042      04.242      02.333
30C   60%   02.545      00.042      04.242      01.697
30C   75%   03.182      00.042      04.242      01.060
30C   90%   03.818      00.042      04.242      00.424
40C   0%    00.000      00.074      07.374      07.374
40C   15%   01.106      00.074      07.374      06.268
40C   30%   02.212      00.074      07.374      05.162
40C   45%   03.318      00.074      07.374      04.056
40C   60%   04.424      00.074      07.374      02.950
40C   75%   05.531      00.074      07.374      01.843
40C   90%   06.637      00.074      07.374      00.737
50C   0%    00.000      00.050      12.350      12.350
50C   15%   01.853      00.050      12.350      10.497
50C   30%   03.705      00.050      12.350      08.645
50C   45%   05.558      00.050      12.350      06.792
50C   60%   07.410      00.050      12.350      04.940
50C   75%   09.263      00.050      12.350      03.087
50C   90%   11.115      00.050      12.350      01.235
 

hippy

Technical Support
Staff member
And, just for completeness with the Arizona calculator, HD ( Humidy Deficit, g/m3) seems calculatable, though not entirely accurately, with -

HD = vpd / 0.14

Code:
  SerTxd( #tempC, "C", TAB, #rh, "%" ) 
  ; Calculste HD
  Select Case vpd
    Case <=  655 : hd = vpd * 100 / 14
    Case <= 6553 : hd = vpd * 10  / 14 * 10
    Else         : hd = vpd       / 14 * 100
  End Select
  BinToAscii hd, b15,b14,b13,b12,b11
  SerTxd( TAB, b15,b14,".",b13,b12,b11 )
  SerTxd( CR, LF )
Might have to adjust that if HD can ever be greater than "64 g/m3". I haven't checked.
 

johnlong

Senior Member
Hi
Have stripped out all the sertxd from the vpd gosub and moved to display gosub and change the variable for the leaf tempreture to satT to avoid
any possible confusion so each tempreture has its own variable and each result its own
Code:
V_Cal:'*********** VPD CALCULATIONS ************
        
        
        ;sertxd("Air temp= ",#dry," ",lf)
        svp = dry * 2 : Read svp, result
          svp = svp   + 1 : Read svp, svp
        ;sertxd( " eeprom for result= ",#result," eeprom for svp= ",#svp,cr,lf)
        result = rh * result
          svp = rh * svp
        result = svp /  100 + result
          result = svp // 100 / 50 + result
        
        
        ;sertxd("leaf temp= ",#satT,lf)
        svp = satT * 2 : Read svp, leaf
          svp = svp   + 1 : Read svp, svp
         ;sertxd( " eeprom for leaf= ",#leaf," eeprom for svp= ",#svp,cr,lf)
          leaf=leaf*100+svp

        if result>leaf then
    vdiff=result-leaf
    elseif result<leaf then
        vdiff=leaf-result
        endif
        return
display:

        bintoascii dry,b50,b51,b52
        sertxd(" Air temp= ",b51,b52," ",cr,lf)
        bintoascii result,b50,b51,b52,b53,b54
        if b50="0" then
            b50=" "
            endif
        sertxd(" svp (from result)=",b50,b51,".",b52,b53,b54," ",cr,lf)
        bintoascii satT,b50,b51,b52
        sertxd(" leaf temp= ",b51,b52," ",cr,lf)
        bintoascii leaf,b50,b51,b52,b53,b54
        if b50="0" then
            b50=" "
            endif
        sertxd(" svp (from leaf)=",b50,b51,".",b52,b53,b54," ",cr,lf)
         bintoascii vdiff,b50,b51,b52,b53,b54
        if b50="0" then
            b50=" "
            endif
        sertxd(" vpd=",b50,b51,".",b52,b53,b54,cr,lf)
        sertxd(" Air= ",#airM,".",#airL," ","RH= ",#RH," ","Photo= ",#photo," ","volt= ",#voltM,".",#voltL," ",cr,lf)
        
return
as an attempt to see if the bintoascii was like you said leaving a bit of garbage behind
but it is still showing error for the VP calculation on the 28x2 with the test rig being wiithin a small margin
the first image micro1 is from the 28x2 (in real time) reported results on the serial terminal the 2nd is the values from simulation
the picaxe is recording a result of 1.321 KPa with the calculator reporting 1.368 KPa, 47KPa out
the test simulation is giving a result of 1.367 KPa for a temperature of 21 with a humidity of 55% ,1KPa out insignificant in the real world
Seem like your having some fun with the calculator never looked at the HD side of it but very nice to know that it within the bounds
of the Picaxe in capable hands
 

Attachments

hippy

Technical Support
Staff member
the picaxe is recording a result of 1.321 KPa with the calculator reporting 1.368 KPa, 47KPa out
the test simulation is giving a result of 1.367 KPa for a temperature of 21 with a humidity of 55% ,1KPa out insignificant in the real world
I'm utterly confused. Your left image shows something entirely different to the right, not just in terms of values, so it's not the same code. So I'm not sure exactly where the evidence is for 'it works in simulation but not on the real chip'.

The calculation works in simulation and the real chip though there are some discrepancies. But those are in the region of 0.002 kPa, or 2 Pa. That's an error of merely 0.08%

Whether I run the calculation in simulation or on an actual PICAXE I get exactly the same results and those seem to tie in with the Arizona calculator ...
Code:
Simulation / PICAXE

Temp  RH    VP          SVP         AVPD        VPD
21C   55%   01.367      00.086      02.486      01.119

Arizona Calculator

SVP : 2.488    2.486 (-0.002) using AVPD
VP  : 1.368    1.367 (-0.001)
VPD : 1.119    1.119
Code:
Simulation / PICAXE

Temp  RH    VP          SVP         AVPD        VPD
19C   55%   01.208      00.097      02.197      00.989

Arizona Calculator

SVP : 2.198   2.197 (-0.001) using AVPD
VP  : 1.209   1.208 (-0.001)
VPD : 0.989   0.989
So any error or major discrepancy you are seeing must have been introduced by your own code, because the calculations seem valid.

It's not clear to me what you are trying to determine. You have an air temperature, a leaf temperate, an RH value, are determining the air VPD and leaf VPD ... and then what ?

If you can provide that equation and what result you expect from 21C, 19C, 55%RH. I can see what results I get.
 

hippy

Technical Support
Staff member
It's been a long time since I looked at the VPD calculations and it was cut and pasted from other code, so how variables are used may not be entirely reflective of how they are named, and there is also variable re-use which is perhaps confusing things.

In particular SVP seems only used as an index into the Data Eeprom, not as an SVP value. AVPD, whatever that was intended to mean, seems to actually be the SVP value.

The calculation was only intended to calculate VPD, not anything else, but it does seem to deliver VP and SVP ( via AVPD ), so it may be better to represent the calculations as shown below -
Code:
#Picaxe 20X2
#Terminal 9600

Symbol tempC = b0
Symbol rh    = b1

symbol lsd   = w1 ; b3:b2
symbol vp    = w2 ; b5:b4
symbol svp   = w3 ; b7:b6
symbol vpd   = w4 ; b9:b8

Data(   6,11 ,   6,57 ,  7,06 ,  7,58 ,   8,13 ,   8,72 ,  9,35  ) ;  0, 1, 2, 3, 4, 5, 6 degC
Data(  10,02 ,  10,73 , 11,48 , 12,28 ,  13,12 ,  14,02 ,  14,97 ) ;  7, 8, 9,10,11,12,13
Data(  15,98 ,  17,05 , 18,18 , 19,37 ,  20,64 ,  21,97 ,  23,38 ) ; 14,15,16,17,18,19,20
Data(  24,86 ,  26,43 , 28,09 , 29,83 ,  31,67 ,  33,61 ,  35,65 ) ; 21,22,23,24,25,26,27
Data(  37,79 ,  40,05 , 42,42 , 44,92 ,  47,54 ,  50,29 ,  53,18 ) ; 28,29,30,31,32,33,34
Data(  56,21 ,  59,40 , 62,73 , 66,23 ,  69,90 ,  73,74 ,  77,76 ) ; 35,36,37,38,39,40,41
Data(  82,08 ,  86,49 , 91,11 , 95,93 , 100,98 , 106,25 , 111,75 ) ; 42,43,44,45,46,47,48
data( 117,50 , 123,50                                            ) ; 49,50

TestProgram:
  Do
    tempC = 19
    rh    = 55
    Gosub CalculateVpd
    SerTxd("Temp",TAB,"RH",TAB,"VP",TAB,TAB,"SVP",TAB,TAB,"VPD",CR,LF)
    SerTxd( #tempC, "C", TAB, #rh, "%" ) 
    ; VP
    BinToAscii vp, b15,b14,b13,b12,b11
    SerTxd( TAB, b15,b14,".",b13,b12,b11 )
    ; SVP
    BinToAscii svp, b15,b14,b13,b12,b11
    SerTxd( TAB, b15,b14,".",b13,b12,b11 )
    ; VPD
    BinToAscii vpd, b15,b14,b13,b12,b11
    SerTxd( TAB, b15,b14,".",b13,b12,b11 )
    SerTxd( CR, LF ) 
    Pause 10000
  Loop

CalculateVpd:
  lsd = tempC * 2 : Read lsd, vp
  lsd = lsd   + 1 : Read lsd, lsd
  vp  = rh * vp
  lsd = rh * lsd
  vp  = lsd /  100 + vp
  vp  = lsd // 100 / 50 + vp
  lsd = tempC * 2 : Read lsd, svp
  lsd = lsd   + 1 : Read lsd, lsd
  svp = svp * 100 + lsd
  vpd = svp - vp
  Return
 

Aries

New Member
It may be an old-fashioned method but, if two allegedly identical pieces of code give different results, then the traditional approach was to pepper the code with print statements (sertxd) of the variables as they are calculated and look for where the differences first occur.
 

johnlong

Senior Member
Hi Hippy
The 2 codes are diffrent the code for the picaxe28x2 obtains its values from a wireless connection via a pair of
DRF1276DM
The remote unit is a 20x2 reading a HTU21D for the air temperature and humidity and a MLX90614 IR sensor to obtain the
leafs temperature.
The calculation to determine the VPD is the saturated vapour pressure of the leaf at for the leafs temperature (assume 100% internal RH) as a healthy plants temperature should always be less than the surrounding environment due to transposition minus the vapour pressure of the air at the airs temperature which should be higher. Thus creating the driving force for transporsition.
VPD=Satuurated vapour pressure(leaf)- Acctual vapour pressure(air).
The image micro1 shows the values recived by the 28x2 from the 20x2 which then does the calculation to determin VPD using the leafs temperature to find svp and the air temperature to find vp
the image test 5 is of the air value and RH value taken in by the 28x2 substituted into the test program and both checked against the VPD online calculator. Both calculations for vpd in either program are the same using the same format
what I am consistantly finding is that the test program produces exellent results but a persistant error in the calculation of vp with the 28x2 (47 to 50)
What I do not understand is even if their is a corruption of the data for the air temp the value is the value and both the actual calculation by the picaxe
and the simulatated should be the same as the values from the picaxe are used for both calculations
sorry for any confusion I may have caused
regards john
 

hippy

Technical Support
Staff member
It may be an old-fashioned method but, if two allegedly identical pieces of code give different results, then the traditional approach was to pepper the code with print statements (sertxd) of the variables as they are calculated and look for where the differences first occur.
Agreed. but, in this case doing that is simply clouding the issue. Simply comparing the end results of the calculation in simulation and on a PICAXE shows them to be the same, and comparing them to the Arizona calculator reference shows them to be correct with just minor discrepancies. Conclusions -

1) There's nothing wrong with the calculations and they aren't going wrong on a PICAXE.

2) Any issue must be with something other than the calculations themselves.

I am prepared to accept that any misunderstanding in using the calculations may have come about by the calculations using variable names which are not necessarily appropriate, particularly with respect to SVP and AVPD. I still have no idea why the SVP variable was used how it was, except for being variable reuse, and particularly why the SVP value is actually the AVPD variable in the calculations. That's been lost to history.

I'm also willing to admit that I had forgotten all about VPD and couldn't really understand the calculations yesterday. However it seems the Sleep Pixies arrived in the night and explained it all while I was sleeping. They are brilliant guys and gals. So ...

The DATA table is the SVP value for a particular temperature; SVP = DATA[tempC].

Data table entries are in the odd format they are in because it's the easiest way to write a 16-bit decimal value as two 8-bit values which can then be easily verified as correct. For 0C, Data(6,11) represents 611 Pa, 0.611 kPa. And Arizona calculator confirms 0C, 100%RH = 0.611 kPa.

The actual values in the table were determined by running some algorithm we had found somewhere and confirming it with other algorithms. It seems correct but does differ from the Arizona Calculator slightly but the differences are negligible. I wouldn't put money on which is right but if one wants to make SVP reflect the Arizona values one only needs to alter the value in the Data table and job done. So ...

SVP = Data[tempC]

That's a value when air is completely saturated, 100%RH, Saturated Vapour Pressure -
Code:
idx = tempC * 2 : Read idx, svp
idx = idx   + 1 : Read idx, lsd
svp = svp * 100 + lsd
'idx' is an index into the Data table, 'lsd' is the least significant digits of the data entry. It should be easy enough to see how Data(6,11) converts to 611 Pa, 0.611 kPa.

VP, Vapour Pressure, is a percentage of that depending on what the %RH value is -

VP = SVP * RH / 100

Rather than take a calculated SVP word value, multiply by RH and divide by 100, which will inevitably overflow or lose resolution, the two bytes are taken from Data() separately, multiplied by RH separately, then combined to give a VP value -
Code:
idx = tempC * 2 : Read idx, vp
idx = idx   + 1 : Read idx, lsd
vp  = rh * vp
lsd = rh * lsd
vp  = lsd /  100 + vp
vp  = lsd // 100 / 50 + vp
The "vp = lsd / 100 + vp" is the crucial calculation here, adds the non-decimal fraction part of 'lsd' to 'vp'. The "vp = lsd // 100 / 50 + vp" simply rounds the decimal fractional value to the nearest integer value.

Finally VPD, Vapour Pressure Deficit, is SVP - VP, which is simple enough to calculate -
Code:
vpd = svp - vp
So, if I were writing the code from scratch, not trying to save on variable use, nor optimising, I would go with this -
Code:
CalculateSvp:
  ; SVP = Data[tempC]
  idx = tempC * 2 : Read idx, svp
  idx = idx   + 1 : Read idx, lsd
  svp = svp * 100 + lsd
  Return
Code:
CalculateVp:
  ; VP = Data[tempC] * rh / 100
  idx = tempC * 2 : Read idx, vp
  idx = idx   + 1 : Read idx, lsd
  vp  = vp  * rh
  lsd = lsd * rh
  vp  = lsd /  100 + vp
  vp  = lsd // 100 / 50 + vp
  Return
Code:
CalculateVpd:
  ; VPD = SVP - VP
  Gosub CalculateSvp
  Gosub CalculateVp
  vpd = svp - vp
  Return
Code:
CalculateHd:
  ; HD = VPD / 0.14
  Gosub CalculateVpd
  Select Case vpd
    Case <=  655 : hd = vpd * 100 / 14
    Case <= 6553 : hd = vpd * 10  / 14 * 10
    Else         : hd = vpd       / 14 * 100
  End Select
  Return
 

hippy

Technical Support
Staff member
The remote unit is a 20x2 reading a HTU21D for the air temperature and humidity and a MLX90614 IR sensor to obtain the leafs temperature.
That makes sense - You are obtaining readings for -

TEMPair, TEMPleaf and %RH.

From that you can calculate -

SVPair, VPair and VPDair,
SVPleaf, VPleaf and VPDleaf.

VPD=Satuurated vapour pressure(leaf)- Acctual vapour pressure(air).
Right so the result you want, if I have read that correctly is -

result = SVPleaf - VPair

And when I test that, in simulation or on a PICAXE I get ...
Code:
TEMPair  = 21
TEMPleaf = 19
%RH      = 55
-----
For air ...
Temp  RH    VP          SVP         VPD
21C   55%    1.367       2.486       1.119
-----
For leaf ...
Temp  RH    VP          SVP         VPD
19C   55%    1.208       2.197       0.989
-----
SVPleaf =    2.197
VPair   =    1.367

result  =    0.830
And all those numbers match with the Arizona calculator as detailed earlier.

Code:
#Picaxe 20X2
#Terminal 9600

Symbol tempC    = b0
Symbol rh       = b1

symbol idx      = w1  ; b3:b2
symbol lsd      = w2  ; b5:b4
symbol vp       = w3  ; b7:b6
symbol svp      = w4  ; b9:b8
symbol vpd      = w5  ; b11:b10
symbol TEMPair  = w6  ; b13:b12
Symbol TEMPleaf = w7  ; b15:b14
Symbol VPair    = w8  ; b17:b16
Symbol SVPleaf  = w9  ; b19:b18
Symbol result   = w10 ; b21:b20

Data(   6,11 ,   6,57 ,  7,06 ,  7,58 ,   8,13 ,   8,72 ,  9,35  ) ;  0, 1, 2, 3, 4, 5, 6 degC
Data(  10,02 ,  10,73 , 11,48 , 12,28 ,  13,12 ,  14,02 ,  14,97 ) ;  7, 8, 9,10,11,12,13
Data(  15,98 ,  17,05 , 18,18 , 19,37 ,  20,64 ,  21,97 ,  23,38 ) ; 14,15,16,17,18,19,20
Data(  24,86 ,  26,43 , 28,09 , 29,83 ,  31,67 ,  33,61 ,  35,65 ) ; 21,22,23,24,25,26,27
Data(  37,79 ,  40,05 , 42,42 , 44,92 ,  47,54 ,  50,29 ,  53,18 ) ; 28,29,30,31,32,33,34
Data(  56,21 ,  59,40 , 62,73 , 66,23 ,  69,90 ,  73,74 ,  77,76 ) ; 35,36,37,38,39,40,41
Data(  82,08 ,  86,49 , 91,11 , 95,93 , 100,98 , 106,25 , 111,75 ) ; 42,43,44,45,46,47,48
data( 117,50 , 123,50                                            ) ; 49,50

#Macro SerTxdNumber( wVar )
  BinToAscii wVar, b55,b54,b53,b52,b51
  If b55 = "0" Then
    b55 = " "
  End If
  SerTxd( TAB, b55,b54,".",b53,b52,b51 )
#EndMacro

TestProgram:
  Do
    ; Readings from sensors
    TEMPair  = 21
    TEMPleaf = 19
    rh       = 55
    SerTxd( "TEMPair  = ", #TEMPair,  CR, LF )
    SerTxd( "TEMPleaf = ", #TEMPleaf, CR, LF )
    SerTxd( "%RH      = ", #rh,       CR, LF )
    SerTxd( "-----",                  CR, LF )

    ; Calculate for air
    SerTxd("For air ...", CR,LF )
    tempC = TEMPair
    Gosub Calculate
    VPair = vp

    ; Calculate for leaf
    SerTxd("For leaf ...", CR,LF )
    tempC = TEMPleaf
    Gosub Calculate
    SVPleaf = svp

    ; Calculate result
    result = SVPleaf - VPair
    SerTxd( "SVPleaf = " ) SerTxdNumber(SVPleaf) SerTxd( CR, LF )
    SerTxd( "VPair   = " ) SerTxdNumber(VPair)   SerTxd( CR, LF )
    SerTxd( CR, LF)
    SerTxd( "result  = " ) SerTxdNumber(result)  SerTxd( CR, LF )
    SerTxd( "=====", CR, LF)
    Pause 10000
  Loop

Calculate:
  SerTxd( "Temp", TAB      )
  SerTxd( "RH",   TAB      )
  SerTxd( "VP",   TAB, TAB )
  SerTxd( "SVP",  TAB, TAB )
  SerTxd( "VPD",  CR, LF   )
  SerTxd( #tempC, "C", TAB, #rh, "%" ) 
  Gosub CalculateVp  SerTxdNumber(vp)  ; VP
  Gosub CalculateSvp SerTxdNumber(svp) ; SVP
  Gosub CalculateVpd SerTxdNumber(vpd) ; VPD
  SerTxd( CR, LF, "-----", CR, LF)
  Return

CalculateSvp:
  ; SVP = Data[tempC]
  idx = tempC * 2 : Read idx, svp
  idx = idx   + 1 : Read idx, lsd
  svp = svp * 100 + lsd
  Return

CalculateVp:
  ; VP = Data[tempC] * rh / 100
  idx = tempC * 2 : Read idx, vp
  idx = idx   + 1 : Read idx, lsd
  vp  = vp  * rh
  lsd = lsd * rh
  vp  = lsd /  100 + vp
  vp  = lsd // 100 / 50 + vp
  Return

CalculateVpd:
  ; VPD = SVP - VP
  Gosub CalculateSvp
  Gosub CalculateVp
  vpd = svp - vp
  Return
 

hippy

Technical Support
Staff member
When I replicate your left-most image values in post #1, I get ...
Code:
TEMPair  = 24
TEMPleaf = 20
%RH      = 58
-----
For air ...
Temp  RH    VP          SVP         VPD
24C   58%    1.730       2.983       1.253
-----
For leaf ...
Temp  RH    VP          SVP         VPD
20C   58%    1.356       2.338       0.982
-----
SVPleaf =    2.338
VPair   =    1.730

result  =    0.608
Your result appears to be 0.654, but I don't understand what your "SVP (from result) = 1.684" represents or how it is calculated, nor where the "Air = 21.0" line is coming from.

Simolarly when replicating your left-most image in Post #7 -
Code:
TEMPair  = 21
TEMPleaf = 19
%RH      = 55
-----
For air ...
Temp  RH    VP          SVP         VPD
21C   55%    1.367       2.486       1.119
-----
For leaf ...
Temp  RH    VP          SVP         VPD
19C   55%    1.208       2.197       0.989
-----
SVPleaf =    2.197
VPair   =    1.367

result  =    0.830
Your result sees to be 0.876
 

johnlong

Senior Member
Hi Hippy
give those sleep Pixies a kiss from me have just rewriten the code as per your post 13
with substituting the air temp into vp and the leaf temp into svp results results
for the 2 values inline with the result of the calculator (marginable diffrences +/-1)
as you mentioned there must have being some overflow issues which is what I was thinking but with
getting such good results from the test program was starting to think maybe some coruption of the data sent
was occuring.
Have noticed whilst typing you have responded twice too answer your question the IR sensor reads the object temp and
the surrounding temp just sending both values as a refrance as to the accrucy of the 2 units and if the rounding up or down of the htu21d is
functioning as expected on the 20x2
will study your recient posts more closely
thank you
regards john
 

johnlong

Senior Member
Hi
responce to post 15
That was the error I was getting
the calculation for Vp for the air temp at 24'C at a RH of 58% the 28x2 was returning a Vp of 1.684
to be correct at those values it should have returned a value of 1.730 which is shown in the 2nd image using those values in the test
program to obtain the Vp of the air
the Svp values from the 28X2 give for 20'C at 58% RH a value of 2.338 which is correct to the calculator and correct with the
test program shown in the 3rd image
This was where I myself was getting confussed if the simulator returns the correct values wny not the 28X2
I did this by subtracting 4 from tempC value and using it for the calculations
Code:
Symbol tempC = b0
Symbol rh    = b1
symbol tempD=b2
symbol vp   = w1 ; b3:b2
symbol svp   = w2 ; b5:b4
symbol Avpd = w5 ;b7:b6
symbol vpd=w6
 'Data(  6,11 ,  6,57 ,  7,06 ,  7,58 ,  8,13 ,  8,72 ,  9,35 )
  'Data( 10,02 , 10,73 , 11,48 , 12,28 , 13,12 , 14,02 , 14,97 )
  'Data( 15,98 , 17,05 , 18,18 , 19,37 , 20,64 , 21,97 , 23,38 )
  'Data( 24,86 , 26,43 ,28,09, 29,83 , 31,67 , 33,61 , 35,65  )
  'Data( 37,79 , 40,05 , 42,42 , 44,92 , 47,54 , 50,29 , 53,18 )
  'Data( 56,21 , 59,40 , 62,73 , 66,23 , 69,90 , 73,74 )
  'Data( 77,76, 82,38 )
Data(  6,11 ,  6,57 ,  7,06 ,  7,58 ,  8,13 ,  8,72 ,  9,35 )'0,1,2,3,4,5,6 degC
  Data( 10,02 , 10,73 , 11,48 , 12,28 , 13,12 , 14,02 , 14,97 )'7,8,9,10,11,12,13
  Data( 15,98 , 17,05 , 18,18 , 19,37 , 20,64 , 21,97 , 23,38 )'14,15,16,17,18,19,20
  Data( 24,86 , 26,43 ,28,09, 29,83 , 31,67 , 33,61 , 35,65  )'21,22,23,24,25,26,27
  Data( 37,79 , 40,05 , 42,42 , 44,92 , 47,54 , 50,29 , 53,18 )'28,29,30,31,32,33,34
  Data( 56,21 , 59,40 , 62,73 , 66,23 , 69,90 , 73,74, 77,76 )'35,36,37,38,39,40,41
  Data( 82,08, 86,49, 91,11, 95,93, 100,98, 106,25, 111,75 )'42,43,44,45,46,47,48
  data ( 117,50, 123,50)'49,50
TestProgram:
  For rh = 58 To 58
    For tempC = 24 To 24
    SerTxd( #tempC, " C ", TAB, #rh, "%RH ",cr, LF )    
      Gosub CalculateVpd
      
    Next
    SerTxd( CR, LF )
  Next
  
  CalculateVpd:
    'calculation for air vapour pressure
  svp = tempC * 2 : Read svp, vp
  svp = svp   + 1 : Read svp, svp
         sertxd( " eeprom for vp= ",#vp," eeprom for svp= ",#svp,cr,lf)
  
          vp = rh * vp
         svp = rh * svp
        vp = svp /  100 + vp
          vp = svp // 100 / 50 + vp
        bintoascii vp,b5,b6,b7,b8,b9
          if b5="0" then
          b5=" "
          endif
           sertxd( "calculated air pressure result= ",b5,b6,".",b7,b8,b9,cr,lf)
        'calculation for leaf saturated vapour pressure
        tempD=tempC-4
        svp = tempD * 2 : Read svp, avpd
          svp = svp   + 1 : Read svp, svp
         sertxd( " eeprom for avpd= ",#avpd," eeprom for svp= ",#svp,cr,lf)
          avpd=avpd*100+svp
    bintoascii avpd,b5,b6,b7,b8,b9
     if b5="0" then
      b5=" "
      endif
    sertxd( " calculated saturated pressure= ",b5,b6,".",b7,b8,b9,cr,lf)
  
   
       vpd=avpd-vp
    bintoascii vpd,b5,b6,b7,b8,b9
     if b5="0" then
      b5=" "
      endif
    sertxd( " VPD of the air= ",b5,b6,".",b7,b8,b9,cr,lf)
  Return
regards john
 

hippy

Technical Support
Staff member
That was the error I was getting
the calculation for Vp for the air temp at 24'C at a RH of 58% the 28x2 was returning a Vp of 1.684
to be correct at those values it should have returned a value of 1.730 which is shown in the 2nd image using those values in the test
I'm guessing something in the way the PICAXE code was written was corrupting that result because the calculation does deliver 1.730 as expected for 24C and 58%. Not sure where though.
 

johnlong

Senior Member
Hi Hippy
rewrote the code as per your layout in post 13
and have had 170 reads without a blimp
Code:
#picaxe 28X2

     '  0        1      2       3       4       5       6 degC
 Data(  6,11 ,  6,57 ,  7,06 ,  7,58 ,  8,13 ,  8,72 ,  9,35 )
       ' 7        8       8      10      11       12     13
  Data( 10,02 , 10,73 , 11,48 , 12,28 , 13,12 , 14,02 , 14,97 )
  '      14       15     16       17     18      19       20
  Data( 15,98 , 17,05 , 18,18 , 19,37 , 20,64 , 21,97 , 23,38 )
  '      21       22    23     24      25       26      27
  Data( 24,86 , 26,43 ,28,09, 29,83 , 31,67 , 33,61 , 35,65  )
  '      28       29     30      31      32      33      34
  Data( 37,79 , 40,05 , 42,42 , 44,92 , 47,54 , 50,29 , 53,18 )
  '      35      36      37      38      39      40     41
  Data( 56,21 , 59,40 , 62,73 , 66,23 , 69,90 , 73,74, 77,76 )
  '      42     43     44     45     46      47      48
  Data( 82,08, 86,49, 91,11, 95,93, 100,98, 106,25, 111,75 )
  data ( 117,50, 123,50)'49,50
symbol TD3=b3 symbol TD2=b2 symbol TD1=b1 symbol TD0=b0 'reusable variables b0 to b4
'PICAXE VARIABLES FOR I2C CLOCK and DISPLAY changable taken from readings in main prog RUN
symbol themode=b5
symbol sec =b6
symbol hour=b7
symbol mins=b8  'Read write variables to the DS1307 using the DS1307 as the
symbol day=b9   'Timer for the Data Logging and Display
symbol month=b10  'hour = hour / 16 * $FA + hour Hippy's solutiON for BCD to Dec conversion
symbol year=b11
symbol date=b12
symbol control=$10'RTC control bit
;I2c address for DS1307
symbol tiktok= %11010000
'*************************
'case pointer and equipment selection between nextion display and picaxe
symbol pointer=b13  'set point and equipment selection for keypad 'constant   
symbol marker =b14      'interupt marker between program slots 'constant
'********************************
'Set point values determned by operator via nextion
symbol photo=b15     'light sensor determin day or night non changeable
symbol vT = b16 'constant day tempreture set point changeable via nextion
symbol vC=b17 'constant night tempreture set point changeable via nextion
symbol vHl=b18 'constant %RH minimum set point for manual changeable via nextion
symbol vH = b19 'constant %RH maximum set point for manual changeable via nextion
symbol vpdl=w10'b20,b21 high %RL low VPD changeable via nextion
symbol vpdh=w11'b22,b23 low %RH high VPD changeable via nextion
symbol Vpd=w12'b24,b25
symbol VP=w13'b27,b26 leaf tempreature and svp result
'Sensor results
symbol air=w14 symbol airM=b29 symbol airL=b28
symbol RH=b36 '%RH

symbol Dry     = b33  ;air tempreture deg C
'symbol SVP= w17 'b35,b34 data location for SVP tempreture values
symbol SVP=w17
symbol idx=b4
symbol lsd=w15
symbol FR=b37'constant alarm action threshold
'***********************
'Data storage selection to and from scratchpad
symbol usbon=b38   ' Data storage ON or off 'constant
symbol line=b39  'VARIABLES FOR uSD for line feed in excel
symbol TempW = w20 ;b40,b41 used for data storage calculations for all sensors
symbol temp2=w21 'uSD write end b38 +b39
symbol locate=b44'constant location of next bit in scratchpad
symbol satT=b45' error code sent from slave
symbol volt=w23 symbol voltM=b47 symbol voltL=b46
symbol prob = b48
symbol sdout=D.0:symbol sdin=D.1 'for sd card
'************************

'************************

'***********************
'Radio communication

'*******************
 Symbol Qlf= $FE 
symbol RX=A.1
symbol TX=A.2
symbol DRF=T9600
symbol minptr=102' for timing loop
symbol Bsin=105 'pins B.0 to B.3 to reload pin on state after return:
symbol bOptr=104'B0toB3 pin state for fan speed
symbol modeptr=96'?
symbol loop_counter=40
symbol transfer=61
symbol zero=106:symbol bone=107:symbol two=108:symbol three=109 'pinB4 to B7 equipment on/off
symbol humptr=110:symbol dehumptr=111:symbol tcptr=112
symbol prgptr=200'used to direct variabe locate for data storage after leaving and returning
symbol cfon=201:symbol cfoff=202:symbol son=203:symbol soff=204'side fan times on/off
symbol rtn_ptr=206'for temp2 location after leaving and returning
symbol tim_c=207'?
symbol dyvl=212'?
symbol dayval=90'day or night selection for variable vt
symbol dateval=91
symbol ptrval=100'?
symbol ptps=101'?
symbol errorptr=210'?
symbol sdfail=211
'*******************
symbol QT=34'NEXTION command
symbol COM = 44       ;comma
symbol RET = 13       ;carriage return
symbol LFEED = 10   'line feed
symbol CTRLZ = 26   'used to put uSD into command mode
#Define ToBin(bcd) bcd / 16 * $FA + bcd
#Define ToBcd(bin) bin / 10 * 6 + bin
'*********************************
#Macro SerTxdNumber( wVar )
  BinToAscii wVar, b55,b54,b53,b52,b51
  If b55 = "0" Then
    b55 = " "
  End If
  SerTxd( TAB, b55,b54,".",b53,b52,b51 )
#EndMacro

'***************************************
Hi2csetup i2cmaster,%11010000, i2cslow, i2cbyte

sec=30:mins=6:hour=15:day=7:date=28:month=8:year=19
pre_launch:

        sec=ToBcd(sec)
        mins = ToBcd(mins)
        hour= ToBcd(hour)
        day = ToBcd(day)
        date = ToBcd(date)
        month = ToBcd(month)
        year = ToBcd(year)
    hi2cout 0,(sec,mins,hour,day,date,month,year,control)
    pause 100
    hi2cin 0,(sec,mins,hour,day,date,month,year)
    sec=toBin(sec):mins=ToBin(mins):hour=ToBin(hour):day=ToBin(day):date=Tobin(date):month=ToBin(month):year=ToBin(year)
    sertxd("date ",#date," month ",#month," year ","20",#year,cr,lf)
    
main:
do 
    
    hi2cin 0,(sec,mins,hour)
    sec=toBin(sec):mins=ToBin(mins):hour=ToBin(hour)
    sertxd("hour ",#hour," mins ",#mins," sec ",#sec,cr,lf)
    poke minptr,mins
    high B.7

    serout TX,DRF,(85,85,85,85,Qlf,"Hello")'wake up slave call


do 
    gosub TH
    sertxd("b0 on release ",#b0,cr,lf)
loop until b0="Y"
gosub CalculateSvp
gosub CalculateVp
gosub V_Cal
gosub display
do 'slave reads sensors ready for transmitting
        
hi2cin 1,(mins) 
mins=ToBin(mins)

        for b0 = 0 to 9
            pause 100
            next b0
        peek minptr, @bptr'poke in sub dis
        
loop until mins<>@bptr
    
loop 

TH:    'Connection for the DRF1276DM to the picaxe 28X2
'picaxe A.1(TXD)--------Pin 4 (RXD) DRF
'picaxe A.2(RXD)--------Pin 5 (TXD) DRF
        inc prob
         sertxd(" TH  prob= ",#prob,cr,lf)
      'serout TX,DRF,(85,85,85,85,Qlf,"Hello")'wake up slave call
      ;serout TX,DRF,(Qlf,fail)
    serin[40000],RX,drf,(Qlf),voltM,voltL,dry,RH,airM,airL,satT,photo,b0
    b2=0:b2=b2+voltM:b2=b2+voltL:b2=b2+dry:b2=b2+RH:b2=b2+airM:b2=b2+airL:b2=b2+satT:b2=b2+photo
    
    if b0=b2 then
        sertxd("read matches ",#b0," w1 ",#b2,cr,lf)
        b0="Y"
    serout TX,DRF,(Qlf,b0)
    hsersetup   B9600_8, %01
    elseif b0<>b2 then
        sertxd("read does not match ",#b0," b2 ",#b2,cr,lf)
        b0="N"
        serout TX,DRF,(Qlf,b0)
    endif
    return
display:

    
        sertxd(" Air temp= ",#dry,cr,lf)
        sertxd(" leaf temp= ",#satT,cr,lf)
        SerTxd( "VPair   = " ) SerTxdNumber(VP)   SerTxd( CR, LF )
         SerTxd( "SVPleaf = " ) SerTxdNumber(SVP) SerTxd( CR, LF )
         SerTxd( "VPD = " ) SerTxdNumber(VPD) SerTxd( CR, LF )
        
        sertxd(" Air= ",#airM,".",#airL," ","RH= ",#RH," ","Photo= ",#photo," ","volt= ",#voltM,".",#voltL," ",cr,lf)
        
return
CalculateSvp:
  ; SVP = Data[satT] leaf temperature
  idx = satT * 2 : Read idx, svp
  idx = idx   + 1 : Read idx, lsd
  svp = svp * 100 + lsd
  Return
  CalculateVp:
  ; VP = Data[dry] * rh / 100  air temperature
  idx = dry * 2 : Read idx, vp
  idx = idx   + 1 : Read idx, lsd
  vp  = vp  * rh
  lsd = lsd * rh
  vp  = lsd /  100 + vp
  vp  = lsd // 100 / 50 + vp
  Return
V_Cal:'*********** VPD CALCULATIONS ************
     vpd = svp - vp
    return
splitting up the variables seemed to go the trick
just one more quick question as drf is T9600 idling high will this interfer with the enablebod command
regards john
 
Top