print("This program is to make automatic gate for staff ")
print("By: Qusyairie")
print("Date: 28/4/2024")

#import library
import ultrasonic_lib #kena sama
from utime import sleep
import random
from machine import Pin, PWM, SoftI2C
import oled_lib
import servo_lib

# Pin declaration for PIR sensor
PIR_PIN = Pin(13, Pin.IN)

# Pin declaration
TRIG = Pin(26)
ECHO = Pin(12)
servo_pinA = Pin(4, Pin.OUT)
servo_pinB = Pin(2, Pin.OUT)
Buzzer = PWM(Pin(14, Pin.OUT))
Buzzer.duty(0)  # Set initial duty cycle to 0
Green_led = Pin(19, Pin.OUT)
Red_led = Pin(18, Pin.OUT)
oled_pin = SoftI2C(scl=Pin(22), sda=Pin(21))

# Initialize PIR sensor pin
PIR_PIN = Pin(13, Pin.IN)

# Initialize ultrasonic sensor
distance_sensor = ultrasonic_lib.HCSR04(trigger_pin=TRIG, echo_pin=ECHO)

# Initialize servo motors
pintuA = servo_lib.Servo(pin=servo_pinA)
pintuB = servo_lib.Servo(pin=servo_pinB)

# Generate a random number between 1 and 100
secret_number = 11

# Initialize servo positions
pintuA.move(angle=180)
pintuB.move(angle=180)

# Main program loop
display = oled_lib.SSD1306_I2C(width=128, height=64, i2c=oled_pin, external_vcc=False)
while True:
    sleep(2)
    jarak_dalam_cm = distance_sensor.distance_cm()
    pir_status = PIR_PIN.value()  # Read PIR sensor status

    print("Distance:", jarak_dalam_cm, "cm, PIR Status:", pir_status)
    display.fill(0)
    display.show()
    sleep(3)

    # Prioritized condition
    if jarak_dalam_cm < 200 and pir_status == 1:
        Buzzer.duty(0)  # Turn off buzzer
        pintuA.move(angle=180)  # Move servo to initial position
        pintuB.move(angle=180)  # Move servo to initial position
        Red_led.on()  # Turn on red LED
        Green_led.off()  # Turn off green LED

    # Condition for access attempt
    elif jarak_dalam_cm < 200 and pir_status == 0:
        display.fill(1)
        display.text("Selamat Datang", x=10, y=10, col=0)
        display.text("Please key in ", x=10, y=20, col=0)
        display.text("   your ID", x=10, y=30, col=0)
        display.show()
        print("Selamat Datang")
        print("\nPlease key in your ID")
        try:
            guess = int(input())  # Assuming user input is a number
        except ValueError:
            print("Invalid input. Please enter a number.")
            continue
        sleep(1.5)

        if guess == secret_number:
            display.fill(1)
            display.text("SUCCESSFUL", x=15, y=20, col=0)
            display.show()
            print("Successful")
            Red_led.off()
            Green_led.on() 
            Buzzer.duty(512)  # Set duty cycle to activate buzzer
            sleep(2)
            pintuA.move(angle=90)
            pintuB.move(angle=90)

        else:
            display.fill(1)
            display.text("UNSUCCESSFUL", x=15, y=20, col=0)
            display.show()
            print("Unsuccessful")
            Red_led.on()
            Green_led.off()
            sleep(5)
            pintuA.move(angle=180)
            pintuB.move(angle=180)
Loading
esp32-devkit-v1