# Project objective: To test a passive buzzer to play an alarm sound at one second interval
#
# Hardware and connections used:
# Passive buzzer GND to Raspberry Pi Pico GND
# Passive buzzer + Pin to GPIO Pin 15
#
# Programmer: Adrian Josele G. Quional
# if passive buzzer is used, import the Speaker class from picozero
from picozero import Speaker
from time import sleep
from picozero import LED
from machine import Pin, I2C
from i2c_lcd import I2cLcd
import utime
#creating a speaker object
speaker = Speaker(15)
#creating an LED object
red = LED(27)
green = LED(28)
#GPIO pins for rows and columns
row_pins = [Pin(i, Pin.OUT) for i in (0, 1, 2, 3)] # rows: output
col_pins = [Pin(i, Pin.IN, Pin.PULL_DOWN) for i in (4, 5, 6, 7)] # columns: input
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=100000)
i2c_addresses = i2c.scan()
lcd_address = i2c_addresses[0] # typically 0x27 or 0x3F
lcd = I2cLcd(i2c, lcd_address, 2, 16) # 2 rows, 16 columns
lcd.putstr("System Ready")
sleep(2)
lcd.clear()
#keypad layout
keys = [
['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']
]
key = ['1','3','5','7']
temp = []
length = 0
def scan_keypad():
lcd.putstr("Enter Password")
sleep(1)
lcd.clear()
global length
global temp
for row_num, row_pin in enumerate(row_pins):
# Set current row high
row_pin.high()
for col_num, col_pin in enumerate(col_pins):
if col_pin.value() == 1 and length <=3:
lcd.putstr(keys[row_num][col_num])
print("Key Pressed:", )
temp.append(keys[row_num][col_num])
length = len(temp)
utime.sleep(0.3) # debounce delay
# Set the row low again
row_pin.low()
while True:
scan_keypad()
if length == 4:
# speaker.on()
if temp == key:
green.on()
print("access granted!")
sleep(2)
green.off()
break
else:
red.on()
print("access denied!", temp);
temp = []
length = 0;
sleep(2)
red.off()
# sleep(1)
speaker.off()
# continuously beep at 1 sec interval while the board has power
# note: a passive buzzer can also be used to play different tones
# while True:
# speaker.on()
# red.on()
# sleep(1)
# speaker.off()
# red.off()
# sleep(2)