import time
import RPi.GPIO as GPIO
import smbus
GPIO.setmode(GPIO.BCM)
address=0x48
delta=20
time_list=[0,0,0]
threshold=[0,0,0]
basic_value_1=[]
basic_value_2=[]
basic_value_3=[]
bus=smbus.SMBus(1)
def loop_print():
flag1=0
flag2=0
flag3=0
while True:
bus.write_byte(address,0x40)
bus.write_byte(address,0x41)
bus.write_byte(address,0x42)
sound1=bus.read_byte_data(address,0x40)
sound2=bus.read_byte_data(address,0x41)
sound3=bus.read_byte_data(address,0x42)
if sound1:
print("the NO.1 power of the sound is %lf"%sound1)
if len(basic_value_1)<=10:
basic_value_1.append(sound1)
if len(basic_value_1)==10:
value=0
for i in range(10):
value+=basic_value_1[i]
threshold[0]=(value/10)+delta
if 0 not in threshold:
if sound1>threshold[0] and flag1==0:
time_list[0]=time.time_ns()/10**9
flag1=1
time.sleep(0.0)
if sound2:
print("the NO.2 power of the sound is %lf"%sound2)
if len(basic_value_2)<=10:
basic_value_2.append(sound2)
if len(basic_value_2)==10:
value=0
for i in range(10):
value+=basic_value_2[i]
threshold[1]=(value/10)+delta
if 0 not in threshold:
if sound2>threshold[1] and flag2==0:
time_list[1]=time.time_ns()/10**9
flag2=1
time.sleep(0.0)
if sound3:
print("the NO.3 power of the sound is %lf"%sound3)
if len(basic_value_3)<=10:
basic_value_3.append(sound3)
if len(basic_value_3)==10:
value=0
for i in range(10):
value+=basic_value_3[i]
threshold[2]=(value/10)+delta
if 0 not in threshold:
if sound3>threshold[2] and flag3==0:
time_list[2]=time.time_ns()/10**9
flag3=1
time.sleep(0.0)
if (0 not in time_list) and (0 not in threshold):
print(threshold)
print(time_list)
break
loop_print()