Control switch statments using logic. Created by Brit Cruise.
Want to join the conversation?
- What perquisite knowledge should we have in order to understand and do this properly?(5 votes)
- Some knowledge of programming in any language would be helpful, but you would really only need to be able to think logically.(3 votes)
- what is it trying to get you to learn?(3 votes)
- It is getting you to learn the basics of Lego NXT robotics programming which may help you in your future career(if you choose to become a programmer or technical engineer).(2 votes)
- why does your video have these weird tool boxes and suit cases i don't really get it.(1 vote)
- where do you go to start the program?(0 votes)
Let's do a slightly more difficult problem using the rotation sensor. Again, assume we have a beam attached to our motor. And what we want our program to do is if the beam is inside this region-- we'll call this Region A-- we want to generate a high-pitched sound. And that being in Region A means we have rotated the motor such that this beam is pointing somewhere in this region. And otherwise, if it's in the Region B, which is anywhere outside the slice-- so where it currently is is Region B-- this is all Region B-- will generate a lower-pitched sound. And remember, in A, we're going to generate a higher-pitched sound. Remember, our motor is equipped with a rotation sensor, and we can solve this problem by controlling a switch statement based on logic. And now, logic is the language of true and false. It's the language of robotics. If we pose this question in terms of logic, we just need to be a little bit more specific. So remember, x can take a value from zero to 360. And let's say we did some tests by polling our sensor, and we said this is the zero position. And we realized this is 100 degrees here, and over here is 200 degrees. So now our question is, if we're thinking like a robot-- and yeah, this is not to scale at all-- is x, which is the current reading in degrees of our rotation sensor, somewhere in this region? And this is Region A here on this little number line, and B is anywhere else. And we can answer this question with true or false, yes or no. If it's in A, we say yes-- yes or true, in terms of logic. If it's not in A, we say no, it's not in A. False. And now the final step is to do something with this true-false condition, such as generate, if it's true, we'll generate our high-pitched sound, and if it's false, we'll generate our low-pitched sound. So let's go ahead and build this. We first have the user zero, the rotation reading, using the orange button on the brick. And the way we do this is to drop a weight block, which is under Flow, onto our beam. And beside the weight block, I'm going to drop the rotation sensor. Now first, look at the weight block. Click on it and see that it is set to wait for the touch sensor to be pressed. But we want to switch that to NXT button. So now what's happening is it's set to the Enter button. Our program will begin, and it will halt here until the user presses the button. Then, it will jump ahead. So what that will allow us to do is have the user press the button to zero the rotation reading. And in this case, I'm going to click on my rotation sensor. I'm going to set it to Port C. That's where I plugged in my motor. And under Action, instead of reading from it, we're just at the stage where we want to zero it. So I'm going to click on the Reset radio button. And that's it. After that, I'm going to play a sound so the user knows that they have zeroed the motor, and I think most appropriately would be-- the robot says zero, meaning it's ready. Now, the heart of our program will be done inside a loop. First, our rotation sensor. We grab that from our input and drop it here. I'm setting it to Port C, and in this case, leaving it on Read, because we're going to grab a value from it. Beside the rotation sensor, I'm going to drop our range block, which is under Flow. And we could use the comparison block if we were checking if it's greater than 100, but we're checking if the reading is between 100 and 200, so our range block is the easiest way to do that. Now, clicking back to my rotation sensor, notice it has two outputs. The output we care about here is we want to wire the degree reading into the number input for range. And what range will do is check if it's inside two variables, A and B, down here. And A is set to 25. I'm changing it to 100, and I'm changing B to 200. So if the reading is between 100 and 200, the range block will output yes or no. And this is where logic is coming in. Yes or no, true or false. We can now do something with this value. We grab our switch statement, and here is where we can control a switch statement based on this logical output. So instead of reading from a sensor as we've done-- and it's set here to switch based on the touch sensor-- I'm going to change it to switch based on a value, not a sensor. So the control is a value, and the type now is logic, meaning this switch statement needs a yes or no to know whether to execute on the top or bottom path. So we take this yes-no output and wire it right into the bottom there on the switch statement. So now this checkmark represents yes or true, and this x represents false or no. And remember, we want to generate a tone, so I'm going to put a tone block under true and under false. So what should be happening here is, once the user runs the program, if they rotate the motor inside that range, the program is going to be in here. So I'll generate a tone, and if we rotated outside the region, this should flip to false and we will execute this block. And in this case, I'm going to change it to tone and generate a lower tone. So that's our whole program now. This will now output a tone based on what region the rotation sensor is in. And let's see it in action. [BEEPING]