Author Topic: Don't ask... LOL  (Read 3194 times)

0 Members and 1 Guest are viewing this topic.

Offline MadScientist267

  • Impossible Condition Curator
  • Moderator
  • Hero Member
  • *****
  • Posts: 1514
  • Karma: +44/-4
  • Rules? What rules?
Don't ask... LOL
« on: August 20, 2014, 08:57:56 pm »
This is the QBasic program that controls the development version of the fridge... Full of "why would you do thats"... Provided as is, no warranty, for whatever comedic value it may have... now then... Ahem...

------------------------------------------------------

'Fridge 44A

'If there's an error, need to restart to attempt recovery
ON ERROR GOTO 5000

'Define the plot storage/movement array
DIM GFX(10000)

'Load the last saved config and error state
OPEN "C:\STEVE\FRIDGE.CFG" FOR INPUT AS 1
INPUT #1, STARVE
INPUT #1, ERRORSTATE
INPUT #1, MASTERDELAY
CLOSE

'Change to Graphics Mode and Load the Historical Plot
SCREEN 13
CLS
BLOAD "C:\STEVE\PLOT.BIN"
        
'If this was just an interruption...
IF ERRORSTATE = 0 THEN

   'Mark the plot at the current time with a grey vertical bar
   LINE (319, 0)-(319, 64), 8

'if there was an error...
ELSE
       
   'Mark the plot at the current time with a pink vertical bar
   LINE (319, 0)-(319, 64), 12
       
   '...and clear the error state
   ERRORSTATE = 0

END IF



'Clear Comm Buffer (read COM1 until there's nothing left)
OPEN "COM1:9600,N,8,1" FOR RANDOM AS 1
5 IF EOF(1) THEN 10
INPUT #1, A$
GOTO 5
10 CLOSE


'Init Variables

'Delay between IO board polls
POLLDELAY = 10000

'Compressor Start Delay (Seconds)
COMPRESSORDELAY = 300

'Core Translate Table
'235=5F  '220=2F  '210=0F  '200=-2F  '190=-4F  '180=-6F  '170=-8F  '160=-10F

'Core Threshold (mV) for Heater Control (Low Cut Off)
COREHEATTHRESHOLD = 210 '235

'Fridge Translate Table
'510=36F '500=35F '490=34F '480=33F '470=32F '460=31F '450=30F '440=29F

'Fridge Threshold (mV) for Heater Control (High Cut Off)
FRIDGEHEATTHRESHOLD = 460 '470 '480

'Normal (Surplus) Mode Thresholds (mV)
FRIDGENORMALMAX = 470 '480
FRIDGENORMALMIN = 455

'Emergent (Battery) Mode Thresholds (mV)
FRIDGEEMERGENTMAX = 520 '540
FRIDGEEMERGENTMIN = 500 '515

'Core Stir Fan Start Delay
COREFANSTARTDELAY = 180

'Compressor Fan and Core Stir Fan cutoff delay (Seconds)
COMPFANSTOPDELAY = 900
COREFANSTOPDELAY = 450

'Blower Compressor Start Threshold for Normal Mode
BLOWERMAX = 1500 '5000

'Blower Disable Threshold (See FRIDGEMV above)
BLOWERDISABLETHRESHOLD = 480 '490

'Plotting/Logging Interval (Seconds)
PLOTTRIGGER = 540 '270=24 Hour Window '540=48 Hour Window

'Core Pen Offset
COREPENOFFSET = 7

'Compressor Pen Offset
COMPRESSORPENOFFSET = -10

'Blower Pen Offset
BLOWERPENOFFSET = 4

'Blower Noise Gate Threshold
BLOWERNOISEGATE = 750


'Open Comm Port to talk to Boards
OPEN "COM1:9600,N,8,1" FOR RANDOM AS 1



'Init IO Board States

PRINT #1, "PLA" 'Compressor OFF
INPUT #1, DUMMY$

PRINT #1, "PLD" 'Compressor Fan OFF
INPUT #1, DUMMY$

PRINT #1, "PLG" 'Core Fan OFF
INPUT #1, DUMMY$

PRINT #1, "PLI" 'Heater OFF
INPUT #1, DUMMY$

PRINT #1, "PLK" 'Blower ENABLED
INPUT #1, DUMMY$

PRINT #1, "PHL" 'Blower NORMAL (Turbo off)
INPUT #1, DUMMY$

PRINT #1, "PHN" 'LED ON
INPUT #1, DUMMY$



