//Example of 2-Dimensional arrays
/*
So as a refressher, we can create an array of any data type
by adding 2 brackets after the variable named. From there,
you can either set that equal to a list closed in curly braces
separated by commas, type the number of items in between the square
braces, or do both
*/
int array_1[] = {1,2,3,4,5};
int array_2[5];
int array_3[5] = {1,2,3,4,5};
/*
Similarly, we can think of a 2D array as an 'array of arrays'. Basically, each
element of the array is another array.
So, in order to do that, we use 2 sets of square braces, but the principal is still the same
however, we do need to tell the arduino how long the internal arrays are, so in this case
it would be 2 (for you, it will be the number of stairs you have)
*/
int array_2D[][2] = {{1,2},{3,4}};
/*
You could also visually change how this looks in the Arduino IDE to make it more readable:
int array_2D[][] = {
{1, 2},
{3, 4}
};
*/
void setup() {
Serial.begin(9600);
/*In a 2D array, accessing an element is done the same way
Since the 2D array is just an array that contains more arrays
the first brace selects which of the internal arrays you are using
For example, the following: */
array_2D[0]; // This line represents the first array {1, 2}
array_2D[1]; // This line represents the second array {3, 4}
//From there, I can access each individual element by using a second
//set of brackets with the desired location:
Serial.println(array_2D[0][0]);
Serial.println(array_2D[0][1]);
Serial.println(array_2D[1][0]);
Serial.println(array_2D[1][1]);
/*
Another way of thinking about this is by row and column!
The first number in the brackets is the row of the item, and the second
is the column. Hence why array_2D[1][0] gave you the number 3, since it
is in the '1' row (remember arrays start counting from zero) and the 0th
column:
col 0 col 1
row 0 {{ 1 , 2 },
row 1 { 3 , 4 }}
*/
}
void loop() {
//Hope that helps!
}