zzhou
Posts: 3
Joined: Wed Jul 29, 2015 9:45 am

help for multiple track

Wed Jul 29, 2015 9:51 am

hi i'm currently doing a program which need 2 button to operate together. For example, when 1 of the button is pressed(hold) and while the code is executing, i am still able to press another button.

But i had a raspberry pi 1 and pi 2. the program works fine on pi 1 but it doesnt work well on pi 2.

or is there any semaphore setting i'm able to edit from?

have been searching for solutions online for multitreading, processing. would appreciate much if anyone could help me. thx alot!

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

GPIO.setup(7, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(10, GPIO.IN, pull_up_down = GPIO.PUD_UP)

def main():

def checkLight(channel):
    while 1:
        print '1'
        time.sleep(0.5)
        if GPIO.input(7) == 1:
            break
GPIO.add_event_detect(7, GPIO.FALLING, callback = checkLight , bouncetime=300)
    
def checkLight2(channel):
    while 1:
        print '2'
        time.sleep(0.5)
        if GPIO.input(10) == 1:
            break
GPIO.add_event_detect(10, GPIO.FALLING, callback = checkLight2 , bouncetime=300)
        
    
if __name__ == '__main__':
    main() 

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: help for multiple track

Wed Jul 29, 2015 5:56 pm

zzhou wrote:But i had a raspberry pi 1 and pi 2. the program works fine on pi 1 but it doesnt work well on pi 2.
What doesn't work? Are you getting errors? If so, provide some details and we'll try to help.

Dave.
Apple say... Monkey do !!

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: help for multiple track

Wed Jul 29, 2015 6:21 pm

Also, that code doesn't look right at all. Are you sure that's actually your code? This bit, for example, looks wrong:

Code: Select all

def main():

def checkLight(channel):
    while 1:
I would expect that to raise an IndentationError.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: help for multiple track

Wed Jul 29, 2015 6:33 pm

elParaguayo wrote:I would expect that to raise an IndentationError.
Just tried it and it did. Only on laptop so couldn't test full code.

Dave.
Apple say... Monkey do !!

zzhou
Posts: 3
Joined: Wed Jul 29, 2015 9:45 am

Re: help for multiple track

Thu Jul 30, 2015 1:28 am

elParaguayo wrote:Also, that code doesn't look right at all. Are you sure that's actually your code? This bit, for example, looks wrong:

Code: Select all

def main():

def checkLight(channel):
    while 1:
I would expect that to raise an IndentationError.
no error at all. It actually run in the main loop. just that i cant press tab on the code here.

zzhou
Posts: 3
Joined: Wed Jul 29, 2015 9:45 am

Re: help for multiple track

Thu Jul 30, 2015 1:33 am

davef21370 wrote:
zzhou wrote:But i had a raspberry pi 1 and pi 2. the program works fine on pi 1 but it doesnt work well on pi 2.
What doesn't work? Are you getting errors? If so, provide some details and we'll try to help.

Dave.
hi thanks Dave for the reply. the program is working fine by executing. But i'm not able to press another button when 1 button is pressed(holding). is there anyway to let me run 2 button concurrently and not sequentially?

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: help for multiple track

Thu Jul 30, 2015 6:26 am

OK. So you're saying those functions are defined within main. I agree that wouldn't give you an error.

It does look like this code should just run and exit as there's no loop other than those defined in the callbacks.

EDIT: It's not ideal to mix tabs and spaces for indents. I'm assuming your code looks like this:

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

GPIO.setup(7, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(10, GPIO.IN, pull_up_down = GPIO.PUD_UP)

def main():

    def checkLight(channel):
        while 1:
            print '1'
            time.sleep(0.5)
            if GPIO.input(7) == 1:
                break

    GPIO.add_event_detect(7, GPIO.FALLING, callback = checkLight , bouncetime=300)
    
    def checkLight2(channel):
        while 1:
            print '2'
            time.sleep(0.5)
            if GPIO.input(10) == 1:
                break

    GPIO.add_event_detect(10, GPIO.FALLING, callback = checkLight2 , bouncetime=300)
        
    
if __name__ == '__main__':
    main()
If that's the case, then it looks (to me) as if main will run, define your callbacks and then exit. However, that doesn't seem to tie in with the symptoms you're describing. Can you please paste your code (replacing tabs with spaces as appropriate) so we can see what's going on.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Return to “Python”