1000 '***** Begin Master Loop *****

'***** Begin Read Boards

'*** Read Digital States
       
   '* Compressor (D)
   PRINT #1, "PRA"
   INPUT #1, RTN$
   COMPRESSOR = -1
   IF RTN$ = "PAL" THEN COMPRESSOR = 0
   IF RTN$ = "PAH" THEN COMPRESSOR = 1
       
   'Reply Sanity Check
   IF COMPRESSOR = -1 THEN
      PRINT "ERROR READING COMPRESSOR STATE!"
      FOR DELAY = 1 TO MASTERDELAY * 5: NEXT DELAY
      GOTO 5001
   END IF

FOR DELAY = 1 TO POLLDELAY: NEXT DELAY
       
   '* Compressor Fan (D)
   PRINT #1, "PRD"
   INPUT #1, RTN$
   COMPRESSORFAN = -1
   IF RTN$ = "PDL" THEN COMPRESSORFAN = 0
   IF RTN$ = "PDH" THEN COMPRESSORFAN = 1
       
   'Reply Sanity Check
   IF COMPRESSORFAN = -1 THEN
      PRINT "ERROR READING COMPRESSOR FAN STATE!"
      FOR DELAY = 1 TO MASTERDELAY * 5: NEXT DELAY
      GOTO 5001
   END IF


FOR DELAY = 1 TO POLLDELAY: NEXT DELAY

   '* Core Fan (D)
   PRINT #1, "PRG"
   INPUT #1, RTN$
   COREFAN = -1
   IF RTN$ = "PGL" THEN COREFAN = 0
   IF RTN$ = "PGH" THEN COREFAN = 1
       
   'Reply Sanity Check
   IF COREFAN = -1 THEN
      PRINT "ERROR READING CORE FAN STATE!"
      FOR DELAY = 1 TO MASTERDELAY * 5: NEXT DELAY
      GOTO 5001
   END IF


FOR DELAY = 1 TO POLLDELAY: NEXT DELAY

   '* Heater (D)
   PRINT #1, "PRI"
   INPUT #1, RTN$
   HEAT = -1
   IF RTN$ = "PIL" THEN HEAT = 0
   IF RTN$ = "PIH" THEN HEAT = 1
       
   'Reply Sanity Check
   IF HEAT = -1 THEN
      PRINT "ERROR READING HEATER STATE!"
      FOR DELAY = 1 TO MASTERDELAY * 5: NEXT DELAY
      GOTO 5001
   END IF


FOR DELAY = 1 TO POLLDELAY: NEXT DELAY
       
   '* Blower Enabled/Disabled (D)
   PRINT #1, "PRK"
   INPUT #1, RTN$
   BLOWERENABLED = -1
   IF RTN$ = "PKL" THEN BLOWERENABLED = 1   'yes, these
   IF RTN$ = "PKH" THEN BLOWERENABLED = 0   'are inverted!
     
   'Reply Sanity Check
   IF BLOWERENABLED = -1 THEN
      PRINT "ERROR READING BLOWER ENABLED STATE!"
      FOR DELAY = 1 TO MASTERDELAY * 5: NEXT DELAY
      GOTO 5001
   END IF


FOR DELAY = 1 TO POLLDELAY: NEXT DELAY


   '* Blower Turbo(D)
   PRINT #1, "PRL"
   INPUT #1, RTN$
   BLOWERTURBO = -1
   IF RTN$ = "PLL" THEN BLOWERTURBO = 1   'yes, these
   IF RTN$ = "PLH" THEN BLOWERTURBO = 0   'are inverted!
     
   'Reply Sanity Check
   IF BLOWERTURBO = -1 THEN
      PRINT "ERROR READING BLOWER TURBO STATE!"
      FOR DELAY = 1 TO MASTERDELAY * 5: NEXT DELAY
      GOTO 5001
   END IF


FOR DELAY = 1 TO POLLDELAY: NEXT DELAY


   '* Blower Turbo(D)
   PRINT #1, "PRL"
   INPUT #1, RTN$
   BLOWERTURBO = -1
   IF RTN$ = "PLL" THEN BLOWERTURBO = 1   'yes, these
   IF RTN$ = "PLH" THEN BLOWERTURBO = 0   'are inverted!
     
   'Reply Sanity Check
   IF BLOWERTURBO = -1 THEN
      PRINT "ERROR READING BLOWER TURBO STATE!"
      FOR DELAY = 1 TO MASTERDELAY * 5: NEXT DELAY
      GOTO 5001
   END IF


