If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

Main content
Current time:0:00Total duration:6:59

Video transcript

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 region B which is anywhere outside the slice so where it currently is region B this is all region B will generate a lower pitch sound and remember in a we're going to generate a higher pitch 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 so 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 0 to 360 and let's say we did some tests and by polling our sensor and we said this is zero the zero position and we realize this is a 100 degrees here and over here is 200 degrees so now our question is if we're thinking like a robot is X and yeah this is not the 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 pitch sound so let's go ahead and build this 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 see 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'll 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 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 Reed 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 blog if we were checking if it's greater than 100 but we're checking if the range or 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 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 oops 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 in to the bottom there on the switch statement so now this check mark 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 their motor inside that range the program is going to be in here so I'll generate a tone and if we rotate it outside the region this should flip the 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 patience sensor is in and let's see it in action