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

What does this project do?
When the train passes in front of the sensor, it will stop the train for 3 seconds and then restart with the same power as it had before it was stopped.
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 use local variables in scripts.
Project File
Building the Set-up
This project uses the exact same set-up as the Train Sensor - Stop project.
Writing the Code
A large part of the code of the Train Sensor - Stop project can be reused for this one. The main difference is that we need to restart the train with a custom power setting, not a fixed value of 50%.
Open the code editor of the sensor tile and make sure you're adding your code to the Trigger event. Start by creating an object to access the train control panel.
train = self.getTile('Train 1')
Before we stop the train, we need to read and save the power settings of the train because we need that to restart the train. We can get the power sent to the train with the .getPower() function. To be able to use it later on in the script, we need to store it in a variable. In our example script, we're going to use the variable trainPower for this, so
trainPower = train.getPower()
Now that we know how fast the train was going we can stop the train, disable the sensor and wait for 3 seconds.
train.stop()
self.disable()
self.sleep(3)
When we restart the train, we can use the trainPower variable we created as argument in the .goForward() function. As such we tell the train to start with the same power as it had before it was stopped.
train.goForward(trainPower)
The only things we still have to do is re-enabling the sensor.
self.enabled()
This gives the following trigger event script:
#Get the train object
train = self.getTile('Train 1')

#Store the current power
trainPower = train.getPower()

#Stop the train
train.stop()

#Disable the sensor
self.disable()

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

#Re-enable the sensor
self.enable()
Validating 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.
Set the power of the train to a certain value and click the sensor tile. The power should go to 0 and 3 seconds later the power should go back to what it was. If you change the train power and click the sensor tile once more. The train should stop and then restart with the modified train power.

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. Change the speed while it's going and see if the sensor restarts the train with the modified power settings.

Scope
The trainPower variable that we created is local to the event script of our sensor tile. This means that when is script is done, the variable is destroyed and its value is lost. So next time the event script is executed, the variable trainPower will no longer exist. The trainPower variable is also not visible to scripts in other tiles, it's only visible to the code running in the trigger event tile.
That's usually referred to as the Scope of Variables: which parts of your program can see or use it. As the trainPower variable is only visible to the script it's running in it's called a local variable.
It is possible to create variables with a global scope, meaning that they are not destroyed once the event script is finished and they can be accessed by other event scripts. The use of global variables will be discussed in other projects.
What Did We Learn?
In this project we learned:
  • How to use local variables to control the behavior of your layout
  • The concept of scope of variables
Documentation > nControl > Train Automation Course > Next Project: Train Sensor - Reverse