已屏蔽 原因:{{ notice.reason }}已屏蔽
{{notice.noticeContent}}
~~空空如也
from machine import Pin
import utime
from machine import UART

# Pin configuration
HX711_SCK = Pin(4, Pin.OUT)
HX711_DT = Pin(13, Pin.IN)
known_weight = 100  # Known weight in grams
uart = UART(2, 115200, rx=16, tx=17)
fire_struct = Pin(15, Pin.OUT, Pin.PULL_DOWN)

def HX711_Read():
    count = 0
    HX711_DT.init(Pin.IN)

    # Ensure DT line goes low
    timeout = utime.ticks_ms() + 1000  # 1 second timeout
    while HX711_DT.value() == 1:
        if utime.ticks_ms() > timeout:
            print("Timeout waiting for HX711 to go low")
            return None

    for _ in range(24):
        HX711_SCK.value(1)
        utime.sleep_us(1)
        count = count << 1
        HX711_SCK.value(0)
        utime.sleep_us(1)
        if HX711_DT.value() == 1:
            count += 1

    HX711_SCK.value(1)
    count = count ^ 0x800000  # Convert to signed integer
    utime.sleep_us(1)
    HX711_SCK.value(0)
    utime.sleep_us(1)

    return count

def HX711_Tare():
    tare_value = 0
    for _ in range(10):
        reading = HX711_Read()
        if reading is None:
            return None
        tare_value += reading
        utime.sleep(0.1)
    tare_value /= 10
    return tare_value

def Calibrate_Scale(tare_value, known_weight):
    raw_value = 0
    for _ in range(10):
        reading = HX711_Read()
        if reading is None:
            return None
        raw_value += reading
        utime.sleep(0.1)
    raw_value /= 10
    return (raw_value - tare_value) / known_weight

def Get_Weight(tare_value, calibration_factor):
    raw_value = HX711_Read()
    if raw_value is None:
        return None
    weight = (raw_value - tare_value) / calibration_factor
    return weight

if __name__ == "__main__":
    fire_struct.value(0)
    fire_Value=0
    tare_value = HX711_Tare()
    if tare_value is None:
        print("Failed to tare the scale")
    else:
        calibration_factor = Calibrate_Scale(tare_value, known_weight)
        if calibration_factor is None:
            print("Failed to calibrate the scale")
        else:
            print(f"Calibration Factor: {calibration_factor}")

            while True:
                weight = Get_Weight(tare_value, calibration_factor)
                if weight is not None:
                    uart.write(f"Weight: {weight:.2f} grams\n")
                else:
                    print("Failed to read weight")
                    
                # Check for UART data
                if uart.any():
                    state = uart.read().strip()
                    print(f"Received: {state}")
                    if state == b'1':
                        fire_Value = not fire_Value
                        fire_struct.value(fire_Value)
                utime.sleep(0.05)


文号 / 935561

十步芳草
名片专栏发私信
学术分 0
总主题 6 帖总回复 172 楼拥有证书:进士 机友 笔友
注册于 2022-11-05 16:48最后登录 2024-09-27 12:51
主体类型:个人
所属领域:无
认证方式:手机号
IP归属地:天津

个人简介

高中牲不想上学

文件下载
加载中...
{{errorInfo}}
{{downloadWarning}}
你在 {{downloadTime}} 下载过当前文件。
文件名称:{{resource.defaultFile.name}}
下载次数:{{resource.hits}}
上传用户:{{uploader.username}}
所需积分:{{costScores}},{{holdScores}}下载当前附件免费{{description}}
积分不足,去充值
文件已丢失

当前账号的附件下载数量限制如下:
时段 个数
{{f.startingTime}}点 - {{f.endTime}}点 {{f.fileCount}}
视频暂不能访问,请登录试试
仅供内部学术交流或培训使用,请先保存到本地。本内容不代表科创观点,未经原作者同意,请勿转载。
音频暂不能访问,请登录试试
投诉或举报
加载中...
{{tip}}
请选择违规类型:
{{reason.type}}

空空如也

插入资源
全部
图片
视频
音频
附件
全部
未使用
已使用
正在上传
空空如也~
上传中..{{f.progress}}%
处理中..
上传失败,点击重试
等待中...
{{f.name}}
空空如也~
(视频){{r.oname}}
{{selectedResourcesId.indexOf(r.rid) + 1}}
处理中..
处理失败
插入表情
我的表情
共享表情
Emoji
上传
注意事项
最大尺寸100px,超过会被压缩。为保证效果,建议上传前自行处理。
建议上传自己DIY的表情,严禁上传侵权内容。
点击重试等待上传{{s.progress}}%处理中...已上传,正在处理中
空空如也~
处理中...
处理失败
加载中...
草稿箱
加载中...
此处只插入正文,如果要使用草稿中的其余内容,请点击继续创作。
{{fromNow(d.toc)}}
{{getDraftInfo(d)}}
标题:{{d.t}}
内容:{{d.c}}
继续创作
删除插入插入
插入公式
评论控制
加载中...
文号:{{pid}}
加载中...
详情
详情
推送到专栏从专栏移除
设为匿名取消匿名
查看作者
回复
只看作者
加入收藏取消收藏
收藏
取消收藏
折叠回复
置顶取消置顶
评学术分
鼓励
设为精选取消精选
管理提醒
编辑
通过审核
评论控制
退修或删除
历史版本
违规记录
投诉或举报
加入黑名单移除黑名单
查看IP
{{format('YYYY/MM/DD HH:mm:ss', toc)}}
ID: {{user.uid}}