from machine import Pin
from hx711 import HX711
from time import sleep
from picozero import Speaker


LoadCell_SCK_pin = 8
LoadCell_DOUT_pin = 9
LoadCellDout = Pin(LoadCell_DOUT_pin, Pin.IN, pull=Pin.PULL_DOWN) 
LoadCellSck = Pin(LoadCell_SCK_pin, Pin.OUT)
LoadCell = HX711(LoadCellSck, LoadCellDout)

led_red = Pin(10,Pin.OUT)
led_orange = Pin(11,Pin.OUT)
led_green = Pin(12,Pin.OUT)
buzzer = Speaker(5)
LoadCell.read_avg()

'''
LoadCellOffset = LoadCell.read_avg()
print("Add weight ")
sleep(5)

while LoadCell.read()-LoadCellOffset<2000: #. The system waits for the load cell to detect a significant change (more than 2000 units from the offset) to ensure the weight is placed correctly.
    print(LoadCell.read()-LoadCellOffset)
    pass

print("Add weight 123")
sleep(5)
print("Starts")
sleep(5)
referenceLoad = LoadCell.read_avg()
LoadCellScaling = referenceLoad- LoadCellOffset

print(int(LoadCellScaling),int(LoadCellOffset))
'''
LoadCellScaling = 2058
LoadCellOffset =  0

buzzer_count =0
while True:
    print("------ Add weight Main weight ----")
    sleep(5)
    print("Org Starts .. ....")
    sleep(5)
    TBW = ((LoadCell.read() - LoadCellOffset)/LoadCellScaling)*4.9
    print(TBW)
    if TBW>-1 and TBW<0:
        TBW =0
    TBW = round(TBW, 2)

    if TBW > 2.5 and TBW < 4.7:
        led_orange.value(1)
        led_red.value(0)
        led_green.value(0)
        buzzer_count =0

    elif TBW > 4.7:
        led_red.value(1)
        led_orange.value(0)
        led_green.value(0)
        if buzzer_count == 0:
            for i in range(5):
                buzzer.on()
                sleep(1)
                buzzer.off()
                sleep(1)
        buzzer_count = 1
    else:
        led_green.value(1)
        led_orange.value(0)
        led_red.value(0)
        buzzer_count =0

    print("Weight :",TBW,"Kg")
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT