Bharath333
Posts: 1
Joined: Sat Sep 15, 2018 6:07 am

Multiple add_event_detect for single GPIO pin and multiple callbacks

Sat Sep 15, 2018 6:35 am

Hi,

I have two IR obstacle sensors. I need two increase count when any obstacle passes from first sensor to second and decrease if obstacle passes from second to first. I also have to check if the obstacle is passed through first sensor and didn't go through second, it should retrieve its value to default, same if it happens for the second sensor. Below is the code I have written for this

Code: Select all

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(first_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)    
GPIO.setup(second_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)	  

first = False
second = False
count=0

while(True):
	if(GPIO.input(first_pin)):
		if second == True:
			count-=1
			first= False
			second= False
		else:
			first = True
	elif(GPIO.input(second_pin)):
		if first == True:
			count+=1
			first= False
			second= False
		else:
			second = True
	print (count)
In the above code I'm facing issue when obstacle is passed through first and didn't go through second. In such case I need to reset 'first' value back to 'False' else when any thing passes through second to first it's increasing the count instead of decreasing. Please help me in this

Thanks

pcmanbob
Posts: 4462
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Multiple add_event_detect for single GPIO pin and multiple callbacks

Sat Sep 15, 2018 9:24 pm

Hi.

So are the objects passing by your sensors one at a time, or are you sensors moving past the objects ?

you probably need to test for the second sensor activation after the first sensor activation on a timed bases.

So if object a passes sensor 1 then start checking for sensor 2 activation if no activation after specified time do nothing , if object passes sensor 2 in specified time do your count + 1

by the way you are not using Multiple add_event_detect & callbacks , you code is just an endless loop with if statements.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

Idahowalker
Posts: 373
Joined: Wed Jan 03, 2018 5:43 pm

Re: Multiple add_event_detect for single GPIO pin and multiple callbacks

Sat Sep 15, 2018 9:37 pm

Without knowing why you are deleting my postings, I will not know how...

User avatar
paddyg
Posts: 2227
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Multiple add_event_detect for single GPIO pin and multiple callbacks

Sun Sep 16, 2018 9:25 am

From your description of what you want to happen I would have thought the loop should record the times that objects pass each sensor and the logic count accordingly i.e

Code: Select all

if tm1 > tm2 and (tm1 - tm2) < MAXTM:
    count +=  1
elif tm2 > tm1 and (tm2 - tm1) < MAXTM:
    count -= 1
You also need to take into account the fact that you don't want to keep counting when a certain status is happening - rather only count once when the status changes. You can do that more easily with edge detection and callback functions but using your single loop method you would have to store the previous state of each GPIO pin. You would probably also need to put in some logic to stop multiple counting edges i.e. with an additional time condition.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”