使用 python 调暗 LED,无需循环

Dim a LED with python without a loop

我想用 Raspberry Pi 3 调暗 LED。它完美地循环工作。但是我如何使 LED 持续调暗 50%?没有脚本 运行 循环。我想启动使 LED 变暗的 python 脚本,然后停止它并且 LED 应该仍然变暗。这可能吗?问题是,当我一直保持 运行 脚本时,Raspberry 的 CPU 负载非常高(一直)。

这是我的代码,循环有效。我只想删除第 10 行,但它不再起作用了。我完全 python 初学者。

import RPi.GPIO as IO
import time
IO.setwarnings(False)
IO.setmode (IO.BCM)
IO.setup(19,IO.OUT)
p = IO.PWM(19,100)
p.start(0)
while 1:
    p.ChangeDutyCycle(50)

据我所知,如果不连续 运行ning 脚本,您将无法获得该行为。如果你想在后台 运行 你的脚本,你应该查看 tmux。你可以用

得到它
$sudo apt-get install tmux

然后 运行 它与

$tmux

然后 运行 你的脚本。只要您的 RPi 打开,它就会在后台继续 运行。

编辑:格式化

您可以结合使用 python 和硬件。您可以使用 variable resistance as an analog dimmer.Then you can use the Rpi to increase the resistance digitally and get a dimmer light (not energy efficient and expensive depending on application). For fun here is a really complex(至少在我的书中)电子学沉重的答案,而不是使用循环(基本上是 PWM)。