Skip to content

Gweithgaredd

Golau nos

Dechreuwr | MakeCode, Python | Dangosydd LED, Synhwyrydd golau | Dewis, Golau, Iteriad, Mewnbwn/allbwn, Synwyryddion, Systemau a rheolaeth

Cam 1: Gwneud

Beth yw e?

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

Cyflwyniad

Canllaw codio

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).

Sut mae'n gweithio

  • 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.

Beth sydd ei angen arnoch

  • micro:bit (neu efelychwr MakeCode)
  • Golygydd MakeCode neu Python
  • pecyn batri (opsiynol)
  • ffynhonnell golau a rhywbeth i'w ddefnyddio i orchuddio'r micro:bit

Cam 2: Codio

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

Cam 3: Gwella

  • Newid y ddelwedd i ddangos lleuad neu seren pan fydd yn tywyllu.
  • Atodi'r micro:bit i'ch bag neu ddillad i ddefnyddio hyn fel golau diogelwch ychwanegol wrth gerdded neu seiclo - allwch chi wneud iddo fflachio i fod yn fwy amlwg?
  • Rhoi cynnig ar y prosiect MakeCode hwn sy'n gwneud i'r dangosydd LED fod yn fwy golau ac yn fwy tywyll gan ddibynnu ar faint o olau sy'n cyrraedd y micro:bit. Ble arall rydych wedi gweld pethau eraill sy'n ymateb i olau yn y ffordd hon?