Member-only story
MicroPython Tutorial XIII
We did quite a few tutorials with the brick buttons and then switched to a more basic task, driving in a straight line. Lets focus on something different, something not present in the LEGO EDU kit the IR Remote and IR Sensor.
You need two lines to build an IR Sensor into your python code.
from pybricks.ev3devices import Motor, InfraredSensor
infra = InfraredSensor(Port.S4)
The first line simply names the class with the details of the device. The second defines the physical place the sensor is plugged into your brick.
Beyond this your going to be testing the values returned within it to see which buttons on your brick are being pressed. The basic code in a loop looks like this.
while True:
buttonPressed = infra.buttons(1)
The number in brackets, one in our case in the channel on which the remote is working. There are only 4 channels, which is challenge in itself in a class, controlled by the red switch on the remote itself.
Now what does it return. To try and keep some good coding practice in here I am going to define a whole set of returned values.
redHigh = 128
blueHigh = 512
redAndblueHigh = redHigh + blueHigh
redLow = 2
blueLow = 8
redAndBlueLow = redLow + blueLow
redCrossBlue = redHigh + blowLow
blueCrossRed = blueHigh + redLow
allRed = redHigh + redLow
allBlue = blueHigh + blueLow
beacon = 256
Cavet, the last value when you press the beacon button…