Beyond the Basics

What happens if a light is shined while a stack is still running?

If you shine a light at the light sensor once, the stack attached to the watcher is set off. If uninterrupted, all the blocks in the stack will be carried out. However, if you happen to shine a light again before the stack is finished, the stack will start over from the top without executing the rest of the commands.

  


© The LEGO Group

 

 

 

 

 

 

 

Another important point here is that the Sensor Watcher runs independent of other parts of the program. That means that the RCX can do more than one thing at a time.

 

Hereís a simple example:

© The LEGO Group

 

Here motors A and C are on. It beeps 3 times when it gets a light reading between 80 and 100, then it spins. It moves backwards for 3 seconds then forward again when it gets a reading between 35 and 50.

 

Setting the range for light Stack Decision Makers: Setting light sensor conditions for the Stack Decision Makers is much like that for the Sensor Watchers with one exception. The Repeat While and Yes or No Decision Makers canít be set to recognize a blink.

 

Since all Stack Decision Makers look for input when their turn to execute comes, it is critically important that the Decision Maker block has a chance to receive and process incoming sensor input.

The following sequence of three programs illustrates that the timing of input is critical when working with Stack Decision Makers.

 

Tech Guide

Tech Guide

 

 

 

 

 

 

In the program NoChance1, the Yes or No Stack Decision Maker happens to be the first block in the stack. It will look for input as soon as the Run button is pressed. If a light is not flashed at the sensor at the same time as the Run button is pressed, the Decision Maker will direct the program to the No branch.

 


© The LEGO Group

 


© The LEGO Group

 

The same will happen in the program NoChance2, because executing the On AC block happens instantaneously, and therefore doesnít buy time.

 

 


© The LEGO Group

 

In the program Chance, however, once the Run button is pressed, there are 3 seconds before the Yes or No Decision Makerís turn comes. For the Decision Maker to choose the Yes branch, the sensor has to receive a bright light when the 3 seconds are up.

Practically, a user will start shining a light at the sensor before the time is up and continue to shine it for a bit longer while the Yes or No block carries out.

 

 

Transparency #6A illustrates the programs NoChance1, NoChance2, and Chance discussed above.

 

Close Window

 

Text Box: Here a bright light will trigger the stack. If shined once, the entire stack will be carried out, ending with a beep sound. However, if a light is shined again, while the stack is still executing (e.g., while moving backwards), the stack will start over again and wonít carry out the beep block.