等待输入时执行无限循环

2024-04-18 00:21:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个小项目我正在做,它相当简单,所以我希望有人可以帮助我。在

我用树莓皮调暗一个单一的LED与一些非常粗糙的脉宽调制。在

我的PWM代码如下:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(7, GPIO.OUT)
frequency = 0.005
dwell = 0.0001
while True:
    time.sleep(frequency)
    GPIO.output(7, 1)
    time.sleep(dwell)
    GPIO.output(7, 0)

基本上,为了使LED灯保持在由“驻留”决定的亮度,我需要这一位代码继续循环下去。在

我想用的是

^{pr2}$

因此,当PWM代码循环时,我可以在驻留时间内增加一个新值,以调整LED的亮度。在

到目前为止,我的所有努力都产生了以下结果之一:

答:调光循环只执行一次并停止等待输入 b: 调光回路将无限执行,但不允许进一步输入

你们中的一个优秀的人能给我一个代码示例来解释我如何做到这一点吗?在

对于那些感兴趣的人,最终我想做的是通过插座设置驻留值,并使用一种更好的PWM输出形式来驱动LED筒灯。婴儿步骤:)


Tags: 项目代码importoutputledgpiotimesleep
1条回答
网友
1楼 · 发布于 2024-04-18 00:21:47

看起来你需要multithreading!在

# import the module
import threading

# define a function to be called in the other thread
def get_input():
    while True:
        dwell=raw_input()

# create a Thread object
input_thread=threading.Thread(target=get_input)

# start the thread
input_thread.start()

# now enter the infinite loop
while True:
    time.sleep(frequency)
    GPIO.output(7, 1)
    time.sleep(dwell)
    GPIO.output(7, 0)

这里可能有关于lockssemaphoresmutex...es (mutices?)的东西,但我不太了解这些。像这样简单的事情似乎对我有用。在

相关问题 更多 >

    热门问题