Switch#
Introduction#
Switch blocks enable students to write Python code directly within a block-based environment. This approach helps bridge the gap between block-based and text-based programming. By allowing users to see how block commands convert into Python, Switch blocks make it easier to learn text-based coding techniques within a familiar interface. Using Switch blocks can simplify advanced logic, calculations, and function calls while still using normal Blocks.
Stack#
The Switch Stack block is used to execute Switch Python commands typed within the open space of the block.
They are shaped to attach above or below other action blocks.
[] :: custom-switch
Switch Stack blocks can be used for single lines of Switch Python commands or multiple lines.
In order for a Switch Stack block to function as intended, the Switch Python commands must be entered with the correct spelling, punctuation, and indentation.
when started :: hat events
[Build Used: Super Code Base 2.0]
[Drive forward then stop.]
[drivetrain.drive(FORWARD)] :: custom-switch
wait [2] seconds
stop driving
C Block#
The Switch C Block block is used to loop the blocks within until the condition in the open space of the block is met.
They are shaped to attach stack or other C blocks above, below, or inside them.
if [] :: custom-switch
end
Some possible Switch Python commands to use with the Switch C block include:
while
if
for
In order for a Switch C block to function as intended, the Switch Python commands must be entered with the correct spelling, punctuation, and indentation.
when started :: hat events
[Build Used: Super Code Base 2.0]
[Drive forward whenever the LED Bumper is pressed.]
forever
if [if bumper.is_pressed():] :: custom-switch
drive [forward v] for [200] [mm v] ▶
C Expandable#
The Switch C Expandable block is used to loop the blocks within a specific branch when the condition in the open space of the block is met. Additional branches can be added by expanding the block.
They are shaped to attach stack and other C blocks above, below, or inside them.
if [] :: custom-switch-expand
end
Some possible Switch Python commands to use with the Switch C Expandable block include:
while
if
for
In order for a Switch C Expandable block to function as intended, the Switch Python commands must be entered with the correct spelling, punctuation, and indentation.
when started
[Build Used: Super Code Base 2.0]
[Drive forward if there is no an object in the way.]
forever
if [if eye.is_object_detected(): ] :: custom-switch-expand
stop driving
else [else:]
drive [forward v] for [200] [mm v] ▶
Boolean#
The Switch Boolean block is used to return a condition as either True or False. These conditions are defined with Switch Python functions.
They are shaped to fit inside any blocks with hexagonal (six-sided) inputs.
<[] :: custom-switch>
Switch Boolean blocks can be used for single lines of Switch Python functions that directly will return a true or false condition.
Operators can be used to evaluate multiple Switch Python functions in a single Switch Boolean block. This includes:
and
or
not
<[bumper.is_pressed()] :: custom-switch>
A Switch Boolean block can also combine Switch Python functions with comparison operators to evaluate how a value returned compares to the given number using the following operators:
Greater Than: >
Less Than: <
Equal to: =
Additional operators that can be used include <=, !=, and >=
<[drivetrain.get_heading() > 100] :: custom-switch>
In order for a Switch Boolean block to function as intended, the Switch Python commands must be entered with the correct spelling, punctuation, and indentation.
when started :: hat events
[Build Used: Super Code Base 2.0]
[Drive forward whenever the LED Bumper is pressed.]
forever
if <[bumper.is_pressed()] :: custom-switch> then
drive [forward v] for [200] [mm v] ▶
Reporter#
The Switch Reporter block is used to return a numeric value. These conditions are defined with Switch Python functions.
They are shaped to fit inside any blocks with circular inputs.
([] :: custom-switch)
Switch Reporter blocks can be used for single lines of Switch Python functions that directly will return a numeric value.
Math functions can be used to calculate values within a Switch Reporter block. This includes, but is not limited to:
Addition: +
Subtraction: -
Division: /
Multiplication: *
([drivetrain.get_heading() + 45] :: custom-switch)
In order for a Switch Reporter block to function as intended, the Switch Python commands must be entered with the correct spelling, punctuation, and indentation.
when started :: hat events
[Build Used: Code Base 2.0]
[Display the rotation while turning.]
turn [right v] for [450] degrees ◀ and don't wait
forever
clear all rows
print ([drivetrain.get_rotation()] ::custom-switch) ▶
wait [0.1] seconds
Hat#
The Switch Hat block is used to run the attached stack of blocks when the function is called.
They are shaped to attach above other action blocks.
[] :: hat custom-switch
The attached stack of blocks are a function. The Switch Hat block is used to define the name of the function and any optional parameters.
Parameters should be enclosed in parentheses and followed by a colon as shown below. If a function has no parameters, use closed parentheses: ().
[def function_name(parameter):] :: hat custom-switch
In order for a Switch Hat block to function as intended, the Switch Python commands must be entered with the correct spelling, punctuation, and indentation.
[def drive_400():] :: hat custom-switch
[Build Used: Code Base 2.0]
[Create a function to drive a robot forward.]
drive [forward v] for (400) [mm v] ▶
when started :: hat events
[drive_400()] :: custom-switch