/* 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); }