Skip to content

Actividad

Luz nocturna

Principiante | MakeCode, Python | Pantalla LED, Sensor de luz | Entrada/salida, Iteración, Luz, Selección, Sensores, Sistemas y controles

Paso 1: Hazlo

¿Qué es?

A nightlight that lights up your BBC micro:bit’s LED display in the dark.

Introducción

Guía de programación

This project uses the micro:bit’s LEDs as a light sensor input to make a light that switches on automatically when it gets dark.

The micro:bit’s light sensor measures light in a range from 0 (very dark) to 255 (very bright).

Cómo funciona

  • An infinite loop in the code keeps the micro:bit checking light levels.
  • It uses logic to decide whether to turn the LEDs on or off. A conditional instruction (if… then… else) makes the decision to turn the LED lights on or off.
  • If the light level falls below 100, then it lights up the LEDs on the micro:bit’s display. Else (otherwise), it clears the screen to turn the LED lights off.
  • Test it out by covering the display or shining a light on it, and see if the LEDs light up when it’s dark.
  • You may need to change the 100 number depending on the light levels around you. Larger numbers will make the light come on more easily. Smaller numbers will make the light only come on when it’s very dark.

Qué necesitas

  • micro:bit (o simulador MakeCode)
  • Editor de MakeCode o de Python
  • pilas (opcionales)
  • una fuente de luz y algo para tapar el micro:bit

Paso 2: Prográmalo

1from microbit import *
2
3while True:
4    if display.read_light_level() < 100:
5        display.show(Image(
6        "99999:"
7        "99999:"
8        "99999:"
9        "99999:"
10        "99999"))
11    else:
12        display.clear()
13    sleep(2000)
14

Paso 3: Mejóralo

  • Haz que muestre una luna o una estrella cuando esté oscuro.
  • Engancha el micro:bit a tu mochila o ropa para usarlo como una luz extra de seguridad cuando vayas paseando o yendo en bicicleta – ¿puedes hacer que parpadee para que sea más llamativo?
  • Prueba este proyecto de MakeCode que hace que la pantalla LED ajuste su brillo dependiendo de cuánta luz mida el micro:bit. ¿Dónde has visto otras cosas que reaccionen así a cambios de luz?