from machine import Pin, PWM, I2C
from time import sleep
from keypad import Keypad
# === CONFIGURATION ===
# Broches du capteur PIR
pir = Pin(13, Pin.IN)
# Broche du servo
servo = PWM(Pin(19), freq=50)
# Position du servo (0 à 180 degrés environ)
def set_servo_angle(angle):
duty = int((angle / 180) * 102 + 26) # correspond à 0.5ms - 2.5ms
servo.duty(duty)
# === CLAVIER 4x4 ===
# Lignes et colonnes selon ton câblage
rows = [14, 27, 26, 25]
cols = [33, 32, 35, 34]
keys = [
['1','2','3','A'],
['4','5','6','B'],
['7','8','9','C'],
['*','0','#','D']
]
keypad = Keypad(rows, cols, keys)
# === CODE SECRET ===
code_correct = "1111"
code_input = ""
# === BOUCLE PRINCIPALE ===
while True:
key = keypad.get_key()
if key:
print("Touche :", key)
if key == '#': # Validation du code
if code_input == code_correct:
print("✅ Code correct")
sleep(0.2)
if pir.value() == 1:
print("Mouvement détecté → Servo tourne")
set_servo_angle(90)
sleep(2)
set_servo_angle(0)
else:
print("Aucun mouvement détecté → Rien ne se passe")
else:
print("❌ Code incorrect")
code_input = "" # Réinitialiser l’entrée
elif key == '*': # Effacer l’entrée
code_input = ""
print("Entrée réinitialisée")
else:
code_input += key
print("Code actuel :", code_input)
sleep(0.1)