Documentation > nControl > Train Automation Course
Train Sensor - Stop
For nControl™ 2019.2 or higher

What does this project do?
When the train passes in front of a sensor, it will stop the train for 3 seconds and then restart it at 50% power.
What will you need to build this project?
  • 1x LEGO® PF train and a 4DBrix™ WiFi train controller   or   1x LEGO® PU train and a BLE112 dongle
  • a loop of LEGO® train tracks
  • 1x 4DBrix™ IR train detection sensor and USB controller for the sensor
  • nControl 2019.2 or higher
What will we focus on?
  • How to process sensor feedback with Python™ scripts.
Project File
Building the Set-up
Start by building a small loop with your LEGO® tracks. Put the sensor along a straight stretch of the track; you can put it along a curved part as well but then you have to make sure the train doesn't hit it. Connect the sensor to the controller, e.g. port 4 of the starter controller, and the controller to a USB port of your computer.



Open nControl, model your train layout and add a sensor and train control tile. We're going to use the default name 'Sensor 1' for the sensor tile and 'Train 1' for the train control tile. Make sure you use the correct controller settings, WiFi or PU hub, in function of the train you're using.

Writing the Code
When the sensor sees the train it has to stop it. The sensor tile can respond to a number of different events; when it sees a train it executes the Trigger event. So open the code editor of the sensor tile and make sure you're adding your code to the Trigger event.


The first thing the sensor script needs to do is access the train control panel:
train = self.getTile('Train 1')
Now that we have access to our train, we can stop it:
train.stop()
Once the train is stopped we have to wait three seconds before we restarting the train. But before we do that, we're going to disable the sensor. We do that to make the sure sensor does not detect the train when we're restarting it, because in that case the sensor would stop our train again. We can disable the sensor with the .disable() function:
self.disable()
Note that we use the self keyword here, because we are working in the sensor tile that we want to disable. Now that the sensor has been disabled, we can wait for three seconds
self.sleep(3)
and then restart the train with 50% power
train.goForward(50)
The only things we still have to do is to re-enable the sensor so it can detect the train once it has made another loop. We're first going to wait 2 seconds to give the train time to leave and then we're going to re-enable the sensor.
self.sleep(2)
self.enabled()
This gives the following trigger event script:
#Get the train object
train = self.getTile('Train 1')

#Stop the train
train.stop()

#Disable the sensor
self.disable()

#Wait 3 seconds before restarting the train
self.sleep(3)
train.goForward(50)

#Re-enable the sensor
self.sleep(2)
self.enable()
Testing the Script in Simulation Mode
Before running our script with an actual LEGO® train, we're going to test it in simulation mode. You can simulate a train passing in front of the sensor by clicking on the sensor tile. If your script is correct, you should seen the train power go to 0 for three seconds before it goes back to 50.

Running the Project with a LEGO® Train
Once the script works as expected in simulation mode you can run it with an actual train. Your train should stop for 3 seconds every time is passes in front of the sensor.

What Did We Learn?
In this project we learned:
  • How to detect a train with a train detection sensor
  • How to execute a custom operation when the sensor detects a train
Documentation > nControl > Train Automation Course > Next Project: Train Sensor - Power