I'm developing a python script on a raspberry pi with a distance sensor. My code is working and i can reach the distance but I want to avoid some false positive/negative so I would like to get the average of the last three data.
Here is my code:
while True:
distance= misura() # GETTING THE REAL DISTANCE IN CM
print "Distanza : %.2f" % distanza
avg_distance = <something> # <- HERE I NEED SOMETHING ELEGANT
if (avg_distance > 30):
print "NOT PRESENT"
else:
print "PRESENT"
time.sleep(1.5)
I would like some function (maybe based on a list?) that returns the average of the last three (or n) distances
IMPORTANT: I don't want to store ALL the values because this script will run for days and days