This document discusses using light sensing with the micro:bit. It begins by explaining light measurement units and light sensing components. It then covers how to sense light levels using the LEDs on the micro:bit by switching them to analog inputs. Two practice exercises are described - an automatic night light that adjusts brightness based on light levels, and a face-changing display that shows different icons when light is blocked from the LEDs.
21. Python程式
def showICON():
if index == 0:
basic.show_icon(IconNames.HAPPY)
elif index == 1:
basic.show_icon(IconNames.SAD)
elif index == 2:
basic.show_icon(IconNames.CONFUSED)
elif index == 3:
basic.show_icon(IconNames.ANGRY)
elif index == 4:
basic.show_icon(IconNames.ASLEEP)
else:
basic.show_icon(IconNames.SURPRISED)
實作練習 -川劇變臉 4/5
22. index = -1
basic.show_string("?")
def on_forever():
global index
if input.light_level() < 35:
while input.light_level() < 35:
pass
index = (index + 1) % 6
showICON()
basic.forever(on_forever)
實作練習 -川劇變臉 5/5