#Smart dustbin
#Open the dustbin if the dustbin is still empty and there's a trash to throw.
#Close and display "FULL" if the dustbin is full.
#The height of the dustbin is 50cm.
#The distance between the trash and the dustbin should be 40cm to open the dustbin in case it's empty.
from machine import Pin, PWM
import utime
LedRed = Pin(0, Pin.OUT)
LedGreen = Pin(1, Pin.OUT)
trig1 = Pin(19, Pin.OUT)
echo1 = Pin(20, Pin.IN)
trig2 = Pin(21, Pin.OUT)
echo2 = Pin(22, Pin.IN)
servo = PWM(Pin(14))
servo.freq(50) #Servo motor's frequency is 50Hz
SegG1 = Pin(2, Pin.OUT)
segF1= Pin(3, Pin.OUT)
segA1= Pin(4, Pin.OUT)
segE1= Pin(5, Pin.OUT)
segF2= Pin(6, Pin.OUT)
segE2= Pin(7, Pin.OUT)
segD2= Pin(8, Pin.OUT)
segC2= Pin(9, Pin.OUT)
segB2= Pin(10, Pin.OUT)
segF3= Pin(11, Pin.OUT)
segE3= Pin(12, Pin.OUT)
segD3= Pin(13, Pin.OUT)
segF4= Pin(16, Pin.OUT)
segE4= Pin(17, Pin.OUT)
segD4= Pin(18, Pin.OUT)
#Display's functions:
def F(): # Display F on seven segment 1
SegG1.off()
segF1.off()
segA1.off()
segE1.off()
def U(): # Display U on seven segment 2
segF2.off()
segE2.off()
segD2.off()
segC2.off()
segB2.off()
def L1(): # Display L on seven segment 3
segF3.off()
segE3.off()
segD3.off()
def L2():
segF4.off()
segE4.off()
segD4.off()
def Fx(): # hide F
SegG1.on()
segF1.on()
segA1.on()
segE1.on()
def Ux(): #hide U
segF2.on()
segE2.on()
segD2.on()
segC2.on()
segB2.on()
def L1x(): #hide L
segF3.on()
segE3.on()
segD3.on()
def L2x():
segF4.on()
segE4.on()
segD4.on()
#The ultrasonic1 measures the distance between already existing trash in the dustbin and the roof of the dustbin.
#This ultrasonic1 is placed on the roof of the dustbin.
def ultrasonic1():
Ton1= Toff1 = t1 =0
distance1 = 0
trig1.off()
trig1.on()
utime.sleep_us(10)
trig1.off()
while(echo1.value()==0):
Toff1=utime.ticks_us()
while(echo1.value()==1):
Ton1= utime.ticks_us()
t1= (Ton1 - Toff1)/2 # t1 is the duration related to the distance1 which is the distance between the trash and the roof of the dustbin.
distance1= int(t1 * 0.034) # 0.034 is the speed of sound.
return(distance1)
#The ultrasonic2 measures the distance between the person that will throw the trash and the dustbin.
#This ultrasonic2 is placed in the front of the dustbin.
def ultrasonic2():
Ton2= Toff2 = t2 =0
distance2 = 0
trig2.off()
trig2.on()
utime.sleep_us(10)
trig2.off()
while(echo2.value()==0):
Toff2=utime.ticks_us()
while(echo2.value()==1):
Ton2= utime.ticks_us()
t2= (Ton2 - Toff2)/2 # t2 is the duration related to the distance2 which is the distance between the person with the trash and the dustbin.
distance2= int(t2 * 0.034) # 0.034 is the speed of sound.
return(distance2)
def map(value, inMin , inMax , outMin , outMax):
value = max(inMin , min(inMax ,value))
return(value- inMin) * (outMax-outMin) /(inMax - inMin) + outMin
#Rotate() closes or opens the dustbin, depending on the case.
def rotate(angle):
servo.duty_u16(int(map(angle , 0 , 180, 1500 , 8000 )))
utime.sleep(1)
#The distance between the person and the dustbin is 40cm in order to open the dustbin.
while True:
d1=d2 = 0
d1= ultrasonic1()
d2= ultrasonic2()
if(d2<=5): # the dustbin is FULL.
F()
U()
L1()
L2()
LedRed.on()
LedGreen.off()
rotate(0)
utime.sleep_ms(70)
elif(d1 <= 40 )and( 5<d2<=50 ): #The dustbin is empty.
Fx()
Ux()
L1x()
L2x()
LedRed.off()
LedGreen.on()
rotate(90)
utime.sleep(1) #The dustbin will be opened for 1s.
rotate(0)
utime.sleep_ms(70)
else:
Fx()
Ux()
L1x()
L2x()
LedGreen.on()
LedRed.off()
rotate(0)
utime.sleep_ms(70)