0% found this document useful (0 votes)
5 views2 pages

Raspberry Pi Passcode LED Control Code

The document contains a Python script for a microcontroller that manages button presses and controls LEDs based on a passcode. It defines functions for handling button interrupts and checking if the entered passcode matches a predefined one. The script continuously reads input from a multiplexer and toggles an LED based on the correctness of the passcode entered by the user.

Uploaded by

moaazallam99
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Raspberry Pi Passcode LED Control Code

The document contains a Python script for a microcontroller that manages button presses and controls LEDs based on a passcode. It defines functions for handling button interrupts and checking if the entered passcode matches a predefined one. The script continuously reads input from a multiplexer and toggles an LED based on the correctness of the passcode entered by the user.

Uploaded by

moaazallam99
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import machine

import time
from utime import sleep

BUTTON_COUNT = 2
LED_COUNT = 4
INPUT_COUNT = 2

BUTTON_START_ID = 16
LED_GPIO_START = 7
last_button_time_stamp = 0
key_presses = []

# Extract the numeric pin id from the passed in Pin instance


def PinId(pin):
Pin(17, mode=IN, pull=PULL_DOWN)
return int(str(pin)[4:6].rstrip(","))

def interrupt_callback(pin):
global last_button_time_stamp

cur_button_ts = time.ticks_ms()
button_press_delta = cur_button_ts - last_button_time_stamp
if button_press_delta > 200:
last_button_time_stamp = cur_button_ts
key_presses.append(pin)
PinId()
print(f'key press: {PinId(pin) - BUTTON_START_ID}')

def main():
global key_presses
global last_button_time_stamp
PASSCODE_LENGTH = 0

s0 = [Link](27, [Link])
s1 = [Link](28, [Link])

mux_in = [Link](26, [Link], [Link].PULL_DOWN)

buttons = []
for btn_idx in range(0, BUTTON_COUNT):
[Link]([Link](BUTTON_START_ID + btn_idx, [Link],
[Link].PULL_DOWN))
buttons[-1].irq(trigger=[Link].IRQ_FALLING,
handler=interrupt_callback)

PASS_CODE = [buttons[1], buttons[0]]


PASSCODE_LENGTH = len(PASS_CODE)

out_pins = []
for out_id in range(0, LED_COUNT):
out_pins.append([Link](LED_GPIO_START + out_id, [Link]))

last_dev = -1
while True:
binary_code = 0
for selector_val in range(INPUT_COUNT):
[Link](selector_val % 2)
sleep(0.02)
binary_code += (pow(2, selector_val) * mux_in.value())

if last_dev != binary_code:
last_dev = binary_code
print(f'selected output: {last_dev}')
sleep(0.1)

if len(key_presses) >= PASSCODE_LENGTH:


if key_presses[:PASSCODE_LENGTH] == PASS_CODE:
print('correct passcode')
out_pins[0].toggle()
else:
print('wrong passcode')
print('')
key_presses = key_presses[PASSCODE_LENGTH:]

if __name__ == "__main__":
main()

You might also like