I wrote this simple test program in python to check if something happen when I press a button in my Raspberry Pi:
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.IN, pull_up_down = GPIO.PUD_UP)
testVar=0
def my_callback(channel):
print "Pressed!"
testVar= 32
GPIO.add_event_detect(24, GPIO.FALLING, callback=my_callback, bouncetime=200)
while True:
print str(testVar)
sleep(0.5)
I'm reading only 0 values and when I pressed the button I see "Pressed!" but the variable did not change. From what I understand the reason is because the the callback function is lunched as new Thread and of course the variable cannot be setted correctly. Is there a way to send a shared var to the callback function in some way?
thanks a lot for any good advice.