print("Pest Control System Using Motion Sensor, Ultrasonic Sensor & Servo Motor")
print("Date: 1/11/2023")
print('Created by AHH')
import ultrasonic_library
import servo_library
import oled_library
from machine import Pin, PWM, SoftI2C
from utime import sleep
#Pin declaration
TRIG_pin = Pin(14, Pin.IN)
ECHO_pin = Pin(27, Pin.OUT)
motion_sensor = Pin (13, Pin.IN)
Servo_Pin = (Pin (4, Pin.OUT))
Pin_scl_sda = SoftI2C (scl=Pin (22), sda=Pin (21))
Buzzer_Pin = Pin (12, Pin.OUT)
Peace_led = Pin (15, Pin.OUT)
Alert_led = Pin (18, Pin.OUT)
#Creat an object name using OOP (Object Oriented Programming)
#library name.class name
detector = ultrasonic_library.HCSR04(trigger_pin=TRIG_pin, echo_pin=ECHO_pin)
monitor = oled_library.SSD1306_I2C(width=128, height=64, i2c=Pin_scl_sda)
door_trap = servo_library.Servo(pin = Servo_Pin) # Do like this for using Servo Motor
#Main Program
while True:
#OLED PART FOR MOTION SENSOR
pest_detacted = motion_sensor.value()
if pest_detacted == 1:
monitor.fill (0) #0=black, 1=white
monitor.text ("Pest is coming!",10,30,1)
monitor.show ()
Peace_led.off()
for b in range (5):
sound_buzzer = PWM (Buzzer_Pin, freq = 1200, duty = 25)
sleep (0.5)
sound_buzzer.duty(0)
sleep (1)
else:
monitor.fill (0)
monitor.text ("No pest around",10,30,1)
monitor.show()
Peace_led.on()
sleep (10)
#ULTRASONIC SENSOR PART AND OLED FOR ULTRASONIC
pest_distance_cm = detector.distance_cm()
if pest_distance_cm <= 20:
door_trap.move (90)
Peace_led.off()
for a in range (10):
Alert_led.on()
sleep (0.5)
Alert_led.off()
sleep (0.5)
for b in range (10):
sound_buzzer = PWM (Buzzer_Pin, freq = 1200, duty = 25)
sleep (0.5)
sound_buzzer.duty(0)
sleep (1)
monitor.fill(0)
monitor.text("Pest is captured", 0,10,1)
monitor.text("Trap door close", 10,30,1)
monitor.show()
else:
door_trap.move (0)
Alert_led.off()
Peace_led.off()
monitor.fill(0)
monitor.text("The pest is near", 0,10,1)
monitor.text("Trap door open", 10,30,1)
monitor.show()
sleep (10)