gryffin13
Posts: 9
Joined: Tue May 30, 2017 3:42 am

Interrupts not behaving as expected

Sun May 05, 2019 1:22 am

Hi, I'm building a project and trying to use interrupts and they are not behaving as expected. It seems like there is not many examples out there to go off of, so I've pieced it together the best I can. (I'm still learning both raspberry pi and python, so everything is pretty rudimentary for me).

What I'm trying to do is have my main code run a timer and then physical hardware causes interrupts to trigger a movie clip to play. But I'm having two issues with my interrupts. 1) the interrupt gets called, but runs the function twice. I don't think it's a debounce issue because I have upped the debounce to be pretty long - and also it always runs exactly twice, never once, never three times. 2) the second problem as I'm trying to expand my integreation is that the interrupt gets called whether my input GPIO pin is 3.3V or GND. I expect it to only get triggered when it moves from ground to 3.3V, but that's not what's happening.

If anyone can help with either I would be very appreciative. Code below.

Main code:

Code: Select all

#!/usr/bin/python

from Tkinter import *
import RPi.GPIO as GPIO
import time
import escapeFunctions


GPIO.setmode(GPIO.BCM)
GPIO.cleanup() 
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)


poorly = ("/home/pi/Videos/poorly.mp4")
wisely = ("/home/pi/Videos/wisely.mp4")


rootWindow = Tk()


def int17(pin17):
    escapeFunctions.playVideoFun(poorly)

def int18(pin18):
    escapeFunctions.playVideoFun(wisely)

GPIO.add_event_detect(17, GPIO.RISING, callback=int17, bouncetime=3000)
GPIO.add_event_detect(18, GPIO.RISING, callback=int18, bouncetime=3000)


# Gets the requested values of the height and widht.
windowWidth = rootWindow.winfo_reqwidth()
windowHeight = rootWindow.winfo_reqheight()
# print("Width",windowWidth,"Height",windowHeight)

# Gets both half the screen width/height and window width/height
positionRight = int(rootWindow.winfo_screenwidth() / 2 - windowWidth / 2)
positionDown = int(rootWindow.winfo_screenheight() / 2 - windowHeight / 2)

# defaultColour = rootWindow.cget("red")
rootWindow.attributes("-fullscreen", True)

time1 = ''
prevSec = ''
mins = 0
secs = 0
hours = 0
running = True
# clock = Label(rootWindow, font=('fixed', 20, 'bold'))
clock = Label(rootWindow, font=('fixed', 100))
clock.grid(row=1, column=2, padx=positionRight / 2, pady=positionDown)
# clock.config(bg='dark red')
clock.config(fg='red')


def tick():
    global prevSec, time1, secs, mins, hours, running
    # get the current local time from the PC
    #    time2 = time.strftime('%Y/%m/%d %H:%M:%S')
    if running:
        newSec = time.strftime('%S')
    else:
        newSec = ''
        prevSec = ''
    if newSec != prevSec:
        prevSec = newSec
        secs = secs + 1
        if secs > 59:
            secs = 0
            mins = mins + 1
            if mins > 59:
                mins = 0
                hours = hours + 1
                if hours > 1:
                    # hours = 0
                    # mins = 0
                    # secs = 0
                    clock.config(bg='black')
                # clock = Label(rootWindow, font=('fixed', 150))
    time2 = '%02d:%02d:%02d' % (hours, mins, secs)
    # if time string has changed, update it
    if time2 != time1:
        time1 = time2
        clock.config(text=time2)
    # calls itself every 200 milliseconds
    # to update the time display as needed
    # could use >200 ms, but display gets jerky
    clock.after(200, tick)


tick()



rootWindow.mainloop()

function:

Code: Select all

def playVideoFun (fileName):
	import os
	os.system ('omxplayer ' + fileName)	
	return

User avatar
MrYsLab
Posts: 312
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Interrupts not behaving as expected

Sun May 05, 2019 2:24 pm

I have modified your program to run using python3 and to match my hardware. When I run this, it runs as expected. Make sure that you have things wired correctly.

Code: Select all

#!/usr/bin/python3

from tkinter import *
import RPi.GPIO as GPIO
import time
# import escapeFunctions