FOR DELAY = 1 TO POLLDELAY: NEXT DELAY


'*** End Digital States

'*** Read Analog Inputs

   '* Blower Speed mV (A)
   PRINT #1, "BRA"
   INPUT #1, RTN$
   BLOWERSPEED = -100000
   IF INSTR(RTN$, "B") = 1 THEN BLOWERSPEED = VAL(MID$(RTN$, 2, LEN(RTN$)))
   
   'Reply Sanity Check
   IF BLOWERSPEED = -100000 THEN
      PRINT "ERROR READING BLOWER SPEED STATE!"
      FOR DELAY = 1 TO MASTERDELAY * 5: NEXT DELAY
      GOTO 5001
   END IF

   'Noise Gate for Historical Plot
   IF BLOWERSPEED < BLOWERNOISEGATE THEN BLOWERSPEED = 0


FOR DELAY = 1 TO POLLDELAY: NEXT DELAY

   '* Core Temp mV (A)
   PRINT #1, "BRB"
   INPUT #1, RTN$
   COREMV = -100000
   IF INSTR(RTN$, "B") = 1 THEN COREMV = VAL(MID$(RTN$, 2, LEN(RTN$)))
       
   'Reply Sanity Check
   IF COREMV = -100000 THEN
      PRINT "ERROR READING CORE mV STATE!"
      FOR DELAY = 1 TO MASTERDELAY * 5: NEXT DELAY
      GOTO 5001
   END IF


FOR DELAY = 1 TO POLLDELAY: NEXT DELAY

   '* Fridge Temp mV (A)
   PRINT #1, "BRC"
   INPUT #1, RTN$
   FRIDGEMV = -100000
   IF INSTR(RTN$, "B") = 1 THEN FRIDGEMV = VAL(MID$(RTN$, 2, LEN(RTN$)))
       
   'Reply Sanity Check
   IF FRIDGEMV = -100000 THEN
      PRINT "ERROR READING FRIDGE mV STATE!"
      FOR DELAY = 1 TO MASTERDELAY * 5: NEXT DELAY
      GOTO 5001
   END IF


FOR DELAY = 1 TO POLLDELAY: NEXT DELAY

   '*** THIS IS BEING LEFT FOR OBSERVATION PURPOSES ONLY ***
   '* Compressor Temp mV (A)
   PRINT #1, "BRD"
   INPUT #1, RTN$
   TEMPD = -100000
   IF INSTR(RTN$, "B") = 1 THEN COMPRESSORMV = VAL(MID$(RTN$, 2, LEN(RTN$)))
       
   'Reply Sanity Check
   IF COMPRESSORMV = -100000 THEN
      PRINT "ERROR READING COMPRESSOR TEMP mV STATE!"
      FOR DELAY = 1 TO MASTERDELAY * 5: NEXT DELAY
      GOTO 5001
   END IF


FOR DELAY = 1 TO POLLDELAY: NEXT DELAY



'***** End Read Boards



'***** BEGIN Decisions


'If the compressor isn't running...
IF COMPRESSOR = 0 THEN

   'turn blower turbo off
   PRINT #1, "PHL"
   INPUT #1, DUMMY$

   '...and running in normal mode...
   IF STARVE = 0 THEN

      'If the fridge is below the blower disable threshold...
      IF FRIDGEMV < BLOWERDISABLETHRESHOLD THEN
        
         'disable the blower
         PRINT #1, "PHK"
         INPUT #1, DUMMY$
     
      END IF

      'If the fridge temp is above the blower disable threshold...
      IF FRIDGEMV > BLOWERDISABLETHRESHOLD THEN

         'enable the blower
         PRINT #1, "PLK"
         INPUT #1, DUMMY$

      END IF

      'If the fridge temp > normal maximum or blower speed > blower trigger threshold...
      IF FRIDGEMV >= FRIDGENORMALMAX OR BLOWERSPEED > BLOWERMAX THEN
       
         'set the compressor start request flag...
         STARTREQUEST = 1
      
      END IF
     
   '...otherwise, if running in starve mode...
   ELSE

      'If the core temp is higher than the fridge temp...
      IF COREMV > FRIDGEMV THEN

         'disable the blower
         PRINT #1, "PHK"
         INPUT #1, DUMMY$
        
      END IF
        
      'If the core is lower than the fridge temp...
      IF COREMV < FRIDGEMV THEN

         'enable the blower
         PRINT #1, "PLK"
         INPUT #1, DUMMY$
           
      END IF

      '...and fridge temp > emergent maximum...
      IF FRIDGEMV >= FRIDGEEMERGENTMAX THEN

         'set the start request flag...
         STARTREQUEST = 1

      END IF

       END IF

