 
 void loop()
 {
+  // Initiliase variables
   int avg;
-  int lidar_array[average_trials];
-  populate_lidar_array(lidar_array);
-  avg = average_lidar_array(lidar_array);
+  int lidar_array[average_trials]; // Create array
+  populate_lidar_array(lidar_array); // Pass array into populate_lidar_array function and fill with distance values
+  avg = average_lidar_array(lidar_array); // Average array values and store in avg variable
   Serial.print(avg);
   Serial.println();
 }

{
  for (int i = 0; i < average_trials; i = i +1)
  {
    lidar_array[i] = get_distance();
  }
}

  int sum = 0;
  for (int i = 0; i < average_trials; i = i + 1)
  {
    // Sum each value in the array
    sum += lidar_array[i];  
  }
  int result; // Work out mean average and return result
  result = ((float) sum) / average_trials;
  return result;
}
  
int get_distance()
{
  // Get distance in millimeters from lidar sensor
  return sensor.readRangeSingleMillimeters();
}

