Email question from a student
I received an email earlier today from a 7th grade student in Utah. Here's the (edited) question:
"How can I program my robot to make more than 1 yes-or-no decision. I have a robot that looks at 3 different sensors but I'm only using sensor logic values and not numbers. The switch block only lets me do one True or False condition."
Great question! And I certainly understand why you're confused. Rather than email the answer, I figured I'd share this with others because if one person is asking, I'm sure there are others. So, here's one option (option #2 will be provided in a separate blog post):
I don't know the details of your robot, but let's just assume that your robot's 3 sensors are Touch, Sound, and Light. You've configured the trigger values for each sensor (For example, does the Light sensor detect a light value less than 20 - true or false?) and are using the logic data plugs to take the True or False response from each sensor.
You didn't state whether all 3 sensor logic values must all be True or all be False, so this example will show you how to make decision based on a mixture of logic values, okay? Let's look at the possible options that can be returned. I'm going to write down all the possible options that could come from 3 sensors in the following format: Light - Sound - Touch with a 1 indicating True and a 0 indicating False. For example, if the Light sensor is True, the Sound sensor is False, and the Touch sensor is True, then I would write 1 - 0 - 1. Got it?
Okay, next we need to look at all the possible options?
Light - Sound - Touch
0 - 0 - 0
0 - 0 - 1
0 - 1 - 0
0 - 1 - 1
1 - 0 - 0
1 - 0 - 1
1 - 1 - 0
1 - 1 - 1
Verify that I've got them all, but I think you'll find that that's it... I've provided everything from all False to all True and everything in between. Now, count them up. There are a total of 8 possible combinations.
Using this, I need to figure out how to program a decision-making procedure that can account for 8 possible combinations. And to do that, I'm going to use the Switch block that you talked about in your question. You are correct that the Switch block only provides 2 possible options when using a Logic value - True or False. What I think will surprise you is that you can place a Switch block inside a Switch block inside a Switch block. Yes, that's 3 levels deep. By doing this, you can check the status of each sensor, one-at-a-time - based on True or False response, another Switch block will be used to check the status of the next sensor (True or False). A final level of Switch block will be added to test the last sensor.
Take a look at the image I've provided here and you'll see how this is done. Starting from the left, you'll see the first Switch block (SB-1) with True or False path. Follow it and you'll see 2 new SBs (SB-2 and SB-3), one in the True path of SB-1 and one in the False path of SB-1. Follow these and you'll see 4 new SBs, 2 in the True path of SB-2 and 2 in the False path for SB-3. And because each of these SBs have a True/False path, you will find that you have a total of 8 possible paths that can be traced.
If this is confusing you, just trace each possible path with your finger and keep count. Use the circles in the right image to represent each switch - blue lines are True and red lines are False. If you're still confused, the best solution would be to duplicate my image in NXT-G and experiment - it'll make sense if you think about it for a while. Thanks for the question - and option 2 will be posted shortly. In that option, I'm going to show you a way to use logic values to determine if all sensors are True or all sensors are False.