Following the Advanced Sensor Calibration course section, it is time to extract the logic for getting a calibrated value into a new block. This block will have an input and on this input, we give the port number. The block will return the calibrated value for this port number. We've built all the other blocks only because of this almost final video here.

**#646**- 04 Oct 2017
- 6:14

### Find the min and max from the array

Here is the challenge - give the port number of the sensor, find the index of the minimum and maximum values in the array.

For port 1 the min and max are in cells 0 and 1

For port 2 the min and max are in cells 2 and 3

For port 3 the min and max are in cells 4 and 5

For port 4 the min and max are in cells 6 and 7

How can we come up with a formula here? For the minimum values, we want a calculation that given the following inputs will give us the following outputs:

Input 1, Output 0

Input 2, Output 2

Input 3, Output 4

Input 4, Output 6

The correct formula would be

Output = Input *2 -2

This means

1 -> 0 (1*2 -2)

2 -> 2 (2*2 -2)

3 -> 4 (3*2 -2)

4 -> 6 (4*2 -2)

Beautiful!

Here it is as an EV3-G sequence of blocks

### Max formula

For max, given the following inputs, we need the following outputs:

Input 1, Output 1

Input 2, Output 3

Input 3, Output 5

Input 4, Output 7

The correct formula would be:

Output = Input *2 -1

This means

1 -> 1 (1*2 -1)

2 -> 3 (2*2 -1)

3 -> 5 (3*2 -1)

4 -> 7 (4*2 -1)

Beautifullllll!

### English

In the previous tutorial we stopped at calibrating for about a second, moving and stopping on the next black line. But we did this with some very ugly hardcoded values that we have in the program and there are some very smart and beautiful ways to implement the program that involves some calculation and in this tutorial we'll do these calculations to make our program much more readable and much more powerful. Here is what the problem with our program is that we have these two hardcoded values - we have 2 and 3. And we can use only sensor 2. So, if for example we need to change this to 3, then we need to change this to 4 and this to 5. And if we want to use sensor 1, we must change here to 1 and then we must change here to 0 and here to 1. And there is a formula involved here - how am I calculating this? So, it's much more easier and smarter to use a mathematical block that will do the formula for us. So, if we have the sensor that we want to read - we know its port number and we put this into a math block like this we need to do a small formula. And the formula is the following. The minimum value is actually (the port number x 2 - 2). So, for 1 this will be 0. For 2 this will be 2. For 3 this will be 4. And for 4 this will be 6. So, (4 x 2 - 2) is actually 6. And we have this calculation to get the index - I know it's getting a little bit more advanced but this is where the program really gets powerful and we have the sensor port and we multiply this sensor port by 2 - 2 and we get the index and this is the index that we want to read for our MIN value. And for MAX value we have a similar formula

because it is just the next cell. And the next cell is (a x 2 - 1) So, for the first cell this will be index 1. And for the second cell this will be index 3. And for the third cell this will be index 5. So, in this way we don't have any hardcoded values in our program. And now we are free to extract this into a new block. I'll just select and I want to select to here. So, from the read sensor value - the raw value to the advanced mathematical block and create a new block. And let's call this new block GetCalValue and for this we'll use the icon of a color sensor because we are calibrating color sensors, we press finish.

We attach them one next to the other like this and like this and we can then remove this constant and now we have a block that can specify directly the port index. So, if we set 1, this will give us the value for port 1. If we set 2, this will give us the value for port 2. And if we set 3, this will give us the value for port 3. This is the calibrated value and we compare this value with 90 so it's important to mention that 0 means something very black and 100 means something very white. And that's why we are moving forward until we detect something that's less than white and in our case on our table this means the black line. And this is the final program that we have. There are a couple of differences between this program that we've developed and the program that you'll find attached for example right here in the program that you'll find attached we have the GetCalibratedValue as a block not GetCalValue and in the program that is using it you can see that there are some nice icons here and some nice names so I'll leave this to you as a task to extract the block with the port numbers with the port names, etc. and to have a nice block. And you can use this program directly, you can import it in your program and you can you the InitArray, the GetCalibratedValue and the CalibrateMinMax blocks. And in this way you won't have any problems with different values that are detected from different sensors at different parts of the competition or in different venues and in different parts of the preparation. So, you'll just calibrate not depending on the EV3 calibration but doing your own calibration that you have actually some control over.