from machine import Pin
import utime
import rp2
from rp2 import PIO
pir_pin = Pin(18, Pin.IN)
buzzer_pin = Pin(27, Pin.OUT)
led = Pin(5, Pin.OUT)
real_pass = "1234"
input_password = ""
password_index = 0
pass_length = 4
motion_detected_flag = False
@rp2.asm_pio(set_init=[PIO.IN_HIGH]*3)
def keypad():
wrap_target()
set(y, 0) # 0
label("1")
mov(isr, null) # 1
set(pindirs, 1) # Adjusted for 3 columns
in_(pins, 3) # Adjusted for 3 columns
set(pindirs, 2) # Adjusted for 3 columns
in_(pins, 3) # Adjusted for 3 columns
set(pindirs, 4) # Adjusted for 3 columns
in_(pins, 3) # Adjusted for 3 columns
mov(x, isr) # 10
jmp(x_not_y, "13") # 11
jmp("1") # 12
label("13")
push(block) # 13
irq(0)
mov(y, x) # 14
jmp("1") # 15
wrap()
# 3 pins for the keypad columns
for i in range(10, 13):
Pin(i, Pin.IN, Pin.PULL_DOWN)
key_names = "*7410852#963"
def motion_detected(pin):
global motion_detected_flag
if not motion_detected_flag:
print("Motion detected!")
motion_detected_flag = True
sm = rp2.StateMachine(0, keypad, freq=2000, in_base=Pin(10, Pin.IN, Pin.PULL_DOWN), set_base=Pin(6))
sm.active(1)
sm.irq(oninput)
buzzer_pin.on()
led.on()
utime.sleep(2)
buzzer_pin.off()
led.off()
utime.sleep(3) # Add a short delay to debounce
motion_detected_flag = False
def oninput(machine):
print("oninput() called.")
global input_password, password_index, motion_detected_flag
keys = machine.get()
while machine.rx_fifo():
keys = machine.get()
pressed = []
for i in range(len(key_names)):
if (keys & (1 << i)):
pressed.append(key_names[i])
print("Keys changed. Pressed keys: ", pressed)
if motion_detected_flag and pressed:
print("Processing keypad input...")
for key in pressed:
input_password += key
password_index += 1
print("Current password: ", '*'*password_index)
if password_index >= PASSWORD_LENGTH:
if input_password == PREDEFINED_PASSWORD:
print("Password correct. Alarm deactivated.")
buzzer_pin.off()
led.off()
motion_detected_flag = False
sm.active(0)
else:
print("Password incorrect. Try again.")
input_password = ""
password_index = 0
return
# Configure the PIR sensor to trigger an interrupt on motion
pir_pin.irq(trigger=Pin.IRQ_RISING, handler=motion_detected)
# Main loop
try:
while True:
print("Waiting for motion...")
utime.sleep(1)
except KeyboardInterrupt:
pir_pin.irq(handler=None) # Disable the PIR interrupt
buzzer_pin.off()
led.off()
print("Exiting...")