Page 1 of 1
GPIO event detection with break statement
Posted: Fri Jun 08, 2018 9:59 am
by Chandru42
i am using GPIO event detect to detect a button press in while loop inside a class and once the button press it is detected , the while loop has to break and the program should execute the consequent statements. I have given here callback = break . But it is showing error. Can someone please help me to resolve the error.
Code: Select all
def real_program(self):
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(24, GPIO.RISING, callback= break, bouncetime=200)
adc = Adafruit_ADS1x15.ADS1015()
GAIN = 1
t = []
values = []
str_time = time.time()
try:
while true:
val = adc.read_adc(0, gain=GAIN)
t.append(time.time() - str_time)
values.append(val)
except KeyboardInterrupt:
GPIO.cleanup() # clean up GPIO on CTRL+C exit
GPIO.cleanup()
x = [round (float(b*1000.0) ,2) for b in t]
y = [float (c*0.002) for c in values]
z = [round(float(d*48.484) ,2) for d in y]
return x,y,z
Re: GPIO event detection with break statement
Posted: Fri Jun 08, 2018 10:25 am
by joan
You need to specify a function for the callback.
I suggest in the callback function you set a global variable called exit_loop and set it to True.
Then rather than while True: do exit_loop = False and while not exit_loop:
Re: GPIO event detection with break statement
Posted: Fri Jun 08, 2018 11:34 am
by Chandru42
Thanks for your reply. but unfortunately i didn't understand your answer correctly. And also i think there is no do while loop. Can you explain me little bit?
Is there any other way to come out of a loop?.
Because the loop has to run very fast , i think when every time it checks for a condition, it makes the program little slow..
Re: GPIO event detection with break statement
Posted: Fri Jun 08, 2018 11:46 am
by joan
There is only one while loop. That is the one I mean.
I'm afraid you will have to take the insignificant performance hit of checking the while loop for the exit condition. In fact there is little difference between while True and while not exit_loop.
Re: GPIO event detection with break statement
Posted: Fri Jun 08, 2018 12:39 pm
by Chandru42
Thanks a lot. Now i got you!!! One last querry.. Now i got a new error
function do_something takes only 1 argument two given.
I have only given self ,nothing else. Can you please clarify also this?
Code: Select all
def real_program(self):
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(24, GPIO.RISING, callback= do_something, bouncetime=200)
adc = Adafruit_ADS1x15.ADS1015()
GAIN = 1
t = []
values = []
global exit_loop
exit_loop =False
str_time = time.time()
try:
while not exit_loop:
val = adc.read_adc(0, gain=GAIN)
t.append(time.time() - str_time)
values.append(val)
except KeyboardInterrupt:
GPIO.cleanup() # clean up GPIO on CTRL+C exit
GPIO.cleanup()
x = [round (float(b*1000.0) ,2) for b in t]
y = [float (c*0.002) for c in values]
z = [round(float(d*48.484) ,2) for d in y]
return x,y,z
def do_something(self):
global exit_loop
self.exit_loop = True
Re: GPIO event detection with break statement
Posted: Fri Jun 08, 2018 5:55 pm
by Paeryn
Chandru42 wrote: ↑Fri Jun 08, 2018 12:39 pm
function do_something takes only 1 argument two given.
I have only given self ,nothing else. Can you please clarify also this?
Code: Select all
def real_program(self):
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(24, GPIO.RISING, callback= do_something, bouncetime=200)
adc = Adafruit_ADS1x15.ADS1015()
GAIN = 1
t = []
values = []
global exit_loop
exit_loop =False
str_time = time.time()
try:
while not exit_loop:
val = adc.read_adc(0, gain=GAIN)
t.append(time.time() - str_time)
values.append(val)
except KeyboardInterrupt:
GPIO.cleanup() # clean up GPIO on CTRL+C exit
GPIO.cleanup()
x = [round (float(b*1000.0) ,2) for b in t]
y = [float (c*0.002) for c in values]
z = [round(float(d*48.484) ,2) for d in y]
return x,y,z
def do_something(self):
global exit_loop
self.exit_loop = True
The callback function will be passed the channel number (which gpio pin it occured on), as it looks like your functions are inside a class Python is trying to pass two parameters, 1st the object that was self when the callback was set and 2nd, the channel number.
Also in your
do_something() you declare
exit_loop to be a global but then set
self.exit_loop which is a totally separate object local to the object
self.
So you want something more like :-
Code: Select all
def do_something(self, channel):
global exit_loop
exit_loop = True