from machine import Pin, PWM, SoftI2C, SPI
import time
import ssd1306
# RFID Library - Wokwi mein ye pehle se hoti hai
from mfrc522 import MFRC522
# Keypad Setup - GP10 se GP17
row_pins = [Pin(i, Pin.IN, Pin.PULL_DOWN) for i in [10, 11, 12, 13]]
col_pins = [Pin(i, Pin.OUT) for i in [14, 15, 16, 17]]
keys = [['1', '2', '3', 'A'], ['4', '5', '6', 'B'], ['7', '8', '9', 'C'], ['*', '0', '#', 'D']]
# OLED I2C Setup - GP0 SDA, GP1 SCL
oled_active = False
try:
i2c = SoftI2C(sda=Pin(0), scl=Pin(1), freq=100000)
time.sleep(1)
oled = ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3C)
oled_active = True
except:
pass
# RFID SPI Setup - GP3 MISO, GP4 MOSI, GP5 SCK, GP6 SDA, GP7 RST
spi = SPI(0, baudrate=1000000, polarity=0, phase=0, sck=Pin(5), mosi=Pin(4), miso=Pin(3))
rfid = MFRC522(spi, Pin(6), Pin(7))
# Buzzer GP18 + Servo GP2
buzzer = PWM(Pin(18))
servo = PWM(Pin(2))
servo.freq(50)
# Variables
entered_pass = ""
master_pass = "7878"
last_key = None
tries = 3
locked = True
unlock_time = 0
change_mode = False
new_pass = ""
register_mode = False
# Default authorized card UIDs - Wokwi ka card
authorized_cards = ["0x83C1A93E", "0x12345678"]
def beep(freq, duration):
buzzer.freq(freq)
buzzer.duty_u16(30000)
time.sleep(duration)
buzzer.duty_u16(0)
def lock_door():
global locked
servo.duty_ns(500000)
locked = True
def unlock_door():
global locked, unlock_time
servo.duty_ns(1500000)
locked = False
unlock_time = time.time()
def update_oled(line1, line2=""):
if not oled_active:
return
oled.fill(0)
oled.text("TARS LEVEL 14", 15, 0)
oled.hline(0, 12, 128, 1)
oled.text(line1, 0, 20)
oled.text(line2, 0, 35)
if register_mode:
oled.text("SCAN NEW CARD", 15, 55)
elif change_mode:
oled.text("CHANGE MODE", 25, 55)
elif locked:
oled.text("LOCKED", 40, 55)
else:
oled.text("UNLOCKED", 35, 55)
oled.show()
def scan_keypad():
global last_key
for c in range(4):
col_pins[c].on()
time.sleep(0.002)
for r in range(4):
if row_pins[r].value() == 1:
col_pins[c].off()
key = keys[r][c]
if key!= last_key:
last_key = key
return key
col_pins[c].off()
if all(p.value() == 0 for p in row_pins):
last_key = None
return None
def scan_rfid():
(stat, tag_type) = rfid.request(rfid.REQIDL)
if stat == rfid.OK:
(stat, raw_uid) = rfid.anticoll()
if stat == rfid.OK:
uid = "0x%02X%02X%02X%02X" % (raw_uid[0], raw_uid[1], raw_uid[2], raw_uid[3])
return uid
return None
# ===== STARTUP =====
time.sleep(1)
lock_door()
time.sleep(2)
update_oled("TARS READY", "CARD OR PASS")
print("TARS LEVEL 14 - READY")
beep(1000, 0.1)
beep(1500, 0.1)
# ===== MAIN LOOP =====
while True:
# Auto Lock after 10 seconds
if not locked and time.time() - unlock_time > 10:
lock_door()
entered_pass = ""
update_oled("AUTO LOCKED", "CARD OR PASS")
beep(500, 0.3)
# RFID Check
card_uid = scan_rfid()
if card_uid:
print("CARD:", card_uid)
if register_mode:
if card_uid not in authorized_cards:
authorized_cards.append(card_uid)
update_oled("CARD ADDED", card_uid)
print("TARS: NAYA CARD ADD:", card_uid)
beep(2000, 0.1)
beep(2500, 0.2)
else:
update_oled("ALREADY ADDED", card_uid)
beep(400, 0.3)
register_mode = False
time.sleep(2)
update_oled("CARD OR PASS", "")
else:
if card_uid in authorized_cards:
unlock_door()
update_oled("CARD ACCEPTED", card_uid)
print("TARS: CARD SE UNLOCK")
beep(2000, 0.1)
beep(2500, 0.1)
beep(3000, 0.2)
tries = 3
else:
update_oled("CARD DENIED", card_uid)
print("TARS: GALAT CARD")
beep(400, 0.5)
time.sleep(1)
# Keypad Check
key = scan_keypad()
if key:
beep(1000, 0.05)
# CARD REGISTER MODE
if key == 'C' and not locked:
register_mode = True
update_oled("REGISTER MODE", "SCAN CARD")
print("TARS: CARD REGISTER MODE")
# PASSWORD CHANGE MODE
elif change_mode:
if key == '#':
if len(new_pass) == 4:
master_pass = new_pass
change_mode = False
new_pass = ""
update_oled("PASS CHANGED", "SUCCESS")
beep(2000, 0.1)
beep(2500, 0.2)
time.sleep(1)
update_oled("CARD OR PASS", "")
else:
update_oled("ERROR", "4 DIGIT ONLY")
beep(400, 0.3)
elif key == '*':
change_mode = False
new_pass = ""
update_oled("CANCELLED", "CARD OR PASS")
elif key.isdigit():
if len(new_pass) < 4:
new_pass += key
display = "*" * len(new_pass)
update_oled("NEW PASS:", display)
# NORMAL MODE
else:
if key == '#' and len(entered_pass) == 4:
if entered_pass == master_pass:
unlock_door()
update_oled("ACCESS GRANTED", "WELCOME SYED")
beep(2000, 0.1)
beep(2500, 0.1)
beep(3000, 0.2)
tries = 3
else:
tries -= 1
if tries > 0:
update_oled("ACCESS DENIED", f"TRIES: {tries}")
beep(400, 0.5)
else:
update_oled("LOCKDOWN", "RESET ME")
for i in range(5):
beep(200, 0.3)
time.sleep(0.2)
entered_pass = ""
elif key == '*':
if not locked:
change_mode = True
new_pass = ""
update_oled("CHANGE PASS", "ENTER NEW")
else:
entered_pass = ""
update_oled("ENTER PASSWORD", "****")
elif key == 'D':
lock_door()
time.sleep(1)
entered_pass = ""
update_oled("SYSTEM LOCKED", "CARD OR PASS")
beep(500, 0.3)
elif key == 'A':
update_oled("ALARM MODE", "INTRUDER ALERT")
for i in range(3):
beep(3000, 0.2)
time.sleep(0.1)
beep(1000, 0.2)
time.sleep(0.1)
elif key.isdigit() and locked:
if len(entered_pass) < 4:
entered_pass += key
display = "*" * len(entered_pass)
update_oled("ENTER PASSWORD", display)
time.sleep(0.01)