from machine import Pin ,ADC
from hx711 import HX711
import utime
import machine
from time import sleep , sleep_ms
from picozero import Speaker
import math ,onewire, ds18x20 , utime
#-------------LOADCELL FOR WEIGHT-------------
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_load = Pin(26,Pin.OUT)
LoadCell.read_avg()
LoadCellScaling = 2058
LoadCellOffset = 0
#-------------ULTRASONIC FOR VOLUME-------------
trig1 = Pin (2, Pin.OUT)
echo1 = Pin (3, Pin.IN)
#-------------DS18B20 and NCT FOR TEMP-------------
BETA = 3950
R1 = 10000
V_REF = 3.3
adc = ADC(Pin(27))
ds_pin = Pin(13)
ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))
roms = ds_sensor.scan()
#-------------------RGB LED's
led_red = Pin(10,Pin.OUT)
led_blue = Pin(11,Pin.OUT)
led_green = Pin(12,Pin.OUT)
#-------------------BUZZER
buzzer = Speaker(5)
button = Pin(21,Pin.IN,Pin.PULL_UP)
def dis1():
timet1 = 0
trig1.low()
utime.sleep_us(2)
trig1.high()
utime.sleep_us(5)
trig1.low()
while echo1.value() == 0:
signal1off = utime.ticks_us()
while echo1.value() == 1:
signal1on = utime.ticks_us()
timet1 = signal1on - signal1off
return timet1
buzzer_count = 0
choice = 1
while True:
print("------ Add weight Main weight ----")
print("Org Starts .. ....")
TBW = ((LoadCell.read() - LoadCellOffset)/LoadCellScaling)*4.9
time1 = dis1()
sleep(1)
cm1 = time1 * 0.034 / 2
# Printing Distance
print ("Distance : ", cm1, "cm")
print("Dis-Choice ",choice)
analog_value = adc.read_u16()
voltage = (analog_value / 65535) * V_REF
resistance = R1 / ((V_REF / voltage) - 1)
kelvin = 1 / (1 / 298.15 + 1 / BETA * math.log(resistance / 10000))
celsius = kelvin - 273.15
ds_sensor.convert_temp()
sleep_ms(200)
for rom in roms:
Temp=ds_sensor.read_temp(rom);
print("Reading from D18B20 Temperature: {:.2f} ℃".format(Temp))
print("Reading from NTC Temperature: {:.2f} ℃".format(celsius))
Average_Temp = (Temp+celsius)/2
print("Average Temperature: {:.2f} ℃".format(Average_Temp))
if TBW>-1 and TBW<0:
TBW =0
TBW = round(TBW, 2)
# Printing TBW
print("Weight :",TBW,"Kg")
if TBW > 4.7:
led_red_load.value(1)
print("Overweight...!! ")
else:
led_red_load.value(0)
if button.value() == 0:
if choice<3:
choice +=1
print(choice)
buzzer.on()
sleep(0.5)
buzzer.off()
else:
choice =1
print(choice)
buzzer.on()
sleep(0.5)
buzzer.off()
# if choice 1 == buzzer "on" level is 45 cm
# if choice 2 == buzzer "on" level is 25 cm
# if choice 3 == buzzer "off"
if cm1 <= 5:
led_red.value(0)
led_blue.value(1)
led_green.value(1)
if buzzer_count == 0 and choice == 1:
for i in range(5):
buzzer.on()
sleep(1)
buzzer.off()
sleep(1)
buzzer_count = 1
elif cm1 < 25 and cm1 > 5:
led_blue.value(1)
led_red.value(0)
led_green.value(0)
if buzzer_count == 0 and choice == 2:
for i in range(5):
buzzer.on()
sleep(1)
buzzer.off()
sleep(1)
buzzer_count = 1
if choice == 1:
buzzer_count =0
else:
led_green.value(0)
led_blue.value(1)
led_red.value(1)
buzzer_count =0