Last Updated: 28/07/2021

Lesson 11 - Arrays

Lessons >> Basics >> Lesson 11 - Arrays

Lesson Contents

What is an Array?
Array syntax
Visualising an Array
Multi Dimensional Arrays
Creating, Reading, Writing and Editing Values in an Array
Coomon Dangers and Errors
Memory Issues
Dangers of Over running the end of the Array

Example 1: arrayv1.ino


Click to Download code:arrayv1.ino

Single dimension Array
Array Syntax
Declaring and Array
Reading and editing values in the Array

 
/*  27/08/2021
 *  
 *  arrayv1
 * 
 *  Understand how to set up, read from, write to and edit a simple array
 *  
 *  Full video tutorial at www.digitaltown.co.uk
 *  
 *  No pins used
 *  
 */
//Create an integer array and populate it with values.

 int carPark[6] = {1,2,3,4,5,6};
 int carParkSlotCounter = 0;
 int mainCounter;

void setup() {
  int q;
  Serial.begin(9600);//Start Serial at 9600
  Serial.println("arrayv1...");//File name
  Serial.println(" ");//Blank line

  Serial.println("Print out original values");
  //NOTE first value is 0 so in a 6 item array the highest value is 5
  //Using the syntax 0 to smaller (<) than the number of items in the array prevents errors.
  for(q = 0;q< 6;q++){
    Serial.println(carPark[q]);
  }
  //Third element is 2 because the array starts at [0]
  Serial.println("Print out 3rd value");
  Serial.println(carPark[2]);

  Serial.println("Print out original values using variable carParkSlotCounter");
  for(carParkSlotCounter = 0;carParkSlotCounter< 6;carParkSlotCounter++){
    Serial.println(carPark[carParkSlotCounter]);
  }

  Serial.println("Print out 4th value using variable carParkSlotCounter");
  carParkSlotCounter = 3;
  Serial.println(carPark[carParkSlotCounter]);
  

  
}

void loop() {
  
  Serial.println("Change the values in the array");
  for(carParkSlotCounter = 0;carParkSlotCounter< 6;carParkSlotCounter++){
    //This line changes the values in the array
    carPark[carParkSlotCounter] = mainCounter;
    mainCounter++;
    //Print out the new value
    Serial.println(carPark[carParkSlotCounter]);
  }
  delay(2000);

  Serial.println("Add 100 to the values in the array");
  for(carParkSlotCounter = 0;carParkSlotCounter< 6;carParkSlotCounter++){
    //This line increases the value in the array by 100
    carPark[carParkSlotCounter] = carPark[carParkSlotCounter] + 100;
    //print out the new value
    Serial.println(carPark[carParkSlotCounter]);
  }
  delay(2000);

}

Example 2: arrayv2.ino


Click to Download code:arrayv2.ino

Creating and Using a Two Dimension Array

 
/*  27/08/2021
 *  
 *  arrayv2
 * 
 *  Understand how to set up, read from, write to and edit a 2 dimensional array
 *  
 *  Full video tutorial at www.digitaltown.co.uk
 *  
 *  No pins used
 *  
 */
//Create a 2 dimension integer array.
//[4] is the number of rows, [6] number of slots per row
 int carPark[4][6];
 int carParkSlotCounter = 0;
 int carParkRowCounter = 0;
 int mainCounter;

void setup() {
  Serial.begin(9600);//Start Serial at 9600
  Serial.println("arrayv2...");//File name
  Serial.println(" ");//Blank line
  

  
}

void loop() {
 
  Serial.println("Set the values in the array");
  for(carParkRowCounter = 0;carParkRowCounter<4;carParkRowCounter++){
    for(carParkSlotCounter = 0;carParkSlotCounter< 6;carParkSlotCounter++){
      //This line changes the values in the array
      carPark[carParkRowCounter][carParkSlotCounter] = mainCounter;
      mainCounter++;
      //Print out the new values seperated by a comma
      Serial.print(carPark[carParkRowCounter][carParkSlotCounter]);
      Serial.print(",");
    }
    //End of row so end line
    Serial.println(" ");
  }
  delay(2000);

  //Print out an individual value, 2nd row, 4th item
  Serial.println("Print out an individual value, 2nd row, 4th item");
  Serial.println(carPark[1][3]);//2nd row is [1] start from ZER0, 4th item therfore is [3]
  delay(2000);

  //Print out an individual value, 2nd row, 4th item after adding 100
  carPark[1][3] += 100;
  Serial.println("Print out an individual value, 2nd row, 4th item after adding 100");
  Serial.println(carPark[1][3]);//2nd row is [1] start from ZER0, 4th item therfore is [3]
  delay(2000);


  

  Serial.println("Add 1000 to the values in the array");
  for(carParkRowCounter = 0;carParkRowCounter<4;carParkRowCounter++){
    for(carParkSlotCounter = 0;carParkSlotCounter< 6;carParkSlotCounter++){
      //This line changes the values in the array
      carPark[carParkRowCounter][carParkSlotCounter] = carPark[carParkRowCounter][carParkSlotCounter] + 1000;
      //The line above can be written
      carPark[carParkRowCounter][carParkSlotCounter] += 1000;
      
      
      
      //Print out the new values seperated by a comma
      Serial.print(carPark[carParkRowCounter][carParkSlotCounter]);
      Serial.print(",");
    }
    //End of row so end line
    Serial.println(" ");
  }
  delay(2000);

}

Example 3: arrayv3.ino


Click to Download code:arrayv3.ino

Creating and Using a Three Dimensional Array

 
/*  27/08/2021

    arrayv3

    Understand how to set up, read from, write to and edit a 3 dimensional array

    Full video tutorial at www.digitaltown.co.uk

    No pins used

*/
//Create a 3 dimension integer array.
//[3] is the floors,[4] is the number of rows, [6] number of slots per row
int carPark[3][4][6];
int carParkSlotCounter = 0;
int carParkRowCounter = 0;
int carParkFloorCounter = 0;
int mainCounter;

void setup() {
  Serial.begin(9600);//Start Serial at 9600
  Serial.println("arrayv3...");//File name
  Serial.println(" ");//Blank line



}

void loop() {

  Serial.println("Set the values in the array");
  for (carParkFloorCounter = 0; carParkFloorCounter < 3; carParkFloorCounter++) {
    for (carParkRowCounter = 0; carParkRowCounter < 4; carParkRowCounter++) {
      for (carParkSlotCounter = 0; carParkSlotCounter < 6; carParkSlotCounter++) {
        //This line changes the values in the array
        carPark[carParkFloorCounter][carParkRowCounter][carParkSlotCounter] = mainCounter;
        mainCounter++;
        //Print out the new values seperated by a comma
        Serial.print(carPark[carParkFloorCounter][carParkRowCounter][carParkSlotCounter]);
        Serial.print(",");
      }
      //End of row so end line
      Serial.println(" ");
    }
    Serial.print("End of Floor: ");
    Serial.println(carParkFloorCounter);
  }
  delay(2000);

  //Print out an individual value, 1st floor, 2nd row, 4th item
  Serial.println("Print out an individual value, floor 1 2nd row, 4th item");
  Serial.println(carPark[0][1][3]);//1st floor [0] start from ZERO, 2nd row is [1] start from ZER0, 4th item therfore is [3]
  delay(2000);

  //Print out an individual value, floor 1 2nd row, 4th item item after adding 100
  carPark[0][1][3] += 100;
  Serial.println("Print out an individual value, 2nd row, 4th item after adding 100");
  Serial.println(carPark[0][1][3]);//2nd row is [1] start from ZER0, 4th item therfore is [3]
  delay(2000);




  Serial.println("Add 1000 to the values in the array");
  for (carParkFloorCounter = 0; carParkFloorCounter < 3; carParkFloorCounter++) {
    for (carParkRowCounter = 0; carParkRowCounter < 4; carParkRowCounter++) {
      for (carParkSlotCounter = 0; carParkSlotCounter < 6; carParkSlotCounter++) {
        //This line changes the values in the array
        carPark[carParkFloorCounter][carParkRowCounter][carParkSlotCounter] = carPark[carParkRowCounter][carParkSlotCounter] + 1000;
        //The line above can be written
        carPark[carParkFloorCounter][carParkRowCounter][carParkSlotCounter] += 1000;
        //Print out the new values seperated by a comma
        Serial.print(carPark[carParkFloorCounter][carParkRowCounter][carParkSlotCounter]);
        Serial.print(",");
      }
      //End of row so end line
      Serial.println(" ");
    }
    Serial.print("End of Floor: ");
    Serial.println(carParkFloorCounter);
  }
  delay(2000);

}

Example 4: arrayv4.ino


Click to Download code:arrayv4.ino

Arrays and Memory Errors

NOTE: This code is designed to give error examples

 
/*  27/08/2021

    arrayv4

    Dangers and Errors when using arrays. 
    Running out of memory.

    NOTE: Using an UNO will cause memeory issues. 
    An Arduino Due handles this without any problems because it has more memory.
    

    Full video tutorial at www.digitaltown.co.uk

    No pins used

*/
//Create a 3 dimension integer array.


//[2] is the floors,[16] is the number of rows, [500] number of slots per row
int carPark[2][16][500];//16000 integers stored...OK

//memory error example
//[2] is the floors,[17] is the number of rows, [500] number of slots per row
//int carPark[2][17][500];//17000 integers stored...Runs out of memory so fails to compile

int carParkSlotCounter = 0;
int carParkRowCounter = 0;
int carParkFloorCounter = 0;
int mainCounter;

void setup() {
  Serial.begin(9600);//Start Serial at 9600
  Serial.println("arrayv4...");//File name
  Serial.println(" ");//Blank line



}

