print("This device will detect motion")
print("4/12/2023")
print("created by ZUNIE")

from machine import Pin, PWM, I2C
from utime import sleep
import UltraSONIC # Replace with the correct library for your ultrasonic sensor
from SERVOOO # Replace with the correct library for your servo
import OLEDDDD  # Replace with the correct library for your OLED

blade = SERVOOO.Servo(pin = Servo_Pin)
skrin = OLEDDDD.SSD1306_I2C(width=128, height=64, i2c=Pin_scl_sda)
sonic = UltraSONIC.HCSR04(trigger_pin = TRIG_pin , echo_pin = ECHO_pin)
# Set up servo
servo_pin = 13
blade = Servo(pin=servo_pin)

# Set up motion sensor
motion_pin = 14
motion_sensor = Pin(motion_pin, Pin.IN)

# Set up LDR Photoresistor
ldr_pin = 34
ldr_sensor = Pin(ldr_pin, Pin.IN)

# Set up buzzer
buzzer_pin = 15
buzzer = PWM(Pin(buzzer_pin), freq=440, duty=0)  # Set initial duty cycle to 0

# Set up ultrasonic sensor
ultrasonic_trigger_pin = 5
ultrasonic_echo_pin = 4
sonic = HCSR04(trigger_pin=ultrasonic_trigger_pin, echo_pin=ultrasonic_echo_pin)

# Set up LEDs
motion_led_pin = 27
darkness_led_pin = 18
approaching_led_pin = 26

motion_led = Pin(motion_led_pin, Pin.OUT)
darkness_led = Pin(darkness_led_pin, Pin.OUT)
approaching_led = Pin(approaching_led_pin, Pin.OUT)

# Set up OLED display
i2c = I2C(0, sda=Pin(21), scl=Pin(22))
skrin = SSD1306_I2C(width=128, height=64, i2c=i2c)

def move_servo(angle):
    blade.angle(angle)

def read_ldr():
    return ldr_sensor.value()

def sound_buzzer():
    buzzer.duty(50)  # Set duty cycle to 50% for half a second
    utime.sleep(0.5)
    buzzer.duty(0)   # Turn off the buzzer

while True:
    # Check motion sensor
    if motion_sensor.value() == 1:
        print("Motion detected!")
        move_servo(90)  # Move servo to 90 degrees
        sound_buzzer()  # Sound the buzzer
        motion_led.value(1)  # Turn on motion LED
        skrin.text("Motion Detected!", 0, 0)
        skrin.show()
        utime.sleep(2)  # Wait for 2 seconds
        motion_led.value(0)  # Turn off motion LED
        skrin.fill(0)  # Clear OLED display

    # Check LDR Photoresistor
    ldr_value = read_ldr()
    print("LDR Value:", ldr_value)
    if ldr_value == 0:
        print("Darkness detected!")
        move_servo(0)   # Move servo to 0 degrees
        sound_buzzer()  # Sound the buzzer
        darkness_led.value(1)  # Turn on darkness LED
        skrin.text("Darkness Detected!", 0, 0)
        skrin.show()
        utime.sleep(2)  # Wait for 2 seconds
        darkness_led.value(0)  # Turn off darkness LED
        skrin.fill(0)  # Clear OLED display

    # Check ultrasonic sensor
    distance = sonic.distance_cm()
    print("Distance:", distance, "cm")
    if distance < 30:  # Adjust the threshold as needed
        print("Chicken approaching!")
        move_servo(180)  # Move servo to 180 degrees
        sound_buzzer()   # Sound the buzzer
        approaching_led.value(1)  # Turn on approaching LED
        skrin.text("Chicken Approaching!", 0, 0)
        skrin.show()
        utime.sleep(2)   # Wait for 2 seconds
        approaching_led.value(0)  # Turn off approaching LED
        skrin.fill(0)  # Clear OLED display