mirror of
https://github.com/pybricks/pybricks-api.git
synced 2026-09-12 09:37:25 +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
33 lines
1013 B
Python
33 lines
1013 B
Python
from pybricks.pupdevices import UltrasonicSensor
|
|
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, sin
|
|
|
|
# Initialize the sensor.
|
|
eyes = UltrasonicSensor(Port.A)
|
|
|
|
# Initialize a timer.
|
|
watch = StopWatch()
|
|
|
|
# We want one full light cycle to last three seconds.
|
|
PERIOD = 3000
|
|
|
|
while True:
|
|
# The phase is where we are in the unit circle now.
|
|
phase = watch.time()/PERIOD*2*pi
|
|
|
|
# Each light follows a sine wave with a mean of 50, with an amplitude of 50.
|
|
# We offset this sine wave by 90 degrees for each light, so that all the
|
|
# lights do something different.
|
|
brightness = [sin(phase + offset*pi/2) * 50 + 50 for offset in range(4)]
|
|
|
|
# Set the brightness values for all lights. The * symbol unpacks the list
|
|
# of brightness values into separate arguments.
|
|
eyes.lights.on(*brightness)
|
|
|
|
# Wait some time.
|
|
wait(50)
|