GPIO.setmode(GPIO.BCM)
# GPIO.cleanup() 
GPIO.setup(18, GPIO.IN)
GPIO.setup(19, GPIO.IN)


poorly = ("/home/pi/Videos/poorly.mp4")
wisely = ("/home/pi/Videos/wisely.mp4")


rootWindow = Tk()


def int18(pin18):
    print('18')

def int19(pin19):
    print('17')

GPIO.add_event_detect(18, GPIO.RISING, callback=int18, bouncetime=3000)
GPIO.add_event_detect(19, GPIO.RISING, callback=int19, bouncetime=3000)


# Gets the requested values of the height and widht.
windowWidth = rootWindow.winfo_reqwidth()
windowHeight = rootWindow.winfo_reqheight()
# print("Width",windowWidth,"Height",windowHeight)

# Gets both half the screen width/height and window width/height
positionRight = int(rootWindow.winfo_screenwidth() / 2 - windowWidth / 2)
positionDown = int(rootWindow.winfo_screenheight() / 2 - windowHeight / 2)

# defaultColour = rootWindow.cget("red")
# rootWindow.attributes("-fullscreen", True)

time1 = ''
prevSec = ''
mins = 0
secs = 0
hours = 0
running = True
# clock = Label(rootWindow, font=('fixed', 20, 'bold'))
clock = Label(rootWindow, font=('fixed', 100))
clock.grid(row=1, column=2, padx=positionRight / 2, pady=positionDown)
# clock.config(bg='dark red')
clock.config(fg='red')


def tick():
    global prevSec, time1, secs, mins, hours, running
    # get the current local time from the PC
    #    time2 = time.strftime('%Y/%m/%d %H:%M:%S')
    if running:
        newSec = time.strftime('%S')
    else:
        newSec = ''
        prevSec = ''
    if newSec != prevSec:
        prevSec = newSec
        secs = secs + 1
        if secs > 59:
            secs = 0
            mins = mins + 1
            if mins > 59:
                mins = 0
                hours = hours + 1
                if hours > 1:
                    # hours = 0
                    # mins = 0
                    # secs = 0
                    clock.config(bg='black')
                # clock = Label(rootWindow, font=('fixed', 150))
    time2 = '%02d:%02d:%02d' % (hours, mins, secs)
    # if time string has changed, update it
    if time2 != time1:
        time1 = time2
        clock.config(text=time2)
    # calls itself every 200 milliseconds
    # to update the time display as needed
    # could use >200 ms, but display gets jerky
    clock.after(200, tick)


tick()

rootWindow.mainloop()


gryffin13
Posts: 9
Joined: Tue May 30, 2017 3:42 am

Re: Interrupts not behaving as expected

Mon May 06, 2019 4:28 am

Thanks for the input. Before you modified it did you notice the same behavior?

Is there a reason you removed the pull down resistor?

User avatar
MrYsLab
Posts: 312
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Interrupts not behaving as expected

Mon May 06, 2019 1:49 pm

The reason I do not add the pull-downs is because the circuity I am using does not require it. What I would suggest you do is first
make sure that your button code works. You can use the code I provided, just change the pull-down and pin numbers.
If that works, then add the things that I left out, one by one and see what is breaking your code.

gryffin13
Posts: 9
Joined: Tue May 30, 2017 3:42 am

Re: Interrupts not behaving as expected

Tue May 07, 2019 3:00 am

Ok, I will do that. Thank you for the help.

gryffin13
Posts: 9
Joined: Tue May 30, 2017 3:42 am

Re: Interrupts not behaving as expected

Thu May 09, 2019 12:19 am

MrYsLab wrote:
Mon May 06, 2019 1:49 pm
The reason I do not add the pull-downs is because the circuity I am using does not require it. What I would suggest you do is first
make sure that your button code works. You can use the code I provided, just change the pull-down and pin numbers.
If that works, then add the things that I left out, one by one and see what is breaking your code.
well after testing out what you provided, it does not work as I expected either. Before modifying your code at all, when I run it, as soon as I connect a wire to pin 18, it starts intermittently printing out "18". The wire is unconnected to anything else, just the pi, a keyboard, a mouse, and that one wire going nowhere. Does that not happen for you?

Return to “Python”