Salta al contenido principal

Si estás viendo este mensaje, significa que estamos teniendo problemas para cargar materiales externos en nuestro sitio.

Si estás detrás de un filtro de páginas web, por favor asegúrate de que los dominios *.kastatic.org y *.kasandbox.org estén desbloqueados.

Transcripción para Switch block

  • 0:02Switch statements are powerful, because they
  • 0:04allow you to change the behavior of your robot
  • 0:07based on some condition.
  • 0:10For example, let's say you're writing a program--
  • 0:13and I always draw out my programs first--
  • 0:16which does something, say it turns on the motors
  • 0:18and plays a sound, and you get to a point
  • 0:21where you want to branch the behavior based on a condition.
  • 0:24For example, if the touch sensor is pressed, go do this,
  • 0:29and if the touch sensor is not pressed, then go do this.
  • 0:33Now this little branch here is our switch statement in action,
  • 0:37and you should think of it as actually splitting the sequence
  • 0:40beam into two possible execution paths.
  • 0:44OK.
  • 0:45So let's do a very simple example, such as an object
  • 0:48detector.
  • 0:49[SERIES OF BEEPING SOUNDS]
  • 0:54--build our simple object detector.
  • 0:57The switch statement is under flow
  • 0:59and is this icon right here.
  • 1:01And as you can see, it has split the sequence beam
  • 1:03into two possible execution paths, which
  • 1:06are controlled by the setting.
  • 1:09Now the setting is down here.
  • 1:11We have it set to be controlled by a sensor, however,
  • 1:15we can select any sensor we want to control this.
  • 1:19So we're going to do an object detector,
  • 1:20but we could just as easily use a sound level
  • 1:23to control the execution-- maybe it'll
  • 1:25detect the presence of a clap-- or a light sensor--
  • 1:29if it's bright, it'll control the execution.
  • 1:32So I'm going to select ultrasonic sensor
  • 1:34and make sure I identify the correct port it
  • 1:37is plugged into.
  • 1:38This is my most common source of error.
  • 1:40I have it plugged into Port 3.
  • 1:43And now the important part is the comparison.
  • 1:45When we use words like close, bright, or loud,
  • 1:48these are arbitrary.
  • 1:49We need to tell our program exactly what we mean by close.
  • 1:53In this case, I'm going to drag the slider between these two
  • 1:57states, close and far, and I'm going
  • 1:59to say close is when we're less than 20 inches.
  • 2:05Now close, which is represented by this flower icon,
  • 2:09means our reading if there is an object
  • 2:12less than 20 centimeters, and this mountain, which
  • 2:14is our far state, is anything greater than 20 centimeters
  • 2:18away or if there's no object at all.
  • 2:21So when we look up here at our switch statement,
  • 2:24we see there's a ultrasonic sensor, so that's correct,
  • 2:27and there's now a flower and a mountain reminding us
  • 2:30if something is detected, it's going to take this top path,
  • 2:33and otherwise, it's going to take this bottom path.
  • 2:36I said we will generate a tone if we detect an object,
  • 2:41so I go to my output here, find the sound block,
  • 2:44and drag the sound block onto the top part of my switch
  • 2:48statement.
  • 2:48And now you can clearly see how there's two execution paths.
  • 2:52If it detects something, it'll play the sound block.
  • 2:55Otherwise, it will do nothing.
  • 2:57And what do we want it to do if it detects an object?
  • 3:01Let's have the sound block play a tone.
  • 3:03It's set to sound file, but I can just click tone here.
  • 3:07And then I'll click a key-- let's say,
  • 3:10it will play a C if it detects an object.
  • 3:14Before I run this, I want to put it inside a loop.
  • 3:17Otherwise, this will just quickly execute,
  • 3:19and I won't have any dynamic behavior.
  • 3:22So I'm going to grab loop from my flow and drop it here.
  • 3:28The loop is empty by default, and it's
  • 3:31going to be an infinite loop, which is what we want.
  • 3:34So we don't need to change anything.
  • 3:35I just need to drag-- I'm going to highlight all of this--
  • 3:38and drag it into my loop.
  • 3:43So there it goes.
  • 3:44It automatically expanded my loop,
  • 3:46and there's my little switch statement inside.
  • 3:48So let's run it and see if it works.
  • 3:50[SERIES OF BEEPING SOUNDS]