• 树莓派瑞士军刀扩展板

Arduino

MicroPython

micro:bit

# LED亮度渐变

## 元件

• 标准5或3mm的LED
• 100 Ohm 电阻
• 多条电线
• 面包板 (选用，但可方便工作)

# 代码

```from pyb import Timer
from time import sleep

# timer 5 will be created with a frequency of 100 Hz 定时器将以100Hz的频率创建
tim = pyb.Timer(5, freq=100)
tchannel = tim.channel(1, Timer.PWM, pin=pyb.Pin.board.X1, pulse_width=0)```

```# maximum and minimum pulse-width, which corresponds to maximum
# and minimum brightness 与最大和最小亮度对应的最大和最小脉宽
max_width = 200000
min_width = 20000

# how much to change the pulse-width by each step 每一步骤对脉宽改变的幅度
wstep = 1500
cur_width = min_width

while True:
tchannel.pulse_width(cur_width)

# this determines how often we change the pulse-width. It is
# analogous to frames-per-second 此处决定我们改变脉宽的频率，类似于每秒帧数
sleep(0.01)

cur_width += wstep

if cur_width > max_width:
cur_width = min_width```

# 呼吸灯效果

```while True:
tchannel.pulse_width(cur_width)

sleep(0.01)

cur_width += wstep

if cur_width > max_width:
cur_width = max_width
wstep *= -1
elif cur_width < min_width:
cur_width = min_width
wstep *= -1```

# 附录

pyboard/tutorial/fading_led.1548930883.txt.gz · 最后更改: 2019/01/31 18:34 (外部编辑)