def HowClose(self): page = 0 while(True): time.sleep(1) self.readDistance = Ultrasonic.getReading1() if self.ValidateReading(): page += 1 for iteration in range(self.window): self.distanceArray[iteration] = self.distanceArray[iteration+1] self.distanceArray[self.window] = self.readDistance print("distanceArray: ", self.distanceArray) if (page >= 10): page = 0 self.averageDistance = np.mean(self.distanceArray) if self.averageDistance <= 18 and self.averageDistance >= 11: self.isCloseEnough = True else: self.isCloseEnough = False print("self.averageDistance: ", self.averageDistance) print("self.isCloseEnough: ", self.isCloseEnough) break
def HowClose(self): page = 0 while (True): time.sleep(1) # reading is taken every second self.readDistance = Ultrasonic.getReading1() if self.ValidateReading(): #true if 14 <= reading <= 200cm page += 1 # increaments after each reading for iteration in range(self.window): self.distanceArray[iteration] = self.distanceArray[ iteration + 1] self.distanceArray[self.window] = self.readDistance print("distanceArray: ", self.distanceArray) if (page >= 10): # after 10 readings page = 0 # reset the number of counted reading self.averageDistance = np.mean(self.distanceArray) if self.averageDistance <= 15 and self.averageDistance >= 1: # 1 cm to !5cm then object is close enough self.isCloseEnough = True else: self.isCloseEnough = False print("self.averageDistance: ", self.averageDistance) print("self.isCloseEnough: ", self.isCloseEnough) break