In this tutorial, we would implement a program that finds the minimum and maximum value detected by the sensor and stores this two values in an array.
- 04 Oct 2017
- Mindstorms Competitions, Mindstorms Sensors, Light & Color Sensor, Programming
- D.Staykov, K.Mitov
Sensor Raw Value
First, we detect the raw sensor for the given sensor. The block is available in the "advance" blocks palette.
Compare of previous Min
Finding minimum and maximum values is one of the first programs in every programming course. In the implemented program, we constantly compare the currently detected value from the sensor with the value that is already considered a minimum. If the currently detected value is less than the known minimum that the currently detected value should become the minimum.
The comparison as pseudo code looks like this
if currentValue < knownMinimum then currentValue is set to the value of knownMinimum
The same logic is implemented in the program with the only difference that the knownMinimum is stored in an array.
Storing min and max in an array
The array is organized in the following way. Cell 0 stores Minimum for Sensor 1. Cell 1 stores Maximum for Sensor 1.
Cell 2 stores Mimimum for Sensor 2. Cell 3 stores Maximum for Sensor 2.
So the odds store the minimum values and the evens stores the maximums.
In the previous tutorial we stopped at the initialized block where we start the program, it initializes the array, shows the value of this array on the brick screen and now it's time to actually move forward and find the minimum and maximum value for a single sensor - to calibrate a single sensor. If we look at the program that we've provided with the course the calibration EV3, you can see the whole logic of the program and we've already in our example that we are developing during the tutorial we've already implemented the InitArray. Next, we need to do the calibration. And this is the part with the calibration. So, we'll do it in a very simple way for a single sensor. And then we'll do it for more than one sensor. First, what we want to do is to move forward. And I'll just move forward with both of the motors for unlimited amount of time and just forward with a power of 20.
Then we must do some logic in a loop. Detect the minimum and maximum. And we'll do this for exactly one second. There are many different ways to stop the calibration but the simplest way is to just wait for about a second. And then we stop with both motors. We just set off here. We start moving forward, we do some calibration and we'll have to implement the calibration in this loop, we do the calibration for about 1 second and then we stop. To do the actual calibration let's first look at the example at the program that we've attached to the course and it has a new block that's called CalibrateMinMax. And if you enter into this new block,
we have 4 different min and max values that are calibrated. And the whole logic is actually it's pretty large you can see
but you can also see that there is a repeating pattern.
These are switches. It is a repeating pattern. And we'll do it for a single sensor. Let me just zoom in. The logic for calibrating the first sensor is actually in these few blocks here. And the rest are the same blocks but just for the other sensors. This is for sensor on port 1 and the rest are for port 2, port 3, port 4. And we have the whole block calibrating the minimum and maximum for the different sensors. How do we do this? Let's just go through the solution that's already implemented and then we'll implement it ourself. First, we read the value from the sensor - the raw value then we compare it with the minimum value that we already have in the array and if we found a new value that's less than the minimum value, we set this value as the current minimum value. And we do the same thing for the maximum. So, we get the value from the sensor and if this value from the sensor is more than the current maximum value then we just set this new value to the current maximum value. And that's basically the logic. Let's implement it ourself. Going to our example program in the loop what I'll do is just read the value - the raw sensor value on let's say in port 2 because I have a light sensor detected on port 2. Then I must read the value from the array. And to read it from an array I first need the block for an array and we read a numeric array and this is the MinMax Array then we have an operation for reading this array and the operation is to read the value in the first cell - on cell 0. Then we must compare these 2 values. The way we compare them - we use a compare block - right here. And we compare the value from the array - the current minimum value with the value from the sensor. And if the value from the array is greater than the current value, this means that we have a new minimum value. For this we must check with a switch. Configure this switch for logic, make it flat, attach the logic.
And if value 'a' is greater than value 'b' this means that the array minimum value is greater than the current minimum value, we must store the current minimum in the array. The way we store it - we take a variable,
we take an operation, we read the numeric array,
it's a single array and then we write in the first cell a value and this value is the value from the sensor. And here I'll just zoom out, take the value from the sensor and add it right here. So, this is our block for we have the minimum variable then we have this operation and after we write the value we must write a whole array in a new array block right here. And we write a numeric array MinMax
and in this way we read a value from an array and we write it back. So, again this is only for calibrating the minimum value for sensor 2. While moving we'll store this because these sensor 2 let's store it in the second cell not the 0 cell because the 0 and 1 are for the first sensor but for the second sensor we have the second and the third cell. And in this way we store the minimum value in the second cell - in the cell with index 2.