Skip to content

Instantly share code, notes, and snippets.

@chris-gunawardena
Last active November 4, 2023 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-gunawardena/2dd099a0ee1902c0d38bf1897b769387 to your computer and use it in GitHub Desktop.
Save chris-gunawardena/2dd099a0ee1902c0d38bf1897b769387 to your computer and use it in GitHub Desktop.
from pybricks.pupdevices import Remote
from pybricks.parameters import Button, Color
from pybricks.hubs import TechnicHub
from pybricks.pupdevices import Motor
from pybricks.parameters import Port
from pybricks.tools import wait
hub = TechnicHub()
# init remote
hub.light.on(Color.YELLOW)
remote = Remote(timeout=None)
hub.light.on(Color.GREEN)
# init motors
speed = 300
motorA = None
motorB = None
try:
motorA = Motor(Port.A)
except OSError:
print("Could not find motorA.")
try:
motorB = Motor(Port.B)
except OSError:
print("Could not find motorB.")
# button colors
def button_to_color(buttons):
# Return a color depending on the button.
if Button.LEFT_PLUS in buttons:
return Color.RED
if Button.LEFT_MINUS in buttons:
return Color.GREEN
if Button.LEFT in buttons:
return Color.ORANGE
if Button.RIGHT_PLUS in buttons:
return Color.BLUE
if Button.RIGHT_MINUS in buttons:
return Color.YELLOW
if Button.RIGHT in buttons:
return Color.CYAN
if Button.CENTER in buttons:
return Color.VIOLET
# Return no color by default.
return Color.NONE
# main loop
while True:
pressed = remote.buttons.pressed()
# change color on button press
if pressed:
# set light color
color = button_to_color(pressed)
remote.light.on(color)
hub.light.on(color)
# Port A
if motorA:
if Button.LEFT_PLUS in pressed:
motorA.run(speed)
elif Button.LEFT_MINUS in pressed:
motorA.run(-1 * speed)
else:
motorA.stop()
# Port B
if motorB:
if Button.RIGHT_PLUS in pressed:
motorB.run(speed)
elif Button.RIGHT_MINUS in pressed:
motorB.run(-1 * speed)
else:
motorB.stop()
# Speed
if Button.LEFT in pressed:
speed = max(50, speed - 50)
wait(100)
print("speed:" + str(speed))
if Button.RIGHT in pressed:
speed = speed + 50
wait(100)
print("speed:" + str(speed))
# if Button.CENTER in buttons:
from pybricks.pupdevices import Remote, ColorDistanceSensor, Light
from pybricks.parameters import Button, Color, Direction, Port, Side, Stop
from pybricks.hubs import TechnicHub
from pybricks.pupdevices import Motor
from pybricks.parameters import Port
from pybricks.robotics import DriveBase
from pybricks.tools import wait, StopWatch
hub = TechnicHub()
# init remote
hub.light.on(Color.YELLOW)
remote = Remote(timeout=None)
hub.light.on(Color.GREEN)
# init motors
motorA = None
motorB = None
motorC = None
colour_sensor = None
light = None
color_array = [
Color.NONE,
Color.GREEN,
Color.YELLOW,
Color.ORANGE,
Color.RED
];
top_speed = 1000
colour_int = 2
speed = top_speed * ((colour_int+1) / len(color_array))
try:
motorA = Motor(Port.A)
except OSError:
print("Could not find motorA.")
try:
motorB = Motor(Port.B)
except OSError:
print("Could not find motorB.")
try:
motorC = Motor(Port.C)
except OSError:
print("Could not find motorC.")
try:
colour_sensor = ColorDistanceSensor(Port.D)
except OSError:
print("Could not find colour_sensor.")
try:
light = Light(Port.D)
except OSError:
print("Could not find light.")
# button colors
def button_to_color(buttons):
# Return a color depending on the button.
if Button.LEFT_PLUS in buttons:
return Color.RED
if Button.LEFT_MINUS in buttons:
return Color.GREEN
if Button.LEFT in buttons:
return Color.ORANGE
if Button.RIGHT_PLUS in buttons:
return Color.BLUE
if Button.RIGHT_MINUS in buttons:
return Color.YELLOW
if Button.RIGHT in buttons:
return Color.CYAN
if Button.CENTER in buttons:
return Color.VIOLET
# Return no color by default.
return Color.NONE
# main loop
while True:
pressed = remote.buttons.pressed()
# change color on button press
# if pressed:
# # set light color
# color = button_to_color(pressed)
# remote.light.on(color)
# hub.light.on(color)
# sensor.light.on(color)
# Port A
if motorA:
if Button.LEFT_PLUS in pressed:
motorA.run(speed)
elif Button.LEFT_MINUS in pressed:
motorA.run(-1 * speed)
else:
motorA.stop()
# Port B
if motorB:
if Button.RIGHT_PLUS in pressed:
motorB.run(speed)
elif Button.RIGHT_MINUS in pressed:
motorB.run(-1 * speed)
else:
motorB.stop()
# Port C
if motorC:
if Button.RIGHT in pressed:
motorC.run(speed)
elif Button.LEFT in pressed:
motorC.run(-1 * speed)
else:
motorC.stop()
# Port D / speed
if Button.CENTER in pressed:
colour_int = (colour_int+1) % len(color_array)
colour = color_array[colour_int]
remote.light.on(colour)
hub.light.on(colour)
speed = top_speed * ((colour_int+1) / len(color_array))
if colour_sensor:
colour_sensor.light.on(colour)
if light:
light.on(100 * colour_int / len(color_array))
wait(100)
# # Speed
# if Button.LEFT in pressed:
# speed = max(50, speed - 50)
# wait(50)
# print("speed:" + str(speed))
# if Button.RIGHT in pressed:
# speed = speed + 50
# wait(50)
# print("speed:" + str(speed))
# if Button.CENTER in buttons:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment