# Project based on:
# - pybricks[dot]com/project/technic-42109-xbox/
# - docs.pybricks[dot]com/en/latest/robotics.html?highlight=car#pybricks.robotics.Car.drive_power
from pybricks.iodevices import XboxController
from pybricks.parameters import Button, Direction, Port
from pybricks.pupdevices import Motor
from pybricks.robotics import Car
from pybricks.tools import wait
# Set up all devices
steering = Motor(Port.B, Direction.CLOCKWISE)
drive = Motor(Port.D, Direction.CLOCKWISE)
car = Car(steering, drive)
xbox = XboxController()
# The main program starts here
while True:
# Control steering using the left joystick
car.steer(xbox.joystick_left()[0])
# Check if any Xbox controller's button is pressed
buttons_pressed = xbox.buttons.pressed()
# Button 'A' acts as brake
is_brake = Button.A in buttons_pressed
drive_power = xbox.triggers()[1] - xbox.triggers()[0]
# Braking has priority over driving (backwards or forwards)
power = 0 if is_brake else drive_power
# Control drive power using the trigger buttons
# Press 'A' to brake
car.drive_power(power)
wait(50)
To embed this program on your website, copy the following code and paste it into your website's HTML: