print('PROJECT IOT NI BOSH')
print('DATE :15/01/2024 ')
print('zunie')

#Import all recessary libraries 
from machine import Pin, PWM, ADC, SoftI2C
from utime import sleep
import library_servo_motor
import _thread
import ssd1306

#Pin declaration
motion_sensor = Pin(12, Pin.IN)
buzzer = PWM(Pin (27), Pin.OUT)
SERVO_PIN = Pin(14, Pin.OUT)
LDR_Pin = ADC(Pin(2, Pin.IN))
led_red = Pin(5, Pin.OUT)
led_blue = Pin(18, Pin.OUT)
i2c_oled = SoftI2C(scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64


#Create the object name for sensor with library ---> LIBRARYNAME.CLASSNAME (classname amik dlm library)
gate_motor = library_servo_motor.Servo(pin=SERVO_PIN)
skrin =ssd1306.SSD1306_I2C(width=oled_width, height=oled_height, i2c=i2c_oled)        #library name.class name()

#Main Program
def first_loop():
    while True :
        #code for the first loop
        #BUZZER, MOTION,ULTRASONIC AND SERVO MOTOR PART
        status_of_pir = motion_sensor.value()  #The value will 0 or 1
        if status_of_pir == True:
            for i in range(20):
                print('WARNING! INTRUDER ALERT!!!')
                sleep(0.3)
                buzzer.init(freq=2200,duty=200)
                sleep(0.5)
                buzzer.init(freq=1,duty=0)
                sleep(0.5)
                gate_motor.move(0)
                sleep(0.2)
                gate_motor.move(0)
                sleep(1)
                skrin.fill(1)   #1  is code WHITE , 0 is code BLACK
                skrin.text('WARNING!', 1, 10,0 )  #Text, column, row , colour
                skrin.text('INTRUDER ALERT!!!', 2, 20,0 )
                skrin.text('BE CAREFUL', 3, 30,0 )
                skrin.show()

        else :
            print('STAY SAFE')
            sleep(1)
            buzzer.init(freq=1,duty=0)
            sleep(2)
            gate_motor.move(90)
            sleep(2)
            gate_motor.move(90)
            sleep(1)
            skrin.fill(1)   #1  is code WHITE , 0 is code BLACK
            skrin.text('STAY SAFE ^_^', 1, 10,0 )  #Text, column, row , colour
            skrin.text("DON'T WORRY", 2, 20,0 )
            skrin.show()
        sleep(1)

def second_loop():
    while True :
        #code for the second loop

        kecerahan_cahaya =  LDR_Pin.read()
        voltage = kecerahan_cahaya / 4096 * 5.0
        if 0.83< voltage < 5:
            print('The voltage detected is :' , voltage, 'V')
            print("It's going to rain, quickly close the window.")
            for u in range (5):
                led_red.on()
                sleep(0.5)
                led_red.off()
                sleep(0.3)
                led_blue.off()
                sleep(0.3)

        elif 0.19< voltage <0.83:
            print('The voltage detected is :' , voltage, 'V')
            print("Full daylight")
            led_blue.on()
            sleep(1)
            led_red.off()
            sleep(0.3)

        else :
            print('The voltage detected is :' , voltage, 'V')
            print("Direct sunlight")
            led_blue.on()
            sleep(1)
            led_red.off()
            sleep(0.3)
        sleep(2)
    

# Start the threads after defining the functions
_thread.start_new_thread(first_loop, ())
_thread.start_new_thread(second_loop, ())