Nitrox Stick Controller
I finally have it working! Thanks so much for all the help from this forum. It has been quite a learning experience and I'm sure I would not have gotten to this point without the help I received here.


' {$STAMP BS2}
' {$PBASIC 2.5}
' Declarations
Baud CON 32
NeedleStep CON 10
Compressor PIN 0
MixDown PIN 1
MixUp PIN 2
AdcCs PIN 3
AdcClk PIN 4
AdcIo PIN 5
Text PIN 6
Solenoid PIN 7
Green PIN 8
Red PIN 9
Servo PIN 10
Index VAR Nib
Counter VAR Nib
Counter2 VAR Nib
AdcIn VAR Nib
Run VAR Nib
ButtonSpace VAR Byte
Remainder VAR Word
CurrentMix VAR Word
CurrentMixFrac VAR Word
MixSetting VAR Word
AdcOut VAR Word
Calibrate VAR Word
CalibrateFrac VAR Word
Average VAR Word
NeedleSet VAR Word
Initilization:
HIGH Green
LOW Red
SEROUT Text, Baud, [22, 17]
PAUSE 500
SEROUT Text, Baud, [22, 17, 12, " Startup"]
PAUSE 500
INPUT Compressor
INPUT Mixup
INPUT MixDown
READ 0, Word MixSetting
LOOKDOWN MixSetting, [210, 260, 270, 280, 290, 300, 310, 320, 330, 340, 350, 360], Index
LOOKUP Index, [1020, 980, 940, 900, 860, 820, 780, 740, 700, 660, 620, 580], NeedleSet
AdcIn = AdcIn | %1011
GOSUB ServoSet
SEROUT Text, Baud, [12, " Ready"]
DO UNTIL Compressor = 1
LOOP
Calibration:
LOW Red
FOR Counter = 1 TO 10
HIGH Green
SEROUT Text, Baud, [12, "Calibrating...", DEC2 Counter]
GOSUB ReadAdc
PAUSE 500
LOW Green
PAUSE 500
Average = Average + AdcOut
NEXT
AdcOut = Average / 10
Average = 0
Calibrate = AdcOut * 10 / 209
Remainder = AdcOut * 10 // 209
CalibrateFrac = Remainder * 10 / 209
Calibrate = (Calibrate * 10) + CalibrateFrac
GOSUB CalcMix
GOTO CalConfirm
Main:
IF MixSetting > 210 THEN HIGH Solenoid
DO
LOW Red
HIGH Green
FOR Counter = 0 TO 15
GOSUB SysCheck
BUTTON MixUp, 1, 255, 255, ButtonSpace, 1, SetMixUp
BUTTON MixDown, 1, 255, 255, ButtonSpace, 1, SetMixDown
GOSUB CalcMix
Average = Average + CurrentMix
NEXT
Average = Average / 16
IF ABS(MixSetting - Average) > 3 THEN GOSUB AdjustMix
Average = 0
GOSUB Display
LOOP
SysCheck:
FOR Counter2 = 1 TO 10
Run = Run + Compressor
NEXT
IF Run = 0 THEN ShutDown
Run = 0
IF CurrentMix > 380 THEN ShutDown
RETURN
SetMixUp:
Index = Index + 1
LOOKUP Index, [210, 260, 270, 280, 290, 300, 310, 320, 330, 340, 350, 360], MixSetting
WRITE 0, Word MixSetting
IF MixSetting > 210 THEN HIGH Solenoid
GOSUB Display
PAUSE 300
GOTO Main
SetMixDown:
Index = Index - 1
LOOKUP Index, [210, 260, 270, 280, 290, 300, 310, 320, 330, 340, 350, 360], MixSetting
WRITE 0, Word MixSetting
IF MixSetting = 210 THEN LOW Solenoid
GOSUB Display
PAUSE 300
GOTO Main
CalcMix:
GOSUB ReadAdc
CurrentMix = AdcOut * 10 / Calibrate
Remainder = AdcOut * 10 // Calibrate
CurrentMixFrac = Remainder * 10 / Calibrate
CurrentMix = (Currentmix * 10) + CurrentMixFrac
RETURN
AdjustMix:
LOW green
IF CurrentMix < MixSetting THEN NeedleSet = NeedleSet - NeedleStep ELSE NeedleSet = NeedleSet + NeedleStep
IF NeedleSet < 460 THEN GOSUB O2Increase
IF NeedleSet > 1100 THEN GOSUB O2Decrease
FOR Counter2 = 1 TO 2
PULSOUT Servo, NeedleSet
PAUSE 250
NEXT
RETURN
Display:
SEROUT Text, Baud, [12, "Set = ", DEC1 MixSetting DIG 2, DEC1 MixSetting DIG 1, ".", DEC1 MixSetting DIG 0, "%"]
SEROUT Text, Baud, ["Current = ", DEC1 CurrentMix DIG 2, DEC1 CurrentMix DIG 1, ".", DEC1 CurrentMix DIG 0, "%"]
RETURN
ReadAdc:
LOW AdcCs
SHIFTOUT AdcIo,AdcClk,LSBFIRST,[AdcIn\4]
SHIFTIN AdcIo,AdcClk,MSBPOST,[AdcOut\12]
HIGH AdcCs
RETURN
ShutDown:
LOW Solenoid
HIGH Red
LOW Green
DO
SEROUT Text, Baud, [12, " OXYGEN SHUT OFF"]
PAUSE 1000
SEROUT Text, Baud, [12]
PAUSE 1000
LOOP
O2Increase:
NeedleSet = 460
LOW Green
HIGH Red
SEROUT Text, Baud, [12, "INCREASE OXYGEN PRESSURE"]
PAUSE 1000
LOW Red
HIGH Green
RETURN
O2Decrease:
NeedleSet = 1100
LOW Green
HIGH Red
SEROUT Text, Baud, [12, "DECREASE OXYGEN PRESSURE"]
PAUSE 1000
LOW Red
HIGH Green
RETURN
ServoSet:
FOR Counter = 0 TO 15
PULSOUT Servo, NeedleSet
PAUSE 250
NEXT
RETURN
CalConfirm:
SEROUT Text, Baud, [12, "Confirm : ", DEC1 CurrentMix DIG 2, DEC1 CurrentMix DIG 1, ".", DEC1 CurrentMix DIG 0, "%"]
SEROUT Text, Baud, ["Up/YES Down/NO"]
DO
BUTTON MixUp, 1, 255, 255, ButtonSpace, 1, Confirmed
BUTTON MixDown, 1, 255, 255, ButtonSpace, 1, Calibration
LOOP
Confirmed:
PAUSE 300
GOTO Main

Comments
No breather for me Hulk. Too much money in those.
:thumb:
Feel free to critique (anyone). I'm sure it could be better.
Gut shot:
Front panel:
System: