Array Title

Lego MINDSTORMS EV3: Array Operations Block In Detail

In Lego Mindstorms by Glenn TurnbullLeave a Comment

In this post we will cover the Array Operations programming block used within the Lego MINDSTORMS EV3 programming software. We will cover all the available options and also provide a step by step example program.

What Is An array?

Before we get started on the EV3 Array Operations block it’s best to define what an array is. An array is a data structure which can be used to store a data collection (a list of values)

If you have read out post on variables, it’s like a variable but instead of holding a single data value for a particular type an array holds multiple values within the one variable (like a list of variables).

What Is the Array Operations Block?

The Lego EV3 Array Options programming block is used to manage an array within a program. Using this block we can:

  • Create and array
  • Add a new element to an array
  • Read and update elements within an array
  • Get the length of and array (i.e. the number of elements within the array)

It’s available from the red data operations palette within the EV3 Programming Software.

Lego EV3 Programming Array Operations Block

The Array Operations block supports 2 types of arrays:

Array TypeDescriptionExample
NumericHolds numeric data1, 10, 8
LogicHolds boolean dataTrue, True, False

What are the EV3 Array Operations Block Modes?

There’s 4 modes available on the Array Operations block. Each mode is outlined below:

Append

Append mode of the Array Operations block will add a new element to an array. This new element will be added to the end of the array and not the start.

In this mode there’s 2 inputs and 1 output available :

Inputs:

  1. Array In: The array to append to is wired into this input. Note that it can also be left blank.
  2. Value: The value to add to the end of the array

Output:

The output needs to be wired into a write variable block with the same array type as the Array Operations block. The output will be a completely new array and the array wired into the “Array In” input will not be affected.

The Append mode can also be used to create a brand new array. If the “Array In” input is not wired in from a read variable block, values can be manually entered into the input using the programming software and the new array will be fed into a new variable.

The following table shows how the Append mode behaves in a couple of scenarios. We are using a Logic array as an example:

Array In Value to Add Output
  True [True]
[True; False] False [True; False; False]

Read at Index

The Read at Index mode will read the value of the element at a particular index (position). This mode will return either a Logic or Numeric value depending on the read at index mode set. In this mode there’s 2 inputs and 1 output available:

Inputs:

  1. Array In: the array to read is wired into this input
  2. Index: The index (or element number) to read.

Note that the Index is a zero based index, which means that the first value in the array is at index 0, the second is at 1 and so on.

Output:

The output in this mode is either a number or logic (depending on the type of array)

The following table shows how the Append mode behaves in a couple of scenarios. We are using a Numeric  array as an example:

Array In Index Output
[1;2;3;4;5] 3 4
[100;200;300] 0 100

Write at Index

The Write at Index mode will override an element in an array at a particular position (index) with a new value. In this mode there’s 3 inputs and 1 output available:

Inputs:

  1. Array In: the array to read is wired into this input
  2. Index: The index (or element number) to replace
  3. Value: the value to replace the element at the specified index

Note that the Index is a zero based index, which means that the first value in the array is at index 0, the second is at 1 and so on.

Output:

The output in this mode is  a new array, the original array is left untouched. However the original array can be updated if the output is wired into a write variable.

The following table shows how the Write at Index  mode behaves in a couple of scenarios. We are using a Numeric  array as an example:

Array InIndexValue Output
[1;2;3;4;5] 3 100 [1;2;3;100;5]
[100;200;300] 0 5 [5;200;300]

Length

The length mode will return the size of the array – i.e. the number of elements the array contains. In this mode there’s 1 input and 1 output

Inputs:

  1. Array In: the array to read is wired into this input

Output:

The length of the array (numeric).

The following table contains examples of the Length modes inputs and outputs. We are using a Numeric  array as an example:

Array InLength
[1;2;3;4;5] 5
[100;200;300] 3

How to use the Array Operations Block

As you can see there are a few ways we can use the Lego Mindstorms EV3 Array Operations Block. In the following section we’ll go through each one and show examples of how each mode can be used.

How to Add to an EV3 Array

We add to an array using the Append mode, we can use this mode to add to an existing array or create a new array. Either way we need an array variable to write to in the output. In the example below we create an array using the Write | Numeric Array of the Variable block (block 1) with the values [0;1] and add 2 to the array using the Array Operations block (number 2).

Note: the output is wired into a Variable block in Write | Numeric Array mode

Lego EV3 Programming Array Operations Block - Append Array

The other example below shows how to create a brand new variable from the Array Operations block. In this case instead of wiring an array into the array input we hard-code the values, we can also use the value input to add an extra element. From there we can wire the new array into a Variable block in a Write | Array mode.

Lego EV3 Programming Array Operations Block - Append Array No 2

Reading an Array Element (Item)

To read an array element we use the Read at Index mode, this enables us to read an item of data from the array. In the example below we set up a numeric array variable with the following values [1;2;3;4;5] , read and display index 3 on the EV3 Brick Display for 5 seconds.

Remember that the index in this mode is a zero based index so the value we receive in the output should be 4 in this case.

Lego EV3 Programming Array Operations Block -Read Array

How to Replace a Value in an Array

