import time
from machine import I2C, Pin, PWM, ADC
from SSD1306 import SSD1306_I2C
import utime
i2c = I2C(1, scl=Pin(21), sda=Pin(22), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)
pwm = PWM(Pin(32), freq=50)
led_verte = Pin(12, Pin.OUT)
led_verte_luminosite = Pin(25, Pin.OUT)
led_rouge = Pin(13, Pin.OUT)
led_bleue = Pin(14, Pin.OUT)
led_rouge_gas = Pin(10, Pin.OUT)
pir_sensor = Pin(33, Pin.IN)
gas_sensor = Pin(34, Pin.IN)
ldr_sensor_pin = 35
adc = ADC(Pin(ldr_sensor_pin))
adc.width(ADC.WIDTH_10BIT)
adc.atten(ADC.ATTN_11DB)
matrix_keys = [['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']]
pin_rows = [19, 18, 5, 4]
pin_columns = [0, 2, 15, 23]
row_pins = [Pin(pin, Pin.OUT) for pin in pin_rows]
col_pins = [Pin(pin, Pin.IN, Pin.PULL_DOWN) for pin in pin_columns]
secret_pin = ['#', '2', '5', 'B', '5', '6']
guess = []
buzzer_pin = 11
buzzer = PWM(Pin(buzzer_pin), freq=440, duty=0)
def activate_alarm():
for _ in range(3):
buzzer.duty(512)
utime.sleep(0.5)
buzzer.duty(0)
utime.sleep(0.5)
def checkPin(guess):
if guess == secret_pin:
display_message("Code correct")
led_verte.on()
pwm.duty(120)
utime.sleep(2)
pwm.duty(75)
led_verte.off()
else:
display_message("Code incorrect")
led_rouge.on()
activate_alarm()
utime.sleep(2)
led_rouge.off()
def display_message(message):
oled.fill(0)
hidden_secret = '*' * len(guess)
oled.text(hidden_secret, 0, 0)
oled.show()
utime.sleep(1)
oled.fill(0)
oled.text(message, 0, 0)
oled.show()
def scan_keys():
while True:
guess.clear()
display_message("Entrez le code : ")
while True:
key_pressed = None
for row in range(4):
row_pins[row].on()
for col in range(4):
if col_pins[col].value() == 1:
key_pressed = matrix_keys[row][col]
while col_pins[col].value() == 1:
utime.sleep(0.01)
break
row_pins[row].off()
if key_pressed:
break
if key_pressed:
guess.append(key_pressed)
display_message("code : " + "*" * len(guess))
utime.sleep(0.5)
if len(guess) == len(secret_pin):
break
checkPin(guess)
def pir_detection():
while True:
if pir_sensor.value() == 1:
led_bleue.on()
utime.sleep(3)
led_bleue.off()
utime.sleep(0.1)
def gas_detection():
while True:
if gas_sensor.value() == 1:
led_rouge_gas.on()
else:
led_rouge_gas.off()
utime.sleep(0.5)
def luminosite_detection():
while True:
luminosite = adc.read()
print (luminosite)
if luminosite > 300:
led_verte_luminosite.on()
else:
led_verte_luminosite.off()
utime.sleep(0.5)
import _thread
_thread.start_new_thread(scan_keys, ())
_thread.start_new_thread(pir_detection, ())
_thread.start_new_thread(gas_detection, ())
_thread.start_new_thread(luminosite_detection, ())