I am really confused: I have a lightbarrier connected over an optocoupler (ACPL-827) with my Raspberry Pi. When the lightbarrier gets triggered (detects a rising edge), it calls the scan-function. This works, but the scan-function only continues if the status of the input-pin is 1. And this does not work although the lightbarrier is still triggered. Does somebody has an idea where this problem comes from? Is it possible that the problem has to do with the optocoupler? If I use the buttons everything works fine. I appreciate any help!
In the following my code:
import RPi.GPIO as GPIO
from time import sleep
green_led = 18
red_button = 17
green_button = 27
LIGHT_BARRIER_PINS = [24, 5]
# green LED gets on
def green_on(pin):
GPIO.output(green_led, GPIO.HIGH)
sleep(1)
GPIO.output(green_led, GPIO.LOW)
def light_barrier_event_handler(channel):
if channel == LIGHT_BARRIER_PINS[0]:
print("start scanning - triggert by photoelectric barrier 1")
scan()
elif channel == LIGHT_BARRIER_PINS[1]:
print("start scanning - triggert by photoelectric barrier 2")
scan()
elif channel == red_button:
print("start scanning - triggert by red button")
scan()
elif channel == green_button:
print("start scanning - triggert by green button")
scan()
def scan():
while True:
if GPIO.input(red_button) == GPIO.HIGH or GPIO.input(green_button) == GPIO.HIGH or GPIO.input(LIGHT_BARRIER_PINS[0]) == GPIO.HIGH or GPIO.input(LIGHT_BARRIER_PINS[1]) == GPIO.HIGH:
green_on(LIGHT_BARRIER_PINS[0])
sleep(0.2)
else:
break
GPIO.setmode(GPIO.BCM)
GPIO.setup(green_led, GPIO.OUT)
GPIO.setup(red_button, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
GPIO.setup(green_button, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
GPIO.setup(LIGHT_BARRIER_PINS[0], GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
GPIO.setup(LIGHT_BARRIER_PINS[1], GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
GPIO.add_event_detect(red_button, GPIO.RISING, callback=light_barrier_event_handler, bouncetime=200)
GPIO.add_event_detect(green_button, GPIO.RISING, callback=light_barrier_event_handler, bouncetime=200)
GPIO.add_event_detect(LIGHT_BARRIER_PINS[0], GPIO.RISING, callback=light_barrier_event_handler, bouncetime=200)
GPIO.add_event_detect(LIGHT_BARRIER_PINS[1], GPIO.RISING, callback=light_barrier_event_handler, bouncetime=200)
try:
while True:
pass
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()