END IF



'Otherwise, If compressor is running...
IF COMPRESSOR = 1 THEN

   '...and running in normal mode...     
   IF STARVE = 0 THEN

      'turn blower turbo off
      PRINT #1, "PHL"
      INPUT #1, DUMMY$

      'and the fridge is below the blower disable threshold...
      IF FRIDGEMV < BLOWERDISABLETHRESHOLD THEN
        
         'disable the blower
         PRINT #1, "PHK"
         INPUT #1, DUMMY$
     
      END IF

      'and the fridge is above the blower disable threshold...
      IF FRIDGEMV > BLOWERDISABLETHRESHOLD THEN

         'enable the blower
         PRINT #1, "PLK"
         INPUT #1, DUMMY$

      END IF

      '...and fridge temp < normal minimum...
      IF FRIDGEMV <= FRIDGENORMALMIN THEN

         'stop the compressor...
         PRINT #1, "PLA"
         INPUT #1, DUMMY$
          
         'Set Comp Fan and Core Stir Shutdown Timer Triggers
         COMPFANSTOPTRIGGER = 1
         COREFANSTOPTRIGGER = 1

      END IF

   '...otherwise, if running in starve mode...
   ELSE

      'enable the blower
      PRINT #1, "PLK"
      INPUT #1, DUMMY$

      'turn blower turbo on
      PRINT #1, "PLL"
      INPUT #1, DUMMY$

      'If fridge temp < emergent minimum...
      IF FRIDGEMV <= FRIDGEEMERGENTMIN THEN

         'stop the compressor...
         PRINT #1, "PLA"
         INPUT #1, DUMMY$
          
         'Set Comp Fan and Core Stir Shutdown Timer Triggers
         COMPFANSTOPTRIGGER = 1
         COREFANSTOPTRIGGER = 1

      END IF

   END IF

END IF







'*** BEGIN Compressor START Module

'If there's a start request pending...
IF STARTREQUEST = 1 THEN
     
   'and the compressor isn't already running...
   IF COMPRESSOR = 0 THEN

      'and there's no active delay running...
      IF COMPRESSORDELAYRUNNING = 0 THEN

         'Initialize a delay
         COMPRESSORDELAYRUNNING = 1
         TIMERSTART = INT(TIMER)

      'if there's an active delay...
      ELSE
         'check the current timer against the starting time
         TIMERNOW = INT(TIMER)
           
         'check for midnight rollover
         IF TIMERNOW < TIMERSTART THEN TIMERSTART = TIMERNOW

         'if the delay timer has expired...
         IF TIMERNOW > TIMERSTART + COMPRESSORDELAY THEN
              
            'clear the start request...
            STARTREQUEST = 0
              
            'and the delay run flag...
            COMPRESSORDELAYRUNNING = 0
            
            'and start the compressor
            PRINT #1, "PHA"
            INPUT #1, DUMMY$

            'turn on the cooling fan
            PRINT #1, "PHD"
            INPUT #1, DUMMY$

            'Start core stir fan delay timer
            COREFANSTARTTRIGGER = 1

         'if the compressor start timer has NOT expired...
         ELSE
            'show how much time has elapsed
            SHOWDELAY = 1
           
             END IF

      END IF

   END IF

END IF

'*** END Compressor START Module




'*** BEGIN Core Stir Fan START Module

'If the fan start timer has been triggered...
IF COREFANSTARTTRIGGER = 1 THEN

   'Increment the fan timer...
   COREFANSTARTTIMER = COREFANSTARTTIMER + 1

   'If the timer has expired...
   IF COREFANSTARTTIMER = COREFANSTARTDELAY THEN

      'Turn on core stir fan
      PRINT #1, "PHG"
      INPUT #1, DUMMY$

      'Reset the timer counter
      COREFANSTARTTIMER = 0

      'Reset the timer trigger
      COREFANSTARTTRIGGER = 0

   END IF

END IF

'*** END Core Stir Fan START Module



'*** BEGIN Core Stir Fan STOP Module

