Interrupts example

I want to use interrupts like this:

import RPi.GPIO as GPIO
import time
GPIO.setup(24, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)

def Interrupt(channel):
GPIO.add_event_detect(24, GPIO.RISING, callback = Interrupt, bouncetime = 200)
while True:

But instead the pin 24 i want to ask the touch sensor. How can i use interrupts in brickpi? It would be nice, if somebody give me an example.

Hello Modulok,

This isn’t going to be possible. You cannot control a sensor directly through the GPIO’s of the Pi; rather the BrickPi sends a message to the sensors over the serial line and to the microcontroller on the BrickPi. The BrickPi controls the LEGO / EV3 / NXT sensors.

To read sensors attached to the BrickPi, you can see our examples on github for each sensor?

Hello, i dont want to control a sensor through the gpio. This is just an example. Is it possible to use interrupts? I know there is a command *waitForChange() * But i dont understand the docs really. Where can i read an example to use interrupts? I cant find it in the sensor or project examples. I think its not a good solution to ask a touch sensor every .1 seconds. It is better to ask a sensor only when it change its value. Or am i wrong?

I don’t think that there is any way by which you can use interrupts directly. Though you can make a something like this to find the change of state:

from BrickPi import *   #import file to use BrickPi operations

BrickPiSetup()  # setup the serial port for communication

BrickPi.SensorType[PORT_4] = TYPE_SENSOR_TOUCH   #Set the type of sensor at PORT_1

BrickPiSetupSensors()   #Send the properties of sensors to BrickPi
def check_touch():
	global last_state,current_state
	result = BrickPiUpdateValues()  # Ask BrickPi to update values for sensors/motors 
    if not result :
        current_state=BrickPi.Sensor[PORT_4]     #BrickPi.Sensor[PORT] stores the value obtained from sensor
	if current_state<>last_state:
		return current_state
		return -1
while True:
	if st<>-1:
		print "state changed to",st
    time.sleep(.01)     # sleep for 10 ms

You can use this with threads if you want even better response.


Thanks! That works!