1 Unit 3:“Stop at the Push of a Button!”– Solution Exercise Make the NXT robot drive forward at random speeds until the touch sensor is pressed. We need to start LabVIEW™ with a Blank VI Targeted to NXT. The first thing to consider is that we will have code that will repeat over and over again, so we will need a while loop. When we create a while loop, we need to think about what we need our robot to do. There are two important things we need our robot to do, move the motors, and stop when the touch sensor is pressed. Let’s start with the motor control. Put down the two motor control functions and select the appropriate port. The code should look like Figure 1. Figure 1. Motor control within while loop The exercise is asking us to make the robot drive at random speeds. To select random speeds, we need to use the Generate Random Number function. We can access this function by right-clicking on the block diagram, selecting the Numeric palette, and clicking NXT Generate Random Number. We can then place the random number generator inside the while loop. On the random number generator, we need to specify the range of the output random numbers. We know this function will be selecting speeds of our motors, and the range of speeds on the motor control functions is 0 to 100. Therefore the range of random numbers also needs to be 0 to 100. To select the maximum value that is output by the random number generator, we can right-click the Max Value terminal on the function block, and create a constant. We can see that it is defaulted to a range of 0 to 100, so we do not need to change anything. Next we must wire the output of the random number generator and wire it to the Power terminals of the motor control functions. This is shown in Figure 2. 2 Figure 2. Random speeds The last thing we need to do is read from the touch sensor. To access the sensor function, we right click on the block diagram, select the NXT I/O sub-palette and click Sensor. We can click the polymorphic selector to bring down a menu with all of the sensors, we can select Read Touch, and make sure Pressed is selected. The touch sensor function outputs a Boolean value. It outputs a true value when the sensor is pressed and a false when it is not pressed. The exercise wants us to stop the program when the touch sensor is pressed, so essentially we want the touch sensor to end the while loop. To do this, we will wire the output of the sensor to the conditional terminal. This makes sense because when the touch sensor is pressed, it will output a true value, and since the conditional terminal is showing a stop sign, a true value will terminate the loop. All that is left to do now is to select the port to which the touch sensor is connected and we should have a running program. Figure 3. Program with sensor functionality To run this program, we can connect the NXT to the PC and click the Run button. We should make sure we save this VI because we will be using it in one of the lessons in the next unit.