Automatic Switch Project

Revised 09-4-06


Tuesday, August 22 Monday, August 28

Saturday, September 2

Wednesday, August 23

Tuesday, August 29

Sunday, September 3

Thursday, August 24 Wednesday, August 30 Monday, September 4
Friday, August 25 Thursday, August 31  
Sunday, August 27 Friday, September 1  



The objective of this controller project is to have automatic block and switch control of five separate engines that run on two completely independent loops of track.  Part of each loop is outside (public) and part is inside (hidden from public view).  The inside part has switch controlled sidings where the idle trains wait.  The inner loop can accommodate three trains and the outer two trains.

When a train completes its outside run and enters the inside area it is automatically assigned to an empty siding.  It enters the siding and, when it reaches an optical sensor, power is removed from that block.  One of the idle trains is activated and moves onto the outside area of the layout. 

This process continues alternating trains so that two trains are outside (one on each of the loops) while the other trains wait inside.

Outside (public area) Drawing
The first drawing below shows the outside (public) part of the layout.  Note that it will be revised to show two loops with one inside of the other, not one above the other as it is now depicted. 
Inside (private area) Drawing
This drawing shows the inside part of the layout where the switches and sidings are located.
There are seven switches in the drawing below.  The switch that is labeled "3" is not automatically controlled as it is for a storage siding.
All trains will go through the two loops in a counter clockwise direction, entering at either "one-in" or "two-in".  This means that control is only needed for switches 1, 4, and 6.  Switches 1 and 4 control the inner loop and switch 6 controls the outer loop.  Optical position sensors are installed at the points on the track labeled a, b, c, d, and e.  The sections of track between a controlled switch and an optical sensor (between switch 4 and sensor a for example) are isolated from the main track power and make up a block where power can be removed so that the train stops and waits there.
  • Two microcontrollers - Programmable PIC processors monitor all sensors and control the switch positions and block power.
  • Two 4 line by 20 character LCD displays to facilitate program changes and to monitor train position and operation status
  • 5 Optical Sensors - These are made up of an infrared LED that is placed on one side of the track and an IR detector that is placed across from it.  When the train breaks the IR beam a data pulse is generated.  The IR LEDs are modulated at 38 kHz to prevent false readings.  The IR detector (part #PNA4602M) only reacts to IR at this frequency.
  • 5 block controls - These are likely to be relays that will completely remove power from a block when occupancy is detected.  This will bring the train to a sudden stop.  To make the stop more gradual a series of diodes can be inserted in the track sections in the block to more gradually slow the train.  The other option is to use a separate microcontroller to provide power to each block.  This would allow complete acceleration / deceleration control but would add significantly to the cost and complexity of the system.
  • 5 Switch controls - The microcontrollers will activate switches  based on the position of the trains.  The exact electronics to be used will be determined after some experimentation.  Options include:  relays, four Mosfets or Darlington transistors per switch, or one sn754410ne per switch
Logic - outer loop
The outer loop (labeled "one-in" through "one-out" in the second drawing) is made up of a manual siding (controlled by switch 3) and two sidings controlled by switch 6.
  1. Operation begins with a train on the siding between switch 6 and sensor d (let's call that siding "6d") and another train on siding 6e.
  2. Both blocks are unpowered.  When the microcontroller system is activated track power, which is controlled by an independent radio controlled system, is activated so that the train on 6d can be operated. This train moves out of the siding and exits on "one-out"
  3. Switch 6 is set to return this train to the same siding when it reenters on "one-in"
  4. When it reaches sensor d power is removed from the siding and the train stops.
  5. The second train is activated when power is applied to block 6e.  It exits on "one-out" and the process continues alternating trains.
Logic - inner loop
The inner loop (labeled "two-in" through "two-out" in the second drawing) is made up of three sidings controlled by switches 1 and 4.
  1. Operation begins with trains on all three sidings with one between switch 4 and sensor a, one between 4 and b and the last between 1 and c.
  2. All three blocks are unpowered.  When the microcontroller system is activated track power, which is controlled by an independent radio controlled system, is activated so that the train on 4a can be operated. This train moves out of the siding and exits on "two-out"
  3. Switches 1 and 4 are set to return this train to the same siding when it reenters on "two-in"
  4. When it reaches sensor a power is removed from the siding and the train stops
  5. The second train is activated when power is applied to block 4b.  It exits on "two-out"
  6. When this train reenters on "two-in" it returns to its original siding
  7. The second train, on 4b, exits and runs outside.  When it returns it stops on the same siding and train 1c is activated.
  8. The process continues alternating trains.
  9. Random selection of the three trains can be added to the software so that the same sequence of trains is not always used.
The schematic below (click the thumbnail to see the full sized image) shows the system that will control one of the loops.  A similar unit will control the second loop.  Both units will be able to control three sidings by observing the status of three optical sensors and controlling two switches.
The hardware that will be needed to activate the switches is not shown.  The three relays are used to activate or deactivate the blocks past each switch.
Other considerations
  1. Track insulators will need to be installed at both ends of each of the sidings where trains will be waiting to move into the public part of the layout.  there are two on the outside loop and three on the inside loop.
  2. Power will need to be supplied from an independent source to control the switches and to power the electronics.  This will be DC, 8 amps or more, 20 volts or more.  An additional "wall wart" power supply will be needed for each controller (9 volts DC)
  3. Wiring will pass under the platform to the control system
  4. Two completely independent control units will be used.  One to operate the inner loop, one to operate the outer loop.
  5. IR emitter / detector pairs will be placed at the end of each block according to the next illustration.  They will extend approximately 2" above the platform.
  6. We may need to consider using a more robust switch controller such as the Tortoise Slow Motion Switch Machine if the LGB units are not up to the challenge of being continuously switched.

Cost Estimate
  1. Design - 3 hours ($ per  hour)
  2. Hardware - two controllers ($ each), 5 sets of Infrared sensors ($ each), 5 relays to control blocks ($ each), DC power supply ($)
  3. Programming - 3 hours ($ per hour)
  4. Installation & testing - 3 hours ($ per hour + $ travel)

Total Estimate: $

Other costs (optional):

  1. 5 Tortoise Slow Motion Switch Machines
  2. Custom programming - as needed at $ per hour


Please note:  The cost estimate is for a completely custom design and reflects only part of the actual cost of developing such a prototype unit.  The actual time to construct, program and test this unit is likely to exceed 30 hours as a prototype railroad will need to be built (HO scale) for testing.  A reduced cost estimate is given here as the development of this device may lead to other sales and / or opportunities for publication in model railroad magazines.



Tuesday, August 22

Built prototype board using a PICAXE 28X microcontroller, 3 SPDT high current relays (Omron, 16 amp / 30 volt DC), H-Bridge (TI - SN754410NE), 5 volt filtered power supply.

In this top view the microcontroller is at the bottom next to the H-Bridge.  The three relays are together in the upper right.  The power supply is to the left.  The connection block in the far upper right (4 screws) connects the 3 relays to the 3 independent blocks of insulated track.  The connector in the lower right goes to the two LGB switch motors


This is a view of the back of the hand wired prototype board.  This will all be replaced with a custom printed circuit board.

Wednesday, August 23

Completed installation and testing of one of three infrared emitter detector pairs.

In the photo below the back of the IR detector is shown at the top.  The small clear LED lights when the emitter and detector pair are NOT properly aligned and goes out when they are aligned.

A four conductor cable goes to each pair.  This provides +5 volts, ground, 38 KHz for the LED and an input for the controller that goes high when the detector sees the IR from the emitter.

The schematic was revised to include the IR pairs and H-Bridge.  The LCD display was removed as this will be on a separate circuit board.


The main task of the day was the design of the printed circuit board.  This was completed and the drawing was sent off to Oregon to be fabricated.  Delivery is scheduled for Monday.

The red layer shows traces that are on the top of the board while green shows traces on the bottom.  The yellow layer will not be visible on the finished boards.


1.  Two boards will be built.  Even though one of the controlled loops has 3 sidings & 3 trains while the other has only 2 both boards will be identical and capable of operating 3 trains.  An option in the software setup will determine the number of trains to be run.  Using identical boards will facilitate troubleshooting and repair as parts can easily be swapped to determine the root of a problem.
2.  The prototype board has the 3 block control relays on the same board with the H-Bridge and the microcontroller.  The finished system will have the relays located on a separate board.  There will be 5 wires connecting the two boards (+12 volts, ground and three control leads that will connect to the base lead on the transistors that activate the relays.)
3.  A separate LCD controller will operate the LCD display unit.  Data will be transferred via a two wire serial connection.
4.  Program modifications will be by means of a program switch and either a potentiometer or an additional switch.
5.  The IR emitter / detector pairs will be connected to the main circuit board through a 6 conductor cable that terminates in an RJ-12 plug / jack pair.  Near the detectors this cable will be separated to provide a connection to each of the three IR units.  Each unit will have a 4 conductor cable going to it with +5 volts, ground, 38 KHz pulses for the LED and an input line (the only unique connection to each unit) that goes from the sensor to the main board.
6.  Each block (siding) will be insulated at both ends on only one rail.  One insulator will be bypassed by the relay that controls it.  When the relay is open  the block will not have power.  One wire from the constantly "hot" part of the loop goes to the common contact of each relay.  The normally open contact from each relay goes to the block that it controls.

Thursday, August 24

The three sets of IR emitter / detector sensors were completed and tested.

Track was laid out with three separate sidings that all met and fed the same main loop.  Insulators were placed to isolate blocks in each siding.  Powered LGB switches were placed on the left side of the layout (the trains will always run in a counter clockwise direction) and unpowered switches were placed on the right.   

Three small engines are running in the two photos.  A PRR switcher pulling a log car on the smallest loop, a Hartland railcar on the middle one (it is blurred in the photo below as it is moving as it approaches the IR sensors) and an Eggliner pulling a gondola on the longest loop..


Some experimentation showed that the LGB powered switches didn't work well when cars and engines relied on the internal springs in a non-powered mode - long rubber bands were used to return the switches once a train passed. 

The reset of the day was spent on software development.  The latest version of the software for the PICAXE 28X is at the very bottom..

Friday, August 25

The software matured to the point that it did what was intended.  The next set of improvements will involve anticipating and dealing with unusual situations (where a train may not return to the loop in a set time or if one is removed from a siding unexpectedly)

The photo below shows the Hartland car going between a set of sensors.

Some configuration will be done through an AristoCraft accessory receiver.  This will allow a function button to stop all trains after a run is completed.

The system was tested all afternoon and no glitches were observed.  The image below is of the display - note that it shows 1117 laps.  The actual number was in excess of 1200 but I got tired of taking pictures.

The serial LCD microcontroller arrived and was interfaced to the PICAXE 28X.  The software was modified to use the new control codes and tested successfully

Sunday, August 27

Switches were added to the relay outputs to allow you to manually "nudge" the engines in the blocks even if the power to the blocks are off.  Three switches allow control of the three blocks.

Diodes were placed in series with the power that goes to the blocks.  Each of the diodes drops the voltage in the blocks by 0.7 volts.  An array of switches and diodes will be installed so that you can use one or more of three switches to add 0, 1, 2, 3, 4, 5, 6 or 7 diodes.  A binary grouping has the first switch add one diode, the second switch adds two and the third switch adds four.

Both of the above changes are detailed in the schematic below.  Please click to enlarge.

The software was modified to make sure that an engine was sitting directly in front of a sensor when power is removed from a block.  This is to resolve a problem where a sensor is blocked by the front of a rail car, the power is removed from the block and the car rolls to a place where the beam goes through an opening in the car (or on a train between cars).  The new routine will check the sensor in a loop and nudge the engine should the beam not be blocked after the power is removed and it pauses for a second or so.



Monday, August 28

The custom circuit boards arrived.  All three of the boards were populated and tested successfully!  The only error on the boards was a connection to the block control / relay board to +5 volts rather than to +12 volts.  This was easily corrected.

Tuesday, August 29

The next major project was to complete the design and construction of the relay / block control  boards.  These boards are interfaced to the main processor board by a five conductor cable that delivers +12 volts for the relays, ground and three control signals that activate any of the three block control relays.

In addition to the three relays there are seven diodes is three groups, 1, 2 and 4 diodes each.  SPDT toggle switches can put any, all or none of the diodes in series with the track voltage, dropping the voltage to the blocks up to nearly 5 volts in .7 volt increments.

Here you see the three relays and the 4 pole header that will connect to the blocks.  Also note the marks on the  board in the upper left corner.  They indicate where the voltage control diodes will be placed.

Here the same board is seen from the back with the header and relays soldered in.  The holes for the 3 amp diodes have been drilled out so that their larger leads will fit.

The diode leads are bent so that they can be inserted vertically.

The diodes have been inserted in the board so that they can be wired in series.

Here is a view of the diodes as soldered to the back.

Each relay is connected to a diode, to protect the circuit from voltage surges when it is in operation, and a transistor that is used to activate it.  The orientation of the diodes and transistors is critical!

The back of the board shows where some foil needs to be removed to protect against short circuits where the diodes connect to the relays.


The additional parts shown here are the transistors and the resistors that connect to their bases.


Wednesday, August 30

The main objective today was to place the LCD display unit, main microcontroller board, relay/block control board and the various switches and buttons in a Plexiglas housing.  The mounting material is 1/4" clear Plexiglas.

The photos below are of a complete unit (front and back) as it is being tested.



Thursday, August 31

Three duplicate unit were made up with two controlling the layout and one spare.

Switches were added for Menu and Enter (upper right in the photo above.)  These will be used to make program changes to the unit without the need to use the PICAXE programmer.

Each of the three control units were tested.

Friday, September 1

The hardware platform was fine tuned a bit by reinforcing some connections and supporting boards with standoffs.  Most of the day was spent working on software so that it additional features could be incorporated.  These include:

  1. adding a menu to set variables
  2. giving more detailed information about the operation of the trains on the display
  3. selecting either two or three trains on a loop
  4. setting delays
  5. adding a switch to end a run after all trains enter the station
Saturday, September 2

Additional progress was made in fine tuning and adding features to the software.  A basic menu was added to allow the end user to select either two or three train operation. 

Routines were added to help align engines with sensors and to bring the running of trains to a stop when both the MENU and ENTER buttons were pressed at the same time.

Sunday, September 3

The AristoCraft accessory receiver was interfaced to the circuit board (pin 15).  This allows the remote control buttons "D" and "E" to shut down the train's runs when all trains are back at the station.  In order to make the connection a 100K resistor was run between pin 15 and +5volts.  One of the receiver's leads was run to ground and the other to pin 15.

The original "nudge" buttons were replaced by ones that can handle 1.5 amps.  The original buttons were likely to fail after continued use.

Monday, September 4

Testing of the two main units and the backup continued.  Labels were applied to help with setup and use.  Additional work was done on the user manual (see below)


User Manual

Hardware Installation:

  1. Connect switches (one for two train system or two for three trains) to the four contact connector on the microcontroller circuit board.  Switch 1 connects to contacts 1 & 2.  Switch 2 goes to contacts 3 & 4. 
  2. Switch 1 controls the most distant loop.  Switch 2 controls the inner loop.
  3. Connect the block power cables to the four contact connector on the relay board. 
  4.  Terminal 1 connects to track power on the same rail that is cut on each block.  Terminal 2 goes to the bock on the longest loop, terminal 3 to the middle loop and terminal 4 to the smallest loop.  On a two train system only terminals 1, 2 and 3 are used.
  5. Place the sensors approximately 18" before the end of each block.  Sensor 1 (the one closest to the RJ-12) goes to the most distant loop.  Sensor 2 to the middle loop and sensor 3 to the inner loop (if used)
  6. Plug the sensor cable (RJ-12 telephone connector) into the matching socked on the microprocessor circuit board
  7. Place a train on each of the block controlled sections just short of the sensors, but not up to them.
  8. Connect the AristoCraft receiver "Sound1" cable to one of the circuit boards and the "Sound 2" to the other.  Observe polarity by keeping like colored wires together.
  9. Connect power to the circuit board.  The LCD display will light and display test information. 

Software Setup:

  1. When power is applied press "MENU" when the copyright screen appears
  2. The number of trains that will be controlled is displayed.
  3. To change it from 2 to 3 or from 3 to 2 press the "ENTER" button
  4. Press "MENU" when done
  5. The unit will test each of the sensors.  If a train blocks a sensor the sensor number and "OK" will be displayed.  If a sensor is open the sensor number and "Open" will be displayed.  Apply track power.  Briefly press the appropriate "nudge" button to move an engine forward so that it blocks its sensor.  When all of the sensors are blocked "TRAINS READY - PRESS ENTER TO START" will be displayed.
  6. Press "ENTER" and the first train will depart.  When it returns and blocks its sensor again the second train will depart and so on.
  7. To stop all trains at the end of a loop either press "D" or "E" on the AristoCraft transmitter or simultaneously press "ENTER" and "MENU"

Speed Control in Blocks:

  1. The train's speed can be automatically be decreased in the blocks by setting the three speed control toggle switches.
  2. Switch 1 inserts a single diode in series with the block's power decreasing it by approximately 0.7 volts
  3. Switch 2 places two diodes in the circuit dropping the voltage about 1.4 volts
  4. Switch 4 inserts four diodes dropping voltage by about 2.8 volts.
  5. Any voltage drop from 0.0 volts (no switches) to 4.9 volts (switches 1, 2 and 4 on) can be inserted.
  6. Experiment to see what speed drop is best in your application.
  7. Note that the diodes are inserted into the circuit when a switch is pushed UP and bypassed when pushed DOWN.  With all three switches UP, as in the photo, the voltage drop is at maximum, approximately 4.9 volts.

Nudge Control Buttons

  1. The Nudge Forward buttons in the photo above allow you to make minor adjustments to the position of a train in a block so that it blocks a sensor.
  2. Just press button "1" to move the train in block 1, "2" for block 2 and so on.



'd. bodnar 9-4-06
'works with 2 or 3 trains based on value of SensorValue
'problem with intermittent detection of all sensors blocked cured by turning PWM on/off
'rather than leaving it on constantly

' 1. XXX configure one button to be an "end after all in blocks" and interface to remote control - may use pin 2 (input port a0)
' ("if porta pin0 = 1 then jump")
' 2. Create menu function - variables to be modified and written to memory (WRITE location, bytevalue) - 0-127 available on 28x
' a. 3 or 2 engines / blocks
' b. pause time for "nudge" function
' c. fixed order or random (random to allow repeats or no?)
' d. reset counter on power up or keep running count?
' e. time between trains exiting blocks - quick, fixed or random?
' f.
' 3. XXX Menu called up by pressing and holding MENU button on power-up (Menu on pin 14 (in3))
' a. change data through button or pot (pot set up on pin 5 (an3))
' 4. XXX When "Sensors not Blocked" flashes show which one(s) (is/are) not blocked
' 5. XXX BUG - when speed is too fast a long car with big spaces (lil critter & log car) set off sensor like it is back as it leaves
' 6. XXX Stop all blocks when 2 or more sensors not blocked after or during first and subsequent runs

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 version = 28
symbol dlay = 120
symbol SensorValue = b8
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!!!!!!!!!!!!!!!!!!!
'SensorValue = %11100000 ' %11100000 for 3 trains and %01100000 for 2 trains
'SensorValue = %01100000 ' %11100000 for 3 trains and %01100000 for 2 trains
read 0, SensorValue
if SensorValue <>%11100000 and SensorValue<>%01100000 then fixSensorValue
goto NoNeedtoFix
SensorValue=%11100000 'set to 3 trains if read is bad
pause 6000 'pause while LCD wakes up and does test display

pause 100
if SensorValue=%11100000 then engines3a
temp0=2:goto skipover1a:

serout 7, t2400,("?f?c0"):pause 200:serout 7,t2400,("?f?c0"):pause 200 '?c0 turns cursor off
serout 7,T2400,("Switch Controller?nd. bodnar 8-2006?nVersion ",#version," Trains=",#temp0)
serout 7, T2400,("?x00?y3 MENU FOR SETTINGS")
for temp1=1 to 100
if MenuSw=0 then goto MENU
pause 30
next temp1
serout 7, T2400,("?fResetting Switches")
gosub sw1turn:pause 200:gosub sw2turn:pause 200:gosub sw1straight:pause 200:gosub sw2straight:pause 400 ' set for initial run

'pause 4000
PWMOut 1, 25, 52 ' 38.4 kHz to IR emitters
temp1=pins & SensorValue
PWMOut 1,0,0
if temp1 <> SensorValue then ShowError

serout 7,T2400,("?fSensors All Blocked")
goto start1:


serout 7,T2400,("?fSensor(s) OPEN ",#temp1)
serout 7,t2400,("?nSensor 1 = ")
temp0= temp1 & %00100000
if temp0=0 then closed1
temp0=temp1 & %00100000
if temp0=1 then open1
open1:serout 7,T2400,("OK"):goto show2
closed1:serout 7,T2400,("open"):goto show2
serout 7,t2400,("?nSensor 2 = ")
temp0= temp1 & %01000000
if temp0=0 then closed2
temp0= temp1 & %01000000
if temp0=1 then open2
open2:serout 7,T2400,("OK"):goto show3
closed2:serout 7,T2400,("open"):goto show3
if SensorValue=%01100000 then done:
serout 7,t2400,("?nSensor 3 = ")
temp0=temp1 & %10000000
if temp0=0 then closed3
temp0=temp1 & %10000000
if temp0=0 then open3
open3:serout 7,T2400,("OK"):goto done
closed3:serout 7,T2400,("open"):goto done
pause 500
goto start:

serout 7,T2400,("?fAll Trains Ready!?n?nPress ENTER?nto START")
if EnterSw =1 then loop:
serout 7,T2400,("?f")
'serout 7,T2400,("?fnextloop= ",#nextloop)
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

gosub Sw2Straight:gosub Sw1Straight 'return 1 to 1
high NextLoop 'start train on block # NextLoop +1
goto AddOneToNextLoop

gosub sw1turn:gosub sw2straight 'return 2 to 2
high NextLoop 'start train on block # NextLoop +1
goto AddOneToNextLoop

gosub Sw2Turn
high NextLoop 'start train on block # NextLoop +1
goto AddOneToNextLoop


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

temp3=NextLoop 'store for use in Wait2Clear routine
goto 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
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 ")
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

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... ")
high tempCL
pause 300:low tempCL
goto DoubleCheck

high swdir1:low swdir2
high swmotor1:pause dlay
low swmotor1
serout 7,T2400,("?x00?y3Switch1=S")

high swdir2:low swdir1
high swmotor1:pause dlay
low swmotor1
serout 7,T2400,("?x00?y3Switch1=T")

high swdir1:low swdir2
high swmotor2:pause dlay
low swmotor2
serout 7,T2400,("?x11?y3Switch2=S")

high swdir2:low swdir1
high swmotor2:pause dlay
low swmotor2
serout 7,T2400,("?x11?y3Switch2=T")

serout 7,T2400,("?f")
pause 200

if endswitch=0 or MenuSW=0 and EnterSW=0 then setendflag
serout 7,T2400,("?fEnd Flag Set?n STOPPING?n at End of Lap")
'pause 3000

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
'serout 7,T2400,("?x00?y3temp= ",#temp1," ")

'if temp=28 or temp=60 or temp=92 or temp=156 then badSensor
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

serout 7,T2400,("?fBAD SENSOR?n STOPPING?n ALL")
low relay1:low relay2:low relay3
Pause 3000
goto start


serout 7,T2400,("?f MENU")
pause 1000
if SensorValue=%11100000 then engines3
temp0=2:goto skipover1:
serout 7,T2400,("?f# of Engines Now = ",#temp0)
serout 7,T2400,("?nENTER to Change?nMENU to Skip")
if MenuSW=0 then skipover2:
if EnterSW=0 then ChangeEngineNumber
goto CheckSwitch1

if SensorValue=%11100000 then EnginesTo2:
SensorValue=%11100000:write 0,SensorValue
serout 7,T2400,("?fNEW VALUE WRITTEN"):pause 1000
goto MENU:
SensorValue=%01100000:write 0,SensorValue
serout 7,T2400,("?fNEW VALUE WRITTEN"):pause 1000
goto MENU

if enterSW=1 then menu 'stay here till button pushed (gives a "0" when pushed)

'serout 7, T2400,("?f")
goto start