void loop() {

  Serial.println("Set the values in the array");
  for (carParkFloorCounter = 0; carParkFloorCounter < 3; carParkFloorCounter++) {
    for (carParkRowCounter = 0; carParkRowCounter < 4; carParkRowCounter++) {
      for (carParkSlotCounter = 0; carParkSlotCounter < 6; carParkSlotCounter++) {
        //This line changes the values in the array
        carPark[carParkFloorCounter][carParkRowCounter][carParkSlotCounter] = mainCounter;
        mainCounter++;
        //Print out the new values seperated by a comma
        Serial.print(carPark[carParkFloorCounter][carParkRowCounter][carParkSlotCounter]);
        Serial.print(",");
      }
      //End of row so end line
      Serial.println(" ");
    }
    Serial.print("End of Floor: ");
    Serial.println(carParkFloorCounter);
  }
  delay(2000);

// Uncomment this block to see memory error
/*

//Print out an individual value, 1st floor, 2nd row, 4th item
  Serial.println("Print out an individual value, floor 1 2nd row, 4th item");
  Serial.println(carPark[0][1][3]);//1st floor [0] start from ZERO, 2nd row is [1] start from ZER0, 4th item therfore is [3]
  delay(2000);

  //Print out an individual value, floor 1 2nd row, 4th item item after adding 100
  carPark[0][1][3] += 100;
  Serial.println("Print out an individual value, 2nd row, 4th item after adding 100");
  Serial.println(carPark[0][1][3]);//2nd row is [1] start from ZER0, 4th item therfore is [3]
  delay(2000);




  Serial.println("Add 1000 to the values in the array");
  for (carParkFloorCounter = 0; carParkFloorCounter < 3; carParkFloorCounter++) {
    for (carParkRowCounter = 0; carParkRowCounter < 4; carParkRowCounter++) {
      for (carParkSlotCounter = 0; carParkSlotCounter < 6; carParkSlotCounter++) {
        //This line changes the values in the array
        carPark[carParkFloorCounter][carParkRowCounter][carParkSlotCounter] = carPark[carParkFloorCounter][carParkRowCounter][carParkSlotCounter] + 1000;
        //The line above can be written
        carPark[carParkFloorCounter][carParkRowCounter][carParkSlotCounter] += 1000;
        //Print out the new values seperated by a comma
        Serial.print(carPark[carParkFloorCounter][carParkRowCounter][carParkSlotCounter]);
        Serial.print(",");
      }
      //End of row so end line
      Serial.println(" ");
    }
    Serial.print("End of Floor: ");
    Serial.println(carParkFloorCounter);
  }
  delay(2000);

  */
}

Example 5: arrayv5.ino


Click to Download code:arrayv5.ino

Dangers of Over Running the end of an Array

NOTE: This skecth is designed to produce errors

 
/*  27/08/2021
 *  
 *  arrayv5
 * 
 *  Errors and dangers from over running the end of the array
 *  
 *  Examples of what NOT TO DO
 *  
 *  Full video tutorial at www.digitaltown.co.uk
 *  
 *  No pins used
 *  
 */
//Create an integer array and populate it with values.
 int carPark[6] = {1,2,3,4,5,6};
 int carParkSlotCounter = 0;
 int mainCounter;

void setup() {
  int q;
  Serial.begin(9600);//Start Serial at 9600
  Serial.println("arrayv5...");//File name
  Serial.println(" ");//Blank line

  
  //Array has 6 elements, in this loop we jump through 10 values
  Serial.println("Over running the end of the array");
  for(carParkSlotCounter = 0;carParkSlotCounter< 10;carParkSlotCounter++){
    Serial.println(carPark[carParkSlotCounter]);
  }
  
}

void loop() {
  //Uncomment the block below to see what happens when you change values outside the array boundary
  //This will not damage your Arduino but will probably cause a weird output to serial monitor
  //This is done as an example of what NOT TO DO
  /*
  Serial.println("Change the values in the array...exceeding array limits");
  for(carParkSlotCounter = 0;carParkSlotCounter< 10;carParkSlotCounter++){
    //This line changes the values in the array
    carPark[carParkSlotCounter] = mainCounter;
    mainCounter++;
    //Print out the new value
    Serial.println(carPark[carParkSlotCounter]);
  }
  delay(2000);

  Serial.println("Add 100 to the values in the array exceeding array limits");
  for(carParkSlotCounter = 0;carParkSlotCounter< 10;carParkSlotCounter++){
    //This line increases the value in the array by 100
    carPark[carParkSlotCounter] = carPark[carParkSlotCounter] + 100;
    //print out the new value
    Serial.println(carPark[carParkSlotCounter]);
  }
  delay(2000);
*/
}

Additional Resource Links

Don't forget to use the Reference in your Arduino IDE and look at Arrays for more help on this lesson.

Comments


This site has been designed to be child friendly, this means that comments cannot be added to videos or directly to the site.
To add a comment or ask a question please email the address in this image: and use Lesson 11 - Arrays as a reference.