Wednesday, January 2, 2008

NXT-G Program Basic Lesson 5- Switch Block



Lesson on use of Switch Block
Today you decide you want to do different things depending on whether the touch sensor is pressed or not. The touch sensor is a button, and your NXT-G program can make decisions depending on which state it is in.

To do this, you would use a Switch Block. By default, it is set up to make a decision using the touch sensor, but it has many other uses.

When you add a Switch Block, a box shows up in your program with the "switch"part on the left. Inside the box are two sequence beams, instead of the usual one. The blocks (you have to add them, of course) on the top beams are only run if the switch condition is "true". The bottom is run for "false".

The condition is indicated by the choices in the panel at bottom-left. Check that the Port is the one you have wired the sensor to, and change it if necessary. Everything else should be already set correctly for this use.

In the example, you are running a car with motors connected to ports B and C. If the touch sensor is pressed when it gets to this part of the program, it will run forward for 2 seconds. If not, it will go backwards for 2 seconds. If this was the complete program, you would have to press the button as the program is started to have the car go forward.




Use of Switch Block

The switch block allows one to make choices



The flat view of the swtich block



Tabbed view of the switch block



Five different paths for the switch block





Switch block with two possible paths



Second switch using sound sensor



NXT robot will turn right if the first and second switch block conditions are met





0 comments: