Lego EV3 Buttons Programming Constant Block Title

Lego MINDSTORMS EV3: Constant Block In Detail

In Lego Mindstorms by Glenn TurnbullLeave a Comment

In this post we will cover the Constant block used within the Lego MINDSTORMS EV3 programming software. As well as explaining this block we will include 2 step by step example programs.

What is a constant? In programming terms a constant is a value that cannot be modified within the program once it is set.

What is the Constant Block?

The Lego MINDSTORMS EV3 Constant block is used to set a value that can be used in multiple places within a program. It is available from the red data operations palette:

Lego EV3 Buttons Programming Constant Block

Unlike a constant within a language like C or C# the constant block within Lego MINDSTORMS can be modified within the program even after it is set. It’s purpose is to make programming / the programming UI more efficient.

What Modes does the Constant Block Support?

The following table outlines the modes that are supported within the EV3 Constant block along with some example data types to give a better idea of how they work.

ModeDescriptionExample
TextStores alphanumeric charactersThis whole sentence including numbers can be stored 1234
NumericStores a number10001
LogicStores a boolean value i.e. True or FalseTrue
Numeric ArrayStores a list of numbers1, 2, 300, 40, 3
Logic ArrayStores a list of True / False valuesTrue, True, False

How to Use the EV3 Constant Block

The Lego MINDSTORMS EV3 Constant block can be used at any time in your program – not necessarily the beginning. The value of the block is set manually prior to running the program using the text value in the top right-hand corner.

The example below shows how to use the Constant block to set the steering direction and rotations within a Move Steering block. The block is designed to be used at multiple times within a program and in this instance it’s used for 2 inputs to make the robot turn in a circle.

Lego EV3 Buttons Programming Constant Block Example

Once the Constant block is in your program, its value can be set by clicking on the right-hand corner of the block.

How to Use the Array Modes

Using either Logic or Numeric Array mode is allows us to store multiple values within the single Constant block. To do this:

  1. Set the Mode to Numeric Array
  2. Click in the right-hand corner (value).
  3. A new window will appear, click the + button to add a new value
Lego EV3 Buttons Programming Constant Block Wire Example -Adding Array Value

Continue to click the + button to add more values:

Lego EV3 Buttons Programming Constant Block Wire Example -Adding More Array Values

Lastly, to remove array values click the x button next to the value you wish to remove:

Lego EV3 Buttons Programming Constant Block Wire Example -Removing Array Values

Can the EV3 Constant Block Value be Set From Another Programming Block?

The value of the block cannot be set by another block such as a variable read or a sensor output, it needs to be set manually prior to running the program.

EV3 Constant Block Limitations

Like all other blocks you will find that the Constant block’s wiring is bound by the usual wiring limits. What do I mean? If for instance in the program below you wanted to wire the constant block into the Variable block’s value, it is not possible due the Constant block being outside the Switch block. In this case the Constant block would need to be in the same Case area of the Switch block.

Lego EV3 Buttons Programming Constant Block Wire Example

Moving the constant block here will work:

Lego EV3 Buttons Programming Constant Block Wire Example -Working

EV3 Constant Block Example Program: Accelerating a Robot

In the  EV3 Constant block example program we’ll use a Constant block to accelerate your EV3 robot, this constant block can be tweaked to make the robot accelerate faster. With this program any type of robot build using both motors is Ok. We do recommend the Explor3r – it’s a fast and simple build and can be used with either the Education or Retail Lego MINDSTORMS kits. Build instructions can be found here.

Program Logic:

  1. Create the constant and a variable named Speed
  2. Add the constant to the Speed variable
  3. Read the new Speed value into the Move Steering Power input
  4. Loop program for 20 seconds

Let’s get started:

  1. Define the constant and variable
    • Drag and drop a red Constant block next to the start block
      • Set its Value to 2
    • Drag and drop a red Variable block next to the Constant block
      • Set its mode to Read | Numeric
      • In the variable name click Add Variable
      • Type Speed and click OK
Lego EV3 Buttons Programming Constant Block - Example Program - Step 1
  1. Loop the program for 20 seconds and accelerate the robot
    • Drag and drop an orange Loop block next to the variable block
      • Set its mode to Time Indicator
      • Set the Seconds input to 20
    • Drag and drop a red Math block inside the loop
      • Wire the output from the Constant block into input a
      • Wire the output from the read variable block into input b
    • Drag and drop a red Variable block next to the math block
      • Set its mode to Write | Numeric
      • Wire the Output from the Math block into the Value input
    • Drag and drop a green Move Steering block next to the variable block
      • Set its mode to On
      • Wire the Read Variable (speed) block into the Power input
Lego EV3 Buttons Programming Constant Block - Example Program - Step 2

Hit to download and play to test your program. Try adjusting the constant block to make the robot accelerate faster in the 20 seconds.

We hope you enjoyed this deep dive post into the Lego MINDSTORMS EV3 Constant programming block. Feel free to leave a comment if you have any questions or have a suggestion for a future post.

Leave a Comment