From dc82cd5d07d85bdb96b6a09bac65d636a928c3b0 Mon Sep 17 00:00:00 2001 From: Laurens Valk Date: Sat, 11 Jul 2020 12:58:15 +0200 Subject: [PATCH] snippets/pup/light: add basic light and math demo --- snippets/pup/light/basics.py | 16 ++++++++++++++++ snippets/pup/light/math.py | 27 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 snippets/pup/light/basics.py create mode 100644 snippets/pup/light/math.py diff --git a/snippets/pup/light/basics.py b/snippets/pup/light/basics.py new file mode 100644 index 0000000..a659757 --- /dev/null +++ b/snippets/pup/light/basics.py @@ -0,0 +1,16 @@ +from pybricks.pupdevices import Light +from pybricks.parameters import Port +from pybricks.tools import wait + +# Initialize the light. +light = Light(Port.A) + +# Blink the light forever. +while True: + # Turn the light on at 100% brightness. + light.on(100) + wait(500) + + # Turn the light off. + light.off() + wait(500) diff --git a/snippets/pup/light/math.py b/snippets/pup/light/math.py new file mode 100644 index 0000000..459d32e --- /dev/null +++ b/snippets/pup/light/math.py @@ -0,0 +1,27 @@ +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)