'd. bodnar 9-17-06 symbol relay1 = 0 symbol relay2 = 1 symbol relay3 = 2 symbol sensor1 = pin5 symbol sensor2 = pin6 symbol sensor3 = pin7 symbol EnterSw = pin2 'pin 13 on PICAXE - 1 when out, 0 when pushed symbol MenuSw = pin3 'pin 14 on PICAXE - 1 when out, 0 when pushed symbol EndSwitch = pin4 'pin 15 on PICAXE - 1 when out, 0 when pushed symbol swmotor1 = 3 symbol swmotor2 = 4 symbol swdir1 = 5 'h-bridge symbol swdir2 = 6 'h-bridge symbol nextloop = b4 symbol temp0 = b3 symbol temp1 = b5 symbol temp2 = b2 symbol temp3 = b6 symbol tempCL = b7 'temp storage of Current Loop Engine symbol Laps = w6 symbol EndFlag = bit8 symbol SwOneFlag = bit9 symbol SwTwoFlag = bit10 symbol SwThreeFlag = bit11 symbol versionW = 3 symbol versionD = 6 symbol dlay = 120 symbol SensorValue = b8 '= %01100000 ' %11100000 for 3 trains and %01100000 for 2 trains symbol PinsByte = b0 'using b0 allows us to access each sensor bit with bit5, bit6 & bit7 NextLoop = 0 'use 0-2 in stead of 1-3 for branch statement 'NOTE SensorValue must be set on first run with MENU!!!!!!!!!!!!!!!!!!! read 0, SensorValue if SensorValue <>%11100000 and SensorValue<>%01100000 then fixSensorValue goto NoNeedtoFix fixSensorValue: SensorValue=%11100000 'set to 3 trains if read is bad NoNeedtoFix: pause 5000 'pause while LCD wakes up and does test display if SensorValue=%11100000 then engines3a temp0=2:goto skipover1a: engines3a: temp0=3 SkipOver1a: serout 7, t2400,("?f?c0"):pause 200:serout 7,t2400,("?f?c0"):pause 200 '?c0 turns cursor off serout 7, t2400,("?Bff"):pause 200 serout 7,T2400,("Switch Controller?nd. bodnar 9-2006?nVersion ",#versionW,".",#versionD," Trains=",#temp0) pause 2000 'pause while LCD wakes up and does test display serout 7, T2400,("?x00?y3 MENU FOR SETTINGS")

