Member-only story
MicroPython Tutorial VIII
Ok, lets go back to basics. Here is the code to do the basic buttons on the brick, with some additions to include the motor classes.
#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.parameters import Button, Port
from pybricks.ev3devices import Motorwhile True:
if Button.DOWN in brick.buttons():
print(“DOWN button”,Button)
if Button.UP in brick.buttons():
print(“UP button”,Button)
To can then define motors by indicating which port they are connected too. Obviously in this example the motors would be physically connected to port C and port B.
leftMotor = Motor(Port.B)
rightMotor = Motor(Port.C)
A word of warning, the error messages you get you get when the ports aren’t connected firmly are not very explicit. This an example of what comes back if one of the motors isn’t connected at the time of writing. A feature of MicroPython I fear.
Traceback (most recent call last):
File “./motor.py”, line 8, in <module>
OSError: [Errno 19] ENODEV
Assuming you managed to connect them and you’re ready to go, try combining the commands you need to run the motors with your base script. You should end up with something along these lines.
while True:
print(leftMotor.speed())
print(rightMotor.speed())
if Button.DOWN in brick.buttons():
leftMotor.run(-1020)
rightMotor.run(-1020)
if Button.UP in…