树莓派 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()

 

树莓派4B连接液位传感器通常涉及几个步骤: 1. **准备硬件**: - 购买适合的液位传感器,比如超声波、磁性或浮子式液位传感器。 - 确保传感器有合适的接口,可能是模拟信号、数字信号输出或是GPIO接口。 2. **物理连接**: - 将传感器的电源线接到树莓派的5V和GND端口。 - 如果传感器有GPIO接口,将其连接树莓派的GPIO脚上,根据传感器的说明书确定相应的引脚。 3. **软件配置**: - 使用树莓派的操作系统(如Raspbian),安装必要的库驱动程序来支持传感器。例如,如果你使用的是GPIO连接,可能需要安装Python的RPi.GPIO库。 - 编写Python代码来读取传感器的数据,这通常涉及到数据解析和转换,比如读取电平变化或超声波反射时间计算液位高度。 4. **编写监控程序**: - 编写一个程序来定期读取液位值,并根据需求显示或存储这个信息。你可以通过命令行输出,GUI界面或者将数据上传至云端。 ```python import RPi.GPIO as GPIO from time import sleep # 设置GPIO模式 GPIO.setmode(GPIO.BCM) # 连接液位传感器脚码 sensor_pin = 18 # 举例,实际根据传感器说明连接 GPIO.setup(sensor_pin, GPIO.IN) # 初始化为输入 while True: if GPIO.input(sensor_pin): # 检测输入是否为高 level = "full" # 液位满 else: level = "empty" # 液位空 print(f"Liquid level: {level}") sleep(1) # 等待一段时间再检测一次 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Programmer__C

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

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

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

打赏作者

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

抵扣说明:

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

余额充值