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.

a VEXcode Switch Blocks Example showing a Switch Stack Block#
 [] :: 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.

a VEXcode Switch Blocks Example showing a Switch Stack Block with switch python commands for driving forward for 400mm#
 when started :: hat events
 [Move forward 400 mm]
[robot.move_for(400, 0)] :: custom-switch

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.

a VEXcode Switch Blocks Example showing a Switch C Block#
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.

a VEXcode Switch Blocks Example with text inside of the switch c block and a print block saying bumper was pressed#
when started :: hat events
[Continuously turn right until the screen is pressed.]
turn [right v]
forever
  if [ if robot.screen.pressing(): ] :: custom-switch
    print [ Screen was pressed! ] on screen ▶
    stop all movement
  end
end

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.

a VEXcode Switch Blocks Example showing a Switch C Expandable block#
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.

a VEXcode Switch Blocks Example showing a Switch C Expandable with if else python code#
when started :: hat events
[Continuously move forward until the robot has a sports ball.]
forever
  if [ if robot.has_sports_ball(): ] :: custom-switch-expand
    print [ Screen was pressed! ] on screen ▶
    stop all movement
  else [else:                                          ] 
    move [forward v]
  end
end

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.

a VEXcode Switch boolean Block#
<[] :: 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

a VEXcode switch Block example with a boolean block that has a line of switch python functions#
<[controller.button_up.pressing() and controller.button_down.pressing()] :: 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 >=

a VEXcode switch block example using a boolean Block with a comparison operator#
<[robot.inertial.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.

a VEXcode switch block expample using a boolean Block with comparison operator#
when started :: hat events
[Continuously turn right until the screen is pressed.]
turn [right v]
forever
  if <[robot.screen.pressing()] :: custom-switch> then
    print [ Screen was pressed! ] on screen ▶
    stop all movement
  end
end

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.

a VEXcode Switch Blocks Example showing a Switch Reporter Block#
([] :: 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: *

a VEXcode Switch Blocks Example showing a Switch Reporter Block with python code that has an addition operator#
([robot.inertial.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.

a VEXcode Switch Blocks Example showing a Switch Reporter Block within a print block#
when started :: hat events
[Current heading of the robot + 45 degrees will be printed on screen]
print ([robot.inertial.get_heading() + 45]  :: custom-switch) on screen ▶

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.

a VEXcode Switch Blocks Example showing a Switch hat block#
[] :: 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: ().

a VEXcode Switch Blocks Example showing a Switch hat block with python function that has a parameter#
[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.

a VEXcode Switch Blocks Example showing a Switch hat block and a drive forward block#
[def move_400():] :: hat custom-switch
[Define a custom function using Switch Hat Blocks.]
move [forward v] for (400) [mm v] ▶

when started :: hat events
[move_400()] :: custom-switch