Hi, I’m only relatively new to the world of BrickPi and have recently updated the firmware on my device as so it can accept the EV3 sensors which I have with my Lego Mindstorms kit.
Thought I might share some code that I’ve put together. It uses the motors plugged into MA and MB and the EV3 touch sensor plugged into S4.
Pressing the button will spin the motors forward, pressing the button again will spin the motors in reverse and so on and so forth.
To run it, place the attached code under Desktop/BrickPi_Python/SensorExamples and call it say, Switch-Motor.py
call the code by issuing the following line:
sudo python Switch-Motor.py
It’s only a simple example, but I’m going to look into the Infrared and the Colour Sensor next and see what I can come up with.
Anyway, in the meantime, I hope this example helps others out with the EV3 sensors.
from BrickPi import * #import BrickPi.py file to use BrickPi operations
BrickPiSetup() # setup the serial port for communication
BrickPi.MotorEnable[PORT_A] = 1 #Enable the Motor A
BrickPi.MotorEnable[PORT_B] = 1 #Enable the Motor B
BrickPi.SensorType[PORT_4] = TYPE_SENSOR_EV3_TOUCH_DEBOUNCE #Set the type of sensor at PORT_4
BrickPiSetupSensors() #Send the properties of sensors to BrickPi
power = 200
BrickPi.MotorSpeed[PORT_A] = power #Set the speed of MotorA (-255 to 255)
BrickPi.MotorSpeed[PORT_B] = power #Set the speed of MotorB (-255 to 255)
r1 = BrickPi.Sensor[PORT_4]
r2 = BrickPi.Sensor[PORT_4]
r3 = BrickPi.Sensor[PORT_4]
if (r1==1 and r2==1 and r3==1): #The button has been pressed if buttonstatus==0: # button has been pressed since last sampling power=-power #Get ready to reverse the motor buttonstatus=1 #Remember the button has been pressed. buttonpress=1 else: if buttonpress==1: #button has been released in this sampling and had currently been recorded as pressed buttonstatus=0 #Remember the button has now been released. time.sleep(.1)