Another short chapter? Of course! You can learn something and then get back to playing around with your NXT robots. But you won't get to play much if your robots keep falling asleep. This chapter will show you how to keep your robots working, even if the Sleep option on the Brick has been set to a short period of time like 2 minutes or 5 minutes.
Your NXT Brick has a built-in feature that automatically turns off your robot after a certain amount of time has passed. This Sleep timer is configured on the Brick, and you can choose for the Brick to shut down after 2, 5, 10, 30, or 60 minutes of inactivity, or you can choose Never.
The Sleep timer is a useful feature, and you should definitely set it to a reasonable time. It can help save battery power if you accidentally leave the Brick turned on. If a program finishes executing, for example, and the robot stops moving while you're away for a little bit, the Sleep timer can make sure your robot turns itself off. But what if you want to build a robot that, for example, monitors the movement of a door using the Ultrasonic sensor? Think of a room alarm system that protects against intrusions from nosy brothers and sisters. If you program the robot to sit and monitor the door, you could easily set the Sleep setting to Never so it won't turn off on its own. However, an easier way is to use the KEEP ALIVE block.
The KEEP ALIVE
block and its configuration panel are shown in Figure 21-1.
Notice that the configuration panel is blank! There are no settings to configure with the KEEP ALIVE
block.
Here's how it works: During the execution of your program, if at any time a KEEP ALIVE
block is executed, the Sleep timer resets to its initial starting time. So, if you configured your Brick for a 5-minute Sleep timer and 1 minute into the program a KEEP ALIVE
block is encountered, the Sleep timer will reset to 5 minutes.
Now, to make this useful, you'll probably need to place the KEEP ALIVE
block in a location where it will frequently be executed. The best location is a LOOP
block that will occasionally (or continually) run the blocks inside it, including the KEEP ALIVE
block. This is one method for continually resetting the Sleep timer. (Remember, the Sleep timer starts counting when you turn on the brick, but resets when you run a program. If you don't run a program and the Sleep timer is set to 2 minutes, then after 120 seconds have elapsed, the Brick will turn itself off.)
An example is shown in Figure 21-2.
In this simple program, the robot will spin around a few times, say "Hello", and then do it again and again and again ... if the Touch sensor button is ever pressed, the SWITCH
block will execute the KEEP ALIVE
block, which resets the Sleep timer. If the button is never pressed, the robot will eventually turn off when the default Sleep timer value has been reached (assuming the Sleep timer is not set to Never).
This is important: always check the Sleep timer setting on your Brick before running a program. Once the time has expired, the program will stop. This happens frequently when the Sleep timer is set to 2 minutes or 5 minutes, so double-check this before running a program, and be certain to set the Sleep timer to a setting that is longer than you expect the program to run.
To close out this chapter, the last item I want to mention about the KEEP ALIVE
block is that it does have an output data plug in its data hub. This data plug will only supply a Number data type, and that number will always be the Sleep timer default value in milliseconds (1,000 milliseconds = 1 second). You might not find this data plug very useful, but it could be used as input to a COMPARE
block that looks to see which is greater—a value from one of the Brick's three internal timers or the default Sleep timer value. Depending on the condition of the COMPARE
block, your robot might perform some final action before letting the robot shut down using a STOP
block or, as mentioned earlier, it might execute a KEEP ALIVE
block to reset the Sleep timer value.
For some reason, a lot of people don't believe me that the brick will simply shut down when the Sleep timer value is reached. Exercise 21-1 is a small sample program to test the validity of this statement. I've included a short sample program at the end of the chapter if you need help.
Create a program to test whether the brick will automatically shut down after two minutes even while running a program that loops forever. Be sure to set your brick to use a value of 2 for the Sleep timer.
Well, that's it for the KEEP ALIVE
block. Up next in Chapter 22 is the FILE ACCESS
block. You'll learn how to program your robot to store data in files that can be accessed at any time, now or later.
Before you run the program, make certain the brick's Sleep setting is set to two minutes. To do this, turn on the brick, use the left or right buttons to navigate to the Settings selection and then press the Enter button (orange button). Use the left or right buttons again to navigate to the Sleep selection and press the Enter button. Use the left or right button to count up or down and change the value on the screen to 2. (Options include 2, 5, 10, 30, 60, and Never.)
Figures 21-3 through 21-7 show the complete test program and configuration panels for testing Exercise 21-1. Notice that the LOOP block is configured to loop for a duration of Forever. When the program runs, the brick will play a sound first, wait 3 seconds, rotate motors B and C a single rotation, wait 3 seconds, and then will repeat the entire process. Use a stopwatch or timer to verify that after the program starts that the brick shuts down after 120 seconds have elapsed.