树莓派 4B 三声音传感器AO模块连接与高精度时间输出

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()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Programmer__C

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值