'If the fan cutoff timer has been triggered...
IF COREFANSTOPTRIGGER = 1 THEN
     
   'increment the fan timer
   COREFANSTOPTIMER = COREFANSTOPTIMER + 1
     
   'if the fan timer has expired...
   IF COREFANSTOPTIMER > COREFANSTOPDELAY THEN

      'Turn off core stir fan
      PRINT #1, "PLG"
      INPUT #1, DUMMY$

      'reset the timer counter
      COREFANSTOPTIMER = 0
        
      'and clear the timer trigger
      COREFANSTOPTRIGGER = 0

   END IF

END IF

'*** BEGIN Core Stir Fan STOP Module



'*** BEGIN Compressor Cooling Fan STOP Module

'If the fan cutoff timer has been triggered...
IF COMPFANSTOPTRIGGER = 1 THEN
     
   'increment the fan timer
   COMPFANSTOPTIMER = COMPFANSTOPTIMER + 1
     
   'if the fan timer has expired...
   IF COMPFANSTOPTIMER > COMPFANSTOPDELAY THEN

      'turn off the cooling fan
      PRINT #1, "PLD"
      INPUT #1, DUMMY$

      'reset the timer counter
      COMPFANSTOPTIMER = 0
        
      'and clear the timer trigger
      COMPFANSTOPTRIGGER = 0

   END IF

END IF

'*** END Compressor Cooling Fan STOP Module



'*** BEGIN Fridge Heat Control Module

'* Heat On

'If the core is warmer than the max core threshold...
IF COREMV > COREHEATTHRESHOLD THEN

   'and the compressor is on...
   IF COMPRESSOR = 1 THEN

      'and the fridge is cooler than the minimum fridge temp...
      IF FRIDGEMV < FRIDGEHEATTHRESHOLD THEN

         'Turn the heater on
         PRINT #1, "PHI"
         INPUT #1, DUMMY$
             
      END IF

   END IF

END IF

'* END Heat On

'* BEGIN Heat Off

'If the core is colder than the min core temp, OR
'...the compressor is off, OR
'...the fridge is warmer than the heat threshold...
IF COREMV < COREHEATTHRESHOLD OR COMPRESSOR = 0 OR FRIDGEMV > FRIDGEHEATTHRESHOLD THEN

   'Turn the heater off
   PRINT #1, "PLI"
   INPUT #1, DUMMY$

END IF

'* END Heat Off

'*** END Fridge Heat Control Module



'***** END Decisions





'*** BEGIN Display
     