Check4MenuButton: for temp1=1 to 100 if MenuSw=0 then goto MENU pause 30 next temp1 serout 7, T2400,("?fResetting Switches") gosub SwitchesThree:gosub SwitchesTwo:gosub SwitchesOne ' set for initial run serout 7, T2400,("?f") Start: PWMOut 1, 25, 52 ' 38.4 kHz to IR emitters temp1=pins & SensorValue PWMOut 1,0,0 if temp1 <> SensorValue then ShowError low relay1:low relay2:low relay3 goto start1: ShowError: serout 7,t2400,("?x00?y0Train1= ") temp0=temp1 & %00100000 if temp0=0 then closed1 open1: low relay1 'block off serout 7,T2400,("OK "):goto show2 closed1:serout 7,T2400,("open") if SwOneFlag=1 then SkipOver1C gosub SwitchesOne 'switch set for train 1 SkipOver1C: high relay1:low relay2:low relay3 'block on/others off goto Start:'recheck show2: serout 7,t2400,("?x00?y1Train2= ") temp0= temp1 & %01000000 if temp0=0 then closed2 open2: low relay2 serout 7,T2400,("OK "):goto show3 closed2:serout 7,T2400,("open") if SwTwoFlag=1 then SkipOver2C gosub SwitchesTwo SkipOver2C: high relay2:low relay1:low relay3: goto Start: show3: if SensorValue=%01100000 then done: serout 7,t2400,("?x00?y2Train3= ") temp0=temp1 & %10000000 if temp0=0 then closed3 open3: low relay3 serout 7,T2400,("OK "):goto done closed3:serout 7,T2400,("open") if SwThreeFlag=1 then SkipOver3C gosub SwitchesThree SkipOver3C: high relay3:low relay1:low relay2 goto start: done: low relay1:low relay2:low relay3 pause 500 goto start: Start1: endflag=0 serout 7,T2400,("?fAll Trains Ready!?n?nPress ENTER?nto START") loop: if endswitch=1 and EnterSw =1 then loop: serout 7,T2400,("?f") Start2: if endflag=1 then Start1 Branch NextLoop, (one, two, three) serout 7,T2400,("?fnextloop= ",#nextloop):serout 7,T2400,("?nERROR FIX ?nPausing 60 seconds"):pause 60000 nextloop=0:goto start2 'error trap - sometimes hung here end one: gosub SwitchesOne high NextLoop 'start train on block # NextLoop +1 goto AddOneToNextLoop two: gosub SwitchesTwo high NextLoop 'start train on block # NextLoop +1 goto AddOneToNextLoop three: gosub SwitchesThree: high NextLoop 'start train on block # NextLoop +1 goto AddOneToNextLoop AddOneToNextLoop: Laps=Laps+1 TempCL=NextLoop 'stores Train Running for possible nudge adjust below NextLoop=NextLoop + 1 serout 7,T2400,("?x00?y0Train Out=", #NextLoop, " Lap=",#Laps) serout 7,T2400,("?x00?y1 ?x00?y2 ") 'clear lines 2 & 3 if nextloop= 2 and SensorValue = %01100000 then FixIt if NextLoop = 3 and SensorValue=%11100000 then FixIt goto Wait2Clear FixIt: temp3=NextLoop 'store for use in Wait2Clear routine NextLoop=0 goto Wait2Clear Wait2Clear: ' wait for train to clear sensor 'may need to add a pause, too (see next note) for temp2=1 to 255 'Was 100 - increased to insure that a car with big breaks won't show end at low speed PWMOut 1, 25, 52 ' 38.4 kHz to IR emitters temp1=pins & SensorValue PWMOut 1, 0,0 if temp1 = SensorValue then Wait2Clear next temp2 temp3=tempCL+1 serout 7,T2400,("?x00?y1Sensor ",#temp3," Cleared") 'wait for train to return to end of block and block sensor serout 7,T2400,("?x00?y2Waiting for Return ") Wait4Return: gosub CheckEndSwitch: gosub CheckForMultipleSensorsOpen: 'if endflag=1 then Start: PWMOut 1, 25, 52 ' 38.4 kHz to IR emitters temp1=pins & SensorValue PWMOut 1, 0,0 if temp1 <> SensorValue then Wait4Return low relay1:low relay2:low relay3 DoubleCheck: pause 1000 PWMOut 1, 25, 52 ' 38.4 kHz to IR emitters temp1=pins & SensorValue PWMOut 1, 0,0 if temp1 <> SensorValue then NudgeForward goto start2: serout 7,T2400,("?x00?y2Nudging... ") NudgeForward: high tempCL pause 300:low tempCL goto DoubleCheck SwitchesOne: SwOneFlag=1:SwTwoFlag=0:SwThreeFlag=0 high swdir1:low swdir2 high swmotor1:pause dlay low swmotor1 serout 7,T2400,("?x00?y3Sw1=Strt") high swdir1:low swdir2 high swmotor2:pause dlay low swmotor2 serout 7,T2400,("?x11?y3Sw2=Strt") return SwitchesTwo: SwOneFlag=0:SwTwoFlag=1:SwThreeFlag=0 high swdir2:low swdir1 high swmotor1:pause dlay low swmotor1 serout 7,T2400,("?x00?y3Sw1=Turn") high swdir1:low swdir2 high swmotor2:pause dlay low swmotor2 serout 7,T2400,("?x11?y3Sw2=Strt") return SwitchesThree: SwOneFlag=0:SwTwoFlag=0:SwThreeFlag=1 high swdir2:low swdir1 high swmotor2:pause dlay low swmotor2 serout 7,T2400,("?x11?y3Sw2=Turn") return CheckEndSwitch: if endswitch=0 or MenuSW=0 and EnterSW=0 then setendflag return setendflag: endflag=1 serout 7,T2400,("?fEnd Flag Set?n STOPPING?n at End of Lap") return CheckForMultipleSensorsOpen: for temp0=1 to 5 'check 5 times to make sure reading is valid PWMOut 1, 25, 52 ' 38.4 kHz to IR emitters temp1=pins & SensorValue PWMOut 1, 0,0 if SensorValue=%11100000 and temp1<>0 and temp1<>32 and temp1<>64 and temp1<>128 then NoBadSensors: if SensorValue=%01100000 and temp1<>0 then NoBadSensors: next temp0 BadSensor: serout 7,T2400,("?fBAD SENSOR?n STOPPING?n ALL") low relay1:low relay2:low relay3 Pause 3000 goto start NoBadSensors: return MENU: serout 7,T2400,("?f MENU") pause 1000 if SensorValue=%11100000 then engines3 temp0=2:goto skipover1: engines3: temp0=3 skipover1: serout 7,T2400,("?f# of Engines Now = ",#temp0) serout 7,T2400,("?nENTER to Change?nMENU to Skip") CheckSwitch1: if MenuSW=0 then skipover2: if EnterSW=0 then ChangeEngineNumber goto CheckSwitch1 ChangeEngineNumber: if SensorValue=%11100000 then EnginesTo2: EnginesTo3: SensorValue=%11100000:write 0,SensorValue serout 7,T2400,("?fNEW VALUE WRITTEN"):pause 1000 goto MENU: EnginesTo2: SensorValue=%01100000:write 0,SensorValue serout 7,T2400,("?fNEW VALUE WRITTEN"):pause 1000 goto MENU SkipOver2: goto start