dltccorreia
Posts: 23
Joined: Sun Feb 08, 2015 5:35 pm

Generat Interrups with LDR

Sun Feb 08, 2015 5:43 pm

Hi,

I'm making a little project, to read my energy meter based on the LED blink.
For detecting the LED i'm using one LDR. So, when the LED flashs the voltage in the GPIO pin will change the level...
I'm trying to use interruptions to make it. Its my first time with interruptions... So there is my code:

Code: Select all

#TEST_1_INTERRUPTIONS
import RPi.GPIO as GPIO
import time
import subprocess

GPIO.setmode(GPIO.BCM)

GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)

channel=23

def my_callback(channel):
        print('Edge detected on channel %s'%channel)

GPIO.add_event_detect(channel, GPIO.BOTH, callback=my_callback)

GPIO.cleanup(23)
My idea (for testing porpuses) ist to execute the function "my_callback" each time the Led Blinks...
I run the program, but nothing happens... :cry:

Thanks in advance :oops:

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Generat Interrups with LDR

Fri Feb 13, 2015 4:45 pm

Your code just 'runs through', cleans up and exits. Try adding debug prints:

Code: Select all

#TEST_1_INTERRUPTIONS
import RPi.GPIO as GPIO
import time
import subprocess

print('Setup...')
GPIO.setmode(GPIO.BCM)

GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)

channel=23

def my_callback(channel):
        print('Edge detected on channel %s'%channel)

print('Add event detect...')
GPIO.add_event_detect(channel, GPIO.BOTH, callback=my_callback)

print('Cleanup...')
GPIO.cleanup(23)

print('Quit.')
You need to keep the script running using an (near-)infinite loop - something like:

Code: Select all

#TEST_1_INTERRUPTIONS
import RPi.GPIO as GPIO
import time
import subprocess

print('Setup...')

GPIO.setmode(GPIO.BCM)

GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)

channel=23

def my_callback(channel):
        print('Edge detected on channel %s'%channel)

try:
    print('Add event detect...')
    GPIO.add_event_detect(channel, GPIO.BOTH, callback=my_callback)
    
    print('Waiting...')
    while True:
        sleep(0.001)

except (KeyboardInterrupt):
    print('Stopping...')

GPIO.cleanup(23)
print('Quit.')
...run and when you want it to stop hit Ctrl+C (= keyboard interrrupt)
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

User avatar
mayannaise
Posts: 41
Joined: Tue Feb 19, 2013 3:04 pm
Location: UK

Re: Generat Interrups with LDR

Fri Feb 13, 2015 5:08 pm

-rst- wrote:You need to keep the script running using an (near-)infinite loop - something like:
Using a loop seems to defy the point of using an interrupt in the first place - might as well just poll the i/p. Surely there is a better way to use the interrupt, or is this one of the limitations of Python?
It takes two keys to unlock the door of success: Imagination and Determination; the latter is ten times greater than the former.

dltccorreia
Posts: 23
Joined: Sun Feb 08, 2015 5:35 pm

Re: Generat Interrups with LDR

Fri Feb 13, 2015 5:43 pm

mayannaise wrote:
-rst- wrote:You need to keep the script running using an (near-)infinite loop - something like:
Using a loop seems to defy the point of using an interrupt in the first place - might as well just poll the i/p. Surely there is a better way to use the interrupt, or is this one of the limitations of Python?
Exacly... I've the same doubt... I've been using the interrupts do avoid the while cycle... Or this while work diferently? :?

jimallyn
Posts: 51
Joined: Wed Nov 12, 2014 9:13 am

Re: Generat Interrups with LDR

Fri Feb 13, 2015 7:50 pm

Have you verified that the LDR will change resistance enough to detect from a LED blinking? Might be better off using a phototransistor.
“So you say you love the poor? Name them.” - Gustavo Gutierrez

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Generat Interrups with LDR

Fri Feb 13, 2015 10:00 pm

This while loop does basically nothing - yielding the processor time to other threads and processes (like the one in the GPIO library handling the actual GPIO reading) - other than keeps the application (script) alive.

If your application (script) is not alive, there is nothing left to receive the interrupt. When the script runs to the end, there is nothing left of the application (unless the code starts another thread and the GPIO.add_event_detect(...) is not doing so). And this applies to any programming language.

Also calling the GPIO.cleanup() will effectively cancel the GPIO.add_event_detect(...) call.

Polling in a while loop bad - sleeping in a while loop ok :D
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

Return to “Python”