0

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()
5
  • some possibilities assuming code for read and event are doing what they might be expected to: 1) callback occurs prior to state being updated for bit. 2) a falling edge occurred immediately after, and state was 0 again by the time you read it.
    – Abel
    May 15 at 13:07
  • 1
    Your event handler should be as short as possible - but you end up in a while loop - bad code.
    – CoderMike
    May 15 at 14:36
  • Your event handlers are registered for GPIO.RISING, so they will only trigger when the line changes from 0 to 1, they will not continue to trigger when it stays 1.
    – PMF
    May 15 at 15:32
  • wait wait wait - instead of stating a code is BAD because of (..) can we EXPLAIN why the code is bad because of (..)?? instead of only downvoting; even if it is obvious; he is new May 19 at 19:30
  • Thanks William! :)
    – leolumpy
    May 20 at 7:06

2 Answers 2

0

Your callback blocks and never exits! Callbacks should do minimal processing.

Callbacks operate on a single thread.

It will NEVER be called a second time (or more accurately the 2nd call will be queued waiting for the first to finish).

You might be better to try gpiozero. See https://gpiozero.readthedocs.io/en/v1.6.2/faq.html?highlight=callback#frequently-asked-questions

0

Thanks for your answers.

FYI: it turned out that it was a hardware problem. The code works perfectly fine and does exactly what it should.

Not the answer you're looking for? Browse other questions tagged or ask your own question.