'* History Plot

   'Establish plot boundaries
   PSET (319, 0), 8   'Max
   PSET (319, 25), 8  'Center
   PSET (319, 51), 8  'Min
       
   LINE (0, 52)-(319, 52), 0 'clear gap between analog and digital
       
   PSET (319, 53), 8  'Track Top
   PSET (319, 65), 8  'Track Bottom

   'Erase the oldest plot column
   LINE (0, 0)-(0, 67), 0


   '* BEGIN Pens

   '*** THIS IS BEING LEFT FOR OBSERVATION PURPOSES ONLY ***
   'Compressor Temp Pen
   COMPRESSORTEMPPOS = 50 - INT((COMPRESSORMV - 800) / 32)
   PSET (319, COMPRESSORTEMPPOS + COMPRESSORPENOFFSET), 4

       
   'Blower Speed Pen
   BLOWERSPEEDPOS = 50 - (BLOWERSPEED / 120)
   IF BLOWERENABLED = 1 AND BLOWERTURBO = 1 THEN BLOWERPEN = 13
   IF BLOWERENABLED = 1 AND BLOWERTURBO = 0 THEN BLOWERPEN = 5
   IF BLOWERENABLED = 0 AND BLOWERTURBO = 0 THEN BLOWERPEN = 0
   IF BLOWERSPEED = 0 THEN
      PSET (319, 50), BLOWERPEN
   ELSE
      PSET (319, BLOWERSPEEDPOS + BLOWERPENOFFSET), BLOWERPEN
   END IF
     

   'Core Temp Pen
   CORETEMPPOS = 50 - INT((COREMV - 135) / 6.5)
   PSET (319, CORETEMPPOS + COREPENOFFSET), 11

       
   'Fridge Temp Pen
   FRIDGETEMPPOS = 50 - INT((FRIDGEMV - 400) / 4)
   PSET (319, FRIDGETEMPPOS), 9
       
       
   'Compressor State Pen
   PSET (319, 55), 9 * COMPRESSOR
       
       
   'Compressor Fan State Pen
   PSET (319, 57), 4 * COMPRESSORFAN
       
       
   'Core Stir Fan State Pen
   PSET (319, 59), 11 * COREFAN

       
   'Heater State
   PSET (319, 61), 12 * HEAT


   'Blower Pen
   PSET (319, 63), BLOWERPEN 'Color established in analog plot code


   'Hour Marker Pen
   SECOND = (INT(TIMER) MOD 3600)
   IF SECOND = 0 OR SECOND = 1 THEN PSET (319, 66), 15
       
   '* End Pens
       
       

   '* BEGIN Plot Manipulation

   'Increment the plot delay counter
   PLOTCOUNT = PLOTCOUNT + 1
       
   'If it's time to shift the plot...
   IF PLOTCOUNT = PLOTTRIGGER THEN
          
      'Grab it and shift it one px to the left
      GET (1, 0)-(319, 66), GFX
      PUT (1, 0), GFX
      PUT (0, 0), GFX
          
      'Reset the plot delay counter
      PLOTCOUNT = 0
          
      'Save the new historic plot to disk
      DEF SEG = &HA000
      BSAVE "C:\STEVE\PLOT.BIN", 0, 32768
           
      'Screen custodian :)
      LINE (0, 67)-(319, 199), 0, BF

   END IF

   '* End Plot Manipulation



   '* BEGIN Display text
       
   'If there's a compressor start pending...
   IF SHOWDELAY = 1 THEN
          
      'Display the progress
      LOCATE 11, 1: PRINT "COMPRESSOR REQUEST:"; TIMERNOW - TIMERSTART; "/"; COMPRESSORDELAY
      SHOWDELAY = 0
       
   'if not...
   ELSE
          
      'Clear the progress indicator off the screen
      LOCATE 11, 1: PRINT STRING$(75, 32)
       
   END IF

       
   'Core Temp and its heater threshold
   LOCATE 13, 1
      COLOR 11: PRINT "Core mV   :"; COREMV; "("; (((2 * COREMV) / 10) - 42); "F )";
   LOCATE 13, 28
      COLOR 12: PRINT " ["; COREHEATTHRESHOLD; "]"
       
       
   'Fridge Temp and its heater threshold
   LOCATE 14, 1
      COLOR 9: PRINT "Fridge mV :"; FRIDGEMV; "("; ((.1 * FRIDGEMV) - 15); "F )";
   LOCATE 14, 28
      COLOR 12: PRINT " ["; FRIDGEHEATTHRESHOLD; "]"
       
       
   '*** THIS IS BEING LEFT FOR OBSERVATION PURPOSES ONLY ***
   'Compressor Temp
   LOCATE 16, 1
      COLOR 4: PRINT "CompTemp mV:"; COMPRESSORMV
       
       
   'Blower Speed

   'BLOWERPEN color established in analog plot code above
   'If blower is enabled, set color to plot pen color
   IF BLOWERPEN <> 0 THEN
      BLOWERTEXT = BLOWERPEN
   'otherwise set to grey
   ELSE
      BLOWERTEXT = 8
   END IF
   LOCATE 18, 1
   COLOR BLOWERTEXT: PRINT "Blower Speed mV:"; BLOWERSPEED;
   LOCATE 18, 25
   COLOR 5: PRINT "["; BLOWERDISABLETHRESHOLD; "]"
       
   'DIO States
   LOCATE 20, 1
      COLOR 9: PRINT "CP:"; COMPRESSOR;
      COLOR 4: PRINT "CF:"; COMPRESSORFAN;
      COLOR 11: PRINT "CS:"; COREFAN;
      COLOR 12: PRINT "HT:"; HEAT;
      COLOR 5: PRINT "BE:"; BLOWERENABLED;
      COLOR 13: PRINT "BT:"; BLOWERTURBO;

   'Mode Display
   COLOR 8
       
   'If running in normal mode...
   IF STARVE = 0 THEN
          
      'Display the normal mode thresholds
      LOCATE 22, 1: PRINT "Normal Max (mV):"; FRIDGENORMALMAX; "("; ((.1 * FRIDGENORMALMAX) - 15); "F )"
      LOCATE 23, 1: PRINT "Normal Min (mV):"; FRIDGENORMALMIN; "("; ((.1 * FRIDGENORMALMIN) - 15); "F )"
       
   'If running in emergent mode...
   ELSE
          
      'Display the emergent mode thresholds
      LOCATE 22, 1: PRINT "Emergent Max (mV):"; FRIDGEEMERGENTMAX; "("; ((.1 * FRIDGEEMERGENTMAX) - 15); "F )"
      LOCATE 23, 1: PRINT "Emergent Min (mV):"; FRIDGEEMERGENTMIN; "("; ((.1 * FRIDGEEMERGENTMIN) - 15); "F )"
       
   END IF

       
   'Reset the default text color to hi-white
   COLOR 15

   '* End Display Text


