from mfrc522 import SimpleMFRC522
import time
import ujson
from machine import Pin, PWM
unlockTime = 5
decPin = 20
incPin = 21
servoPin=0
decBut=Pin(decPin,Pin.IN,Pin.PULL_UP)
incBut=Pin(incPin,Pin.IN,Pin.PULL_UP)
redPin=15
greenPin=14
bluePin=13
redLED=Pin(redPin,Pin.OUT)
greenLED=Pin(greenPin,Pin.OUT)
blueLED=Pin(bluePin,Pin.OUT)
redLED.on()
greenLED.off()
blueLED.off()
incButState=1
incButStateOld=1
decButState=1
decButStateOld=1
myServo=PWM(Pin(servoPin))
myServo.freq(50)
servoAngle=0
reader=SimpleMFRC522(spi_id=0,sck=18,miso=16,mosi=19,cs=17,rst=9)
try:
f=open("/dict.json","r")
servoDict=ujson.load(f)
f.close()
except:
myDict={}
f=open('dict.json','w')
ujson.dump(myDict,f)
f.close()
def readRFID():
global cardID,servoAngle
print()
print("System LOCKED")
print("Reading . . . Please Place the Card")
cardID,readText = reader.read()
print()
print("ID: ",cardID)
try:
f=open('/dict.json','r')
servoDict = ujson.load(f)
f.close()
servoAngle=servoDict[cardID]
print(servoAngle)
moveServo()
except:
f=open('/dict.json','r')
myDict=ujson.load(f)
f.close()
myDict[cardID]=servoAngle
f = open('/dict.json','w')
ujson.dump(myDict,f)
f.close()
moveServo()
def moveServo():
writeVal = 6553/180*servoAngle + 1638
myServo.duty_u16(int(writeVal))
f=open('/dict.json','r')
myDict =ujson.load(f)
f.close()
myDict[cardID]=servoAngle
f=open('/dict.json','w')
ujson.dump(myDict, f)
f.close()
while True:
redLED.on()
greenLED.off()
blueLED.off()
readRFID()
print("System Unlocked by Card: ",cardID)
unlockStart= time.time()
while time.time()-unlockStart<unlockTime:
redLED.off()
greenLED.on()
incButState=incBut.value()
decButState=decBut.value()
if incButState==0 and incButStateOld==1:
servoAngle=servoAngle + 10
if servoAngle>180:
servoAngle=180
if servoAngle<0:
servoAngle=0
moveServo()
unlockStart=time.time()
time.sleep(.2)
if decButState==0 and decButStateOld==1:
servoAngle=servoAngle - 10
if servoAngle>180:
servoAngle=180
if servoAngle<0:
servoAngle=0
moveServo()
unlockStart=time.time()
time.sleep(.2)
incButStateOld=incButState
decButStateOld=decButState