mirror of
https://github.com/pybricks/pybricks-api.git
synced 2026-09-11 17:14:41 +00:00
These code samples are rendered in the docs, so we include them here. For the git history of these snippets, see: https://github.com/pybricks/pybricks-projects/commits/f4914069aaacfd9ab1e0dd6f05524f62cfc56b29/snippets
28 lines
669 B
Python
28 lines
669 B
Python
from pybricks.pupdevices import Light
|
|
from pybricks.parameters import Port
|
|
from pybricks.tools import wait, StopWatch
|
|
|
|
# The math module is part of standard MicroPython:
|
|
# https://docs.micropython.org/en/latest/library/math.html
|
|
from math import pi, cos
|
|
|
|
# Initialize the light and a StopWatch.
|
|
light = Light(Port.A)
|
|
watch = StopWatch()
|
|
|
|
# Cosine pattern properties.
|
|
PERIOD = 2000
|
|
MAX = 100
|
|
|
|
# Make the brightness fade in and out.
|
|
while True:
|
|
# Get phase of the cosine.
|
|
phase = watch.time()/PERIOD*2*pi
|
|
|
|
# Evaluate the brightness.
|
|
brightness = (0.5 - 0.5*cos(phase))*MAX
|
|
|
|
# Set light brightness and wait a bit.
|
|
light.on(brightness)
|
|
wait(10)
|