'*** END Display


'Auto Timing Adjust (PLL)

'Get the latest timer value
NEWTIMERCAL = INT(TIMER * 100)

'Detect and deal with midnight rollover ... GRRRR
IF NEWTIMERCAL < OLDTIMERCAL THEN OLDTIMERCAL = NEWTIMERCAL - 100

'Rolling Average Window Size
COUNTLIMIT = 30

'If this is first run? O.o
IF OLDTIMERCAL = 0 THEN OLDTIMERCAL = INT(TIMER * 100) - 100

'Compact Averaging Algo
RXX = RAV * RCOUNT
RXX = RXX + (NEWTIMERCAL - OLDTIMERCAL)
RCOUNT = RCOUNT + 1

'With a "roller"...
IF RCOUNT > COUNTLIMIT THEN
   RXX = RXX - RAV
   RCOUNT = RCOUNT - 1
END IF

'Calculate the average
RAV = RXX / RCOUNT

'Calculate how far to adjust the master delay based on deviance from 100mS
DELAYMOVE = INT((RAV * 1000) - 100000)

'Make the adjustment if it's not dead on
IF RAV <> 100 THEN MASTERDELAY = MASTERDELAY - DELAYMOVE

'Set the old timer value for next pass to compare to
OLDTIMERCAL = NEWTIMERCAL

LOCATE 25, 1: COLOR 8: PRINT "Master Dly:"; MASTERDELAY; " "; DATE$; " "; TIME$;



'*** BEGIN Logging

'Open the log file for an append
OPEN "C:\STEVE\PLOT.LOG" FOR APPEND AS 2

'If this is a first pass, establish the constants in the log
IF LOGINITIALIZED = 0 THEN
       
   PRINT #2, "*"
   PRINT #2, "DATE", DATE$
   PRINT #2, "TIME", TIME$
   PRINT #2, "PLOTTRIGGER", PLOTTRIGGER
   PRINT #2, "FRIDGENORMALMAX", FRIDGENORMALMAX
   PRINT #2, "FRIDGENORMALMIN", FRIDGENORMALMIN
   PRINT #2, "FRIDGEEMERGENTMAX", FRIDGEEMERGENTMAX
   PRINT #2, "FRIDGEEMERGENTMIN", FRIDGEEMERGENTMIN
   PRINT #2, "COREHEATTHRESHOLD", COREHEATTHRESHOLD
   PRINT #2, "FRIDGEHEATTHRESHOLD", FRIDGEHEATTHRESHOLD
   PRINT #2, "POLLDELAY", POLLDELAY
   PRINT #2, "MASTERDELAY", MASTERDELAY
   PRINT #2, "COMPRESSORDELAY", COMPRESSORDELAY
   PRINT #2, "COMPFANSTOPDELAY", COMPFANSTOPDELAY
   PRINT #2, "COREFANSTARTDELAY", COREFANSTARTDELAY
   PRINT #2, "COREFANSTOPDELAY", COREFANSTOPDELAY

   'Set the log as being initialized with the constants
   LOGINITIALIZED = 1

END IF

'Increment the log delay counter
LOGCOUNT = LOGCOUNT + 1

'If the delay has expired...
IF LOGCOUNT = PLOTTRIGGER THEN

   'Write out the current variable states to the log
   PRINT #2, "!"
   PRINT #2, "STARVE", STARVE
   PRINT #2, "COMPRESSOR", COMPRESSOR
   PRINT #2, "COMPRESSORFAN", COMPRESSORFAN
   PRINT #2, "BLOWERSPEED", BLOWERSPEED
   PRINT #2, "BLOWERENABLED", BLOWERENABLED
   PRINT #2, "BLOWERTURBO", BLOWERTURBO
   PRINT #2, "COREMV", COREMV
   PRINT #2, "FRIDGEMV", FRIDGEMV
   PRINT #2, "HEAT", HEAT
   PRINT #2, "COREFAN", COREFAN
   PRINT #2, "STARTREQUEST", STARTREQUEST
   PRINT #2, "NORMALSTART", NORMALSTART
   PRINT #2, "EMERGENTSTART", EMERGENTSTART
   PRINT #2, "COMPRESSORDELAYRUNNING", COMPRESSORDELAYRUNNING
   PRINT #2, "TIMERSTART", TIMERSTART
   PRINT #2, "TIMERNOW", TIMERNOW
   PRINT #2, "COMPFANSTOPTRIGGER", COMPFANSTOPTRIGGER
   PRINT #2, "COMPFANSTOPTIMER", COMPFANSTOPTIMER
   PRINT #2, "COREFANSTARTTRIGGER", COREFANSTARTTRIGGER
   PRINT #2, "COREFANSTARTTIMER", COREFANSTARTTIMER
   PRINT #2, "COREFANSTOPTRIGGER", COREFANSTOPTRIGGER
   PRINT #2, "COREFANSTOPTIMER", COREFANSTOPTIMER
   PRINT #2, "MASTERDELAY", MASTERDELAY 'added for curiosity over time

   '*** THIS IS BEING LEFT FOR OBSERVATION PURPOSES ONLY ***
   PRINT #2, "COMPRESSORMV", COMPRESSORMV

   'Reset the log delay counter
   LOGCOUNT = 0
       
