В момента ресурсът е наличен само на английски

How to display on the screen all the values from an array, using an "Iteration" Pro Preview

In this tutorial, we show you how to display all the values from an EV3-G array on the EV3 brick display. We are using an array Read Operation along with a loop. We are also detecting the loop counter and using it as an index to an array. It's basically the only viable way to do it. The process is commonly referred to as - "Iteration over an array" (although we still do not have an iterator in the programming language, we promise that one day we would do the super advance videos on "iteration" using the LEGO MINDSTORMS robots")  

  • #644
  • 04 Oct 2017
  • 6:05

Loops have a counter

Each loop in the EV3-G software and in almost all other programming languages have a counter. The counter counts the number of times the body of the loop is executed. The number of times you 'loop'. When you do it for the first time you've looped 0 times. You have executed the body of the loop exactly 0 times. When you enter the loop a second time the counter is 1 because you've executed the loop 1 time and you are now at the second execution.

So the counter starts from 0 and increments. This is very convenient considering that arrays indices also start from 0. 

On the picture below we set the loop to increment to 8 and after the 8 execution of the loop, it will stop. This means the counter will have the following values - 0,1,2,3,4,5,6,7. These are 0 to 7. Because 0 to 7 are 8 executions of the array.

Arrays start from 0

The index of the first cell in an array is 0. The index of the second cell is 1. The index of the third cell is 2. This is very convenient when using counter in a loop. You can directly take the output from the counter and add it as an input in the array index. In this way, on each iteration of the loop, you would index the next cell. 

Calculate position on the screen

Each new value should be at a new position on the screen. If we do not place the text on a new position we will see only the last value. What we are doing is moving from up to down and showing each value below the previous. We are incrementing the Y value of the display. To do the incrementation we again use the counter. By multiplying the value of the counter is 15 we get the new values for the Y. The coordinates on which the values will be displayed are 

Counter -> Y position

0 -> 0 (0*15)

1 -> 15 (1*15)

2 -> 30 (2*15)

3 -> 45 (3*15)

4 -> 60 (4*15)

5 -> 75 (5*15)

6 -> 90 (6*15)

7 -> 105 (7*15)

Here is the actual calculation with the EV3-G software.