 # WRO Junior-High 2015. Treasure Hunt. Part 4. Program With Arrays ProPreview

We would continue from the previous program where we used variables and we would change this to arrays.

• #201
• 24 Apr 2016
• 8:59

### Arrays

We make a nice explanation on what arrays are and how they work. How could they be indexed, access, how do we read from them and write to them.

### Complete playlist

Find the complete playlist at WRO Junior-High 2015. Treasure Hunt

### WRO Box Collecting Robot

Building instructions for this LEGO Mindstorms EV3 robot used in the World Robotics Olympiad Junior competition

### English

- Previously we stopped at the program for the World Robotics Olympiad 2015 Treasure Hunt. And this program was detecting the colors in the initial position that we must decode so that we know the rows and the columns. And we were using variables, and this was not a nice solution. So in this video we are using arrays. And this will be a small introduction to arrays.

For this program we are using arrays, but we again extract some of the logics in different blocks and we have four blocks. First we are starting and moving the motor forward, then we have collect grid colors, collect first coordinate then we translate row and translate column and now describe each of the blocks. Just to mind you here is the field. So on the field we have different colors-Different... Not this tool. I would like to use the box tool. So we have first, second, third, fourth, and we detect the colors of each of these box. And if the first box is red, this means that our first row is red, then we have blue, yellow, green, and we have a total of eight boxes on the field. The first four are for the rows. The second four are for the columns. And then we have two more boxes. And collecting grid colors will move through the first eight of these boxes and detect the colors of these boxes. Collecting first coordinate will detect the colors of the last two boxes. And now I will enter into the "Collect Grid Colors." And Collect Grid Colors it has two blocks, "Collect Row Colors," and "Collect Column Colors." The Collect Row Colors uses the following algorithm and this is where it gets interesting. Again we have the sensor moving forward while we are on white. Then we detect the color, we wait for 0.01 seconds and then we detect the color. We are over the color with the sensor. And we use this array function. What we are doing with this array function? It works like this: we have a variable and this variable is row-array. And this variable in the beginning is empty. So we have an empty array. You can imagine it like this. We have an empty array. And what we are doing is the following: we detect the color and the value is, let's say, five. And for five we would like to store five as the first value in the array. Then we move in the cycle and we detect again a color. And let's say the next color is two, and third color is three, and then we have four. So we have an array with four different cells. And each cell of this array, it has an index. The first cell of the array has an index of zero. This is in every program language in this world, probably. The second is one, the third is two, and the fourth is three. So we have the values that we are storing in the array and the indexes of these values. And the array operation, it uses the indexes and the value. Now I hope that you get the difference between an index and a value that's in this cell. The index tells us what's the order of this cell. And the value, what is the value in this cell of the array. For our array we have row-array, it's initially empty. And we pass this as an input array to the array block, then we pass the value. The value is the value detected by the color sensor. And the index is the number of loops that we are currently doing. Initially we are in our zero loop so we haven't done any loops in this loop and we're at the zero. So initially the value will be zero for index. The index will be zero. The value if it is red, i think that red is five, five for the value. And the array as a result will have the following structure. It will be... I'll just draw a box. One box with the value of five in it. And this is our array after the first iteration of this cycle. Again after the second iteration, if we detect a color with the value of two, then we'll have the second box of our array with the value of two. That's it, two, two, two, two, like this. And now we fill five, two, three, four. All the values in our array. And we have four of them. The same way we do the algorithm for the "Collect Column Colors." And we have the Collect Column Colors. At the end of these two blocks, the Collect Grid Colors, we have the color sensor. We know the row-array and we know the column-array. Next we're collecting the last two colors that are giving the first coordinates and then we do this special "Translate Rows" and "Translate Columns." What are they doing? The basic idea is the following: we have, we have the value of five for the first column. This means that the first row has a value of five which means that it is red. So when we have five as the first value, we need to find the number of this row, that this row relates to one. And this is the purpose of this Translate Rows. If you have any questions on this, I recommend that you look at the code, and we'll be happy to discuss them in the comments. For now, let's just run the program and see that it works in exactly the same way. And while we are solving the competition, you'll learn how to use these four blocks and you get more details about them. Running the program,

the robot detects the different colors, then it rotates.

And now if I take the robot, we can see on the display the values for the different rows and different columns, and what are the two final values that are the first coordinates of the intersection. Let me just check them. We can see that the rows are five, two, three, four. So the colors that we have detected are five, four, three...five, two, three, four. And for the columns we have five, two, four, and three. So this is for our program. In the next videos, we'll try to use the coordinates detected by the robot so that we can move on the field and collect some of the treasures.