The other part I'm not sure about is when it knows to check the sensor value. What if I want 10 samples....or 80 samples etc? Possibly we would need to implement a millis timer to know when to sample?
If you want to spread the sampling over a period of time then don't use a for loop. A for loop, by its nature, locks you into it until all of its iterations have occurred. Actually, you can exit from it early but you can't then go back to where you left off.
Let the loop() function do what its name implies and use millis() for timing
Something like this maybe
averaging = false
doneMeasuring = false
counter = 0
total = 0
start of loop()
code here to set averaging to true to start measurements when required
if averaging equals true
if wait between measurements has passed // use millis() for timing
take a measurement
add measurement to the total
increment counter
if all measurments taken
set averaging to false to prevent more being taken
set doneMeasuring to true to trigger calculation
end if
end if
if doneMeasuring is true
divide total by counter to get average of measurements
set doneMeasuring to false to prevent more calculations until required
end if
end of loop