To replace an array value we need to use the Write at Index mode from within the Array Operations EV3 block. In the example below we set up a numeric array variable with the following values [1;2;3;4;5] and replace the 4 with a 6 (#3), then read and display index 3 on the EV3 Brick Display for 5 seconds to see the update.

Remember that the index both these modes are zero based index so the value we receive in the output should be 6 in this case.

Lego EV3 Programming Array Operations Block -Write Array

How to see how many elements (items) are in an Array

We need to use the Length mode to check how many elements are in an array. In the example below we set up a numeric array variable with the following values [1;2;3;4;5] , read the number of elements in the array (block 3) and output it to the screen for 5 seconds. In this case here we should see the number 5 as there are 5 elements in the array.

Lego EV3 Programming Array Operations Block -Array Length

EV3 Array Operations Block Example Program:

In this example program we’ll put a number of the EV3 Array Operations block’s modes to use. The program itself could have been condensed into a single loop but we thought it best to write it the way we did to help get the array concepts (and uses) across. This program is a 3 number guessing game – it generates 3 numbers and waits for your guess via the EV3 Brick buttons (3 times).

Program logic:

  1. Generate a random number between 1 and 5 and save it to a numeric array
  2. Repeat step 1 three times
  3. Display a prompt to guess the number using the EV3 buttons (by pressing a brick button)
  4. Wait for an EV3 brick button to be pressed
  5. Check the pressed brick button’s index against the computed array value and display either: Correct or Wrong
  6. Repeat steps 3-5 three times

Note: for more information on the Lego EV3 brick buttons, see our post which covers everything there is to them. We also use a few different types of variables in this post, if you are unfamiliar with variables check out our variable post here.

Let’s get started:

  1. Create a new Numeric Array variable named “Random Numbers”
    • Drag and drop a red Variable block next to the display block
      • Set its mode to Write | Numeric Mode
      • Click the variable name in the top right of the block and select Add Variable
      • Enter the name RandomNumber
Lego EV3 Programming Array Operations Block -Example Program Step 1 V2
  1. Generate the 3 random numbers
    • Drag and drop an orange Loop block next to the variable block
      • Set its mode to Count
      • Set its value to 3
    • Drag and drop a red Random block inside the loop
      • Set its mode to Numeric
      • Set the lower bound to 1
      • Set the upper bound to 5
    • Drag and drop a red Variable block next to the random block
      • Set its mode to Read | Numeric Array
      • Ensure the variable name is set to RandomNumbers
    • Drag and drop a red Array Operations block next to the variable
      • Set its mode to Append | Numeric
      • Wire the read Variable block into the Array In input
      • Wire the value output from the Random block into the Value input
    • Drag and drop a red Variable block next to the array operations block
      • Set its mode to Read | Numeric Array
      • Ensure it’s set to the RandomNumber variable name
Lego EV3 Programming Array Operations Block -Example Program Step 2
  1. Loop the logic to prompt, check the guess and display the result three times. Also add a variable to keep track of the guess number.
    • Drag and drop a red Variable block outside of the loop block
      • Set its mode to Write | Numeric
      • Click Add Variable under the variable name
      • Name the variable Index
      • Set its value to 0
    • Drag and drop an orange Loop block next to the variable block
      • Set its mode to Count
    • Drag and drop a red Variable block inside of the new loop block
      • Set its mode to Read | Numeric
      • Ensure the name is set to Index
    • Drag and drop a red Text block next to the variable block
      • Set A: input to Guess Number:
      • Wire the variable read block into B: Input
    • Drag and drop a green Display block next to the text block
      • Set its mode to Text | Grid
      • Set Aa (text size) to 1
      • Set its Value (top right) to Wired
      • Wire the Output from the Text block into the Text input
    • Drag and drop an orange Wait block next to the Display block
      • Set its mode to Brick Buttons | Change | Brick Buttons
    • Drag and drop a red Variable block next to the wait block
      • Set its mode to Read| Numeric Array
      • Ensure the variable name is set to RandomNumbers
    • Drag and drop a red Array Operations block next to the variable block
      • Set its mode to Read at Index | Numeric
      • Wire the read array variable block (RandomNumbers) into the “Array In” input
      • Wire the read numeric variable block (Index) into the Index Input
    • Compare the pressed brick button ID to the random number at the current index:
      • Drag and drop a red Compare block next to the array operations block
        • Set its mode to Equal To
        • Wire the Value output from the Array Operations block into the A input
        • Wire the output from the orange Wait block into the B input
      • Drag and drop an orange Switch block next to the compare block
        • Set its mode to Logic
      • Drag and drop a green Display block inside the True case of the Switch block
        • Set its mode to Text | Grid
        • Set the text to Correct 🙂
      • Drag and drop a green Display block inside the False case of the Switch block
        • Set its mode to Text | Grid
        • Set the text to Wrong 🙁
      • Drag and drop an orange Wait block outside of the Switch block
        • Set its mode to Time Indicator
        • Set the Seconds value to 2
    • Add one to the index variable and calculate the number of loops
      • Drag and drop a red Math block next to the wait block
        • Set its mode to Add
        • Wire the read Index variable block (at the start of the loop) into input A
        • Set input B to 1
      • Drag and drop a red Variable block next to the match block
        • Set its mode to Write| Numeric
        • Ensure the name is set to Index
        • Wire the output from the Math block into the Value input
      • Drag and drop a red Variable block next to the match block
        • Set its mode to Read | Numeric Array
        • Ensure its name is set to RandomNumbers
      • Drag and drop a red Array Operations block next to the variable block
        • Set its mode to Length | Numeric
        • Wire the RandomNumbers variable block into the Array In input
        • Wire the output into the Loop block’s count input
Lego EV3 Programming Array Operations Block -Example Program Step 3

Congratulations, you have now completed the program. Hit the download and run button to test it out. The complete program should look like the image below:

Lego EV3 Programming Array Operations Block -Example Program Whole Program

Leave a Comment