from machine import Pin
import time
# =====================================
# KONFIGURASI PIN SENSOR IR
# =====================================
IR1_PIN = 34
IR2_PIN = 35
IR3_PIN = 18
IR4_PIN = 19
# =====================================
# KONFIGURASI LED
# =====================================
GREEN_LED_PIN = 4
RED_LED_PIN = 2
# =====================================
# INISIALISASI SENSOR
# =====================================
ir1 = Pin(IR1_PIN, Pin.IN)
ir2 = Pin(IR2_PIN, Pin.IN)
ir3 = Pin(IR3_PIN, Pin.IN)
ir4 = Pin(IR4_PIN, Pin.IN)
# =====================================
# INISIALISASI LED
# =====================================
green_led = Pin(GREEN_LED_PIN, Pin.OUT)
red_led = Pin(RED_LED_PIN, Pin.OUT)
# =====================================
# VARIABEL COUNTER
# =====================================
people_count = 0
# =====================================
# WAKTU DEBOUNCE / VALIDASI
# =====================================
TRIGGER_TIMEOUT = 2 # detik
# =====================================
# FUNCTION SETUP
# =====================================
def setup():
print("System Started")
green_led.off()
red_led.off()
# =====================================
# FUNCTION MEMBACA SENSOR
# =====================================
def read_ir1():
return ir1.value()
def read_ir2():
return ir2.value()
def read_ir3():
return ir3.value()
def read_ir4():
return ir4.value()
# =====================================
# FUNCTION AKSI MASUK
# =====================================
def person_entered():
global people_count
people_count += 1
print("Person ENTERED")
print("Total People:", people_count)
green_led.on()
red_led.off()
time.sleep(3)
# =====================================
# FUNCTION AKSI KELUAR
# =====================================
def person_exited():
global people_count
if people_count > 0:
people_count -= 1
print("Person EXITED")
print("Total People:", people_count)
red_led.on()
green_led.off()
time.sleep(3)
# =====================================
# FUNCTION STANDBY
# =====================================
def idle_state():
green_led.off()
red_led.off()
# =====================================
# VALIDASI MASUK
# IR1 -> tunggu IR2
# =====================================
def validate_enter():
start_time = time.time()
while time.time() - start_time < TRIGGER_TIMEOUT:
if read_ir2() == 1:
return True
time.sleep(0.01)
return False
# =====================================
# VALIDASI KELUAR
# IR3 -> tunggu IR4
# =====================================
def validate_exit():
start_time = time.time()
while time.time() - start_time < TRIGGER_TIMEOUT:
if read_ir4() == 1:
return True
time.sleep(0.01)
return False
# =====================================
# LOOP UTAMA
# =====================================
def loop():
sensor1 = read_ir1()
sensor3 = read_ir3()
# =================================
# DETEKSI MASUK
# =================================
if sensor1 == 1:
print("IR1 Triggered")
print("Waiting IR2...")
if validate_enter():
print("IR2 Confirmed")
person_entered()
else:
print("Enter Cancelled")
# =================================
# DETEKSI KELUAR
# =================================
elif sensor3 == 1:
print("IR3 Triggered")
print("Waiting IR4...")
if validate_exit():
print("IR4 Confirmed")
person_exited()
else:
print("Exit Cancelled")
# =================================
# IDLE
# =================================
else:
idle_state()
time.sleep(0.1)
# =====================================
# PROGRAM UTAMA
# =====================================
setup()
while True:
loop()