END IF

'Close the log file
CLOSE 2


'*** END Logging



'Erase a previous screen capture acknowledgement
IF SCREENCAPTURED = 1 THEN

   LOCATE 10, 13: COLOR 15: PRINT "               ";
   SCREENCAPTURED = 0

END IF
     


'*** BEGIN Key Trap and Master Delay

FOR DELAY = 1 TO MASTERDELAY
       
   'Read key buffer
   K$ = INKEY$
     
   '*** DEBUGGER FOR AVERAGES (PLL)
   IF K$ = "r" OR K$ = "R" THEN COUNT = 0: AV = 0

   'Quit if ESC is pressed
   IF K$ = CHR$(27) THEN 10000
       
   'Screen Capture
   IF K$ = "s" OR K$ = "S" THEN

      FILENAME$ = "C:\STEVE\" + MID$(DATE$, 1, 2) + MID$(DATE$, 4, 2) + MID$(TIME$, 1, 2) + MID$(TIME$, 4, 2) + ".BIN"
          
      DEF SEG = &HA000
      BSAVE FILENAME$, 0, 32768
          
      LOCATE 10, 13: COLOR 15: PRINT "Screen Captured";
      SCREENCAPTURED = 1

   END IF

   'User placed event marker/marker erase
   IF K$ = "m" OR K$ = "M" THEN

      'If a marker other than green is present (including no mark)
      IF POINT(319, 54) <> 2 THEN

         'Draw a green marker
         LINE (319, 0)-(319, 64), 2

      'Otherwise, it must be green
      ELSE
         'so get rid of it
         LINE (319, 0)-(319, 64), 0

      END IF

   END IF


'********* REDO AS PIN STATE *********

   'Mode Toggle (Normal/Emergent)
   IF K$ = "*" THEN
          
         'toggle normal/emergent mode...
         IF STARVE = 0 THEN STARVE = 1 ELSE STARVE = 0

      IF STARVE = 0 THEN
          
         'mark plot as being in normal mode
         LINE (319, 0)-(319, 64), 14
             
      ELSE

         'mark plot as being in emergent mode
         LINE (319, 0)-(319, 64), 6
          
      END IF
       
   END IF

'********** END REDO MODE CHANGE BY PIN STATE


'*** END Key Trap and Master Delay
NEXT DELAY



GOTO 1000 'END OF MASTER COMM LOOP



'Error Trap
5000 RESUME 5001
5001 ERRORSTATE = 1



10000 'Master Loop Abandon

'Save the Historic Plot
DEF SEG = &HA000
BSAVE "C:\STEVE\PLOT.BIN", 0, 32768


'Turn off LED
PRINT #1, "PLN" 'LED OFF
INPUT #1, DUMMY$


'Write the config file
OPEN "C:\STEVE\FRIDGE.CFG" FOR OUTPUT AS 2
PRINT #2, STARVE
PRINT #2, ERRORSTATE
PRINT #2, MASTERDELAY

'Close COM port, config file, and anything else that hopefully isnt open :)
CLOSE


'If there was an error, restart from scratch to attempt recovery
IF ERRORSTATE = 1 THEN RUN


'Fell thru this far, someone must have hit ESC... It's over. :)
COLOR 15
END




'NOTES:

' READS:
' [BOARD] R [INPUT]
' REPLIES:
' DIO: [BOARD] [INPUT] H/L
' ANA: [BOARD] mV
Wanted: Schrödinger's cat, dead and alive.