You cold look into the code of TPinInput from the TDuino library, which can bot calculate a buffered average or a non-buffered average - bot non-blocking