Initialize an array for Advanced Light/Color sensors calibration Pro Preview

"Array initialization" is the first step in every program that involves Arrays. This applies to most programming languages and for EV3-G it is a must. 

In this tutorial, we would show you how to initialize the array and how to extract this logic in a new block

  • #633
  • 04 Oct 2017
  • 7:20


Each array has a number of cells. Each cell has an index and a value. When a new array is created we should set the number of cells that we have in the array along with the values of each array.

The EV3-G software allows us to set this values using the plus sign below the Array Variable block. 


The first part of the calibration is to initialize an array and in this array we'll store the minimum and maximum values that the sensors detect. If I start the program, the initialization is the first thing that happens right after you start the program. Then we store the variables for minimum and maximum detected values in this array and at the end of the program you can see from up to down we have 4 sensors. And on port number 1 we have nothing. It's not connected so it's a sensor we are not working with. But for the other 3 sensors - 2, 3 and 4 as ports we have different values. Like the minimum for port 2 is 5 and the maximum is 77. The minimum for port 3 is 7 and the maximum is 79. And the minimum for port 4 is 5 and the maximum is 76. So, again from up - down and we have the different values. We store these values in an array and the first part of our program is to initialize this array. This here is the program as we have it at the end. And the first block is the InitArray block. And in this InitArray we have a simple initialization of an array. So, to do it again I'll just create a new project to show you how to do the initialization and in this new project I'll zoom in here to see it better on the camera.

And now we take a new array and we take a new variable. We would like to write a new numeric array and we give a name to this array and the name of the array will be MinMaxArray.

So, this is the array where we'll store our values for minimum and maximum detected by the sensors. Then we must initialize the array with the default values - the values for minimum and maximum that are at the start of the program. And we can just create the different values right here. We need 8 values because we have 4 sensors - minimum and maximum for each of the sensors - we need 8 different values.

And we must initialize them. And a very clever way to initialize variables is the following. The first cell of our array will store the minimum value for sensor 1, the second cell of our array will store the maximum value for sensor 1. So, the first 2 cells are for sensor 1. The next 2 cells are for sensor 2 and we have again - minimum and maximum. So, if it is an odd cell, we store the minimum, if it is an even cell, we store the maximum. What would be a good default minimum value? A good default minimum value is a very large value because we are searching for something very small after the initialization. So, initially we can just initialize that the initial value for minimum is 12345 - that's a very large number. And for maximum we have -12345. And we have the same for minimum everywhere - each of the odd cells because these are all minimum values and we have for maximum -12345.

In this way we are sure that after we start the program the initial minimum value for each of the sensors is very large. And whatever the sensor detects will be taken into account because if we set the minimum value to -1000, the sensor will never detect anything that's less than 1000. And we are not able to actually store the value from the sensors in the MinMaxArray. Then we can extract this new block and we can extract it in a new block. How do we extract this? To extract this MinMax block in a new block I'll just return it back. Then we go to Tools > MyBlock Builder and here we give a name for our block and this block is called InitArray and since we are using arrays it's good to have the array icon. And we can give some description but we don't need to. We give this as finished. Then we just fix it. We open the InitArray and again fix it. I don't know why it is doing this. And we have a new block in our program and we can use this block, we can export it, we can use it in other programs, we can use it in our program and it is just a simple block for initializing the array and it's the same principle in most of the other programs that we are doing. If we need to initialize an array, this is the way we are doing it. Then a few things that we would like to do also additionally is to go to Project Settings and to remove this Show Check here because when we open the program we don't want to also open the InitArray. We would like to stay closed. And I'll save this project and I'll save it right here as an example like calibration_example and this is the example that we are developing.

Great! We have our Init block. Next, we'll move to the calibration.

Courses and lessons with this Tutorial

This Tutorial is used in the following courses and lessons

Image for FIRST LEGO League Competition. Constructing and Programming a Robot Base
  • 42
  • 249:53
  • 10
Image for Advanced Light/Color Sensor Calibration
  • 15
  • 0
  • 4
  • 3d_rotation 0