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

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

#Pin declaration
motion_sensor = Pin(19, Pin.IN)
buzzer = PWM(Pin (4), Pin.OUT)
LDR_Pin = ADC(Pin(2, Pin.IN))
led_red = Pin(5, Pin.OUT)
led_blue = Pin(18, Pin.OUT)

#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('PERLAHAN KAN KENDERAAN DAN BUKAK LAMPU')
                sleep(0.3)
                buzzer.init(freq=2200,duty=200)
                sleep(0.5)
                buzzer.init(freq=1,duty=0)
                sleep(0.5)

        else :
            print('MASIH CERAH')
            sleep(1)
            buzzer.init(freq=1,duty=0)
            sleep(2)
        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("MATAHARI DA NAK TERBENAM.")
            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, ())