I have two simple scripts, which work individually. One simply lights an LED, the other reads RFID. Now I want to put them together, but seem to fail.
Once this runs, I can't read RFID anymore until I restart my Raspberry. Both functions work as intended, if I comment out the respectively other code (LED and RFID).
#!/usr/bin/env python
import time
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
# RFID setup
reader = SimpleMFRC522() #GPIO.setmode(GPIO.BOARD)
# LED setup
rl=37
gl=35
bl=33
freq=100
GPIO.setup(rl, GPIO.OUT)
GPIO.setup(gl, GPIO.OUT)
GPIO.setup(bl, GPIO.OUT)
r=GPIO.PWM(rl,freq)
g=GPIO.PWM(gl,freq)
b=GPIO.PWM(bl,freq)
r.start(0)
g.start(0)
b.start(0)
GPIO.cleanup()
# Set LED to R, G, B for TIME seconds
def led(rv,gv,bv,t):
r.ChangeDutyCycle(rv)
g.ChangeDutyCycle(gv)
b.ChangeDutyCycle(bv)
time.sleep(t)
r.ChangeDutyCycle(0)
g.ChangeDutyCycle(0)
b.ChangeDutyCycle(0)
while True:
try:
print("Reading..")
id, text = reader.read()
print(id)
print(text)
if text == "test":
print('READ TEST. LED GREEN NOW.')
led(0,2,0,1)
else:
print('DIDN\'T READ TEST. LED RED NOW.')
led(2,0,0,1)
time.sleep(1)
finally:
print('Loop done.')
GPIO.cleanup()
In an endless loop, I read RFID, if the text "test" is read, the LED is green, if not it's red.
Why does the LED and RFID function interfere so badly? How can I change the code to achieve the desired functionality?