Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中循环的多次迭代期间等待输入_Python_Loops_Raspberry Pi_User Input - Fatal编程技术网

在python中循环的多次迭代期间等待输入

在python中循环的多次迭代期间等待输入,python,loops,raspberry-pi,user-input,Python,Loops,Raspberry Pi,User Input,我正试图编写一个python代码来控制GPIO引脚的电压。 其背后的想法是,在1KHz的频率下,如果引脚一半时间关闭,一半时间打开,则电压将为50% 我需要保持while循环运行,同时等待用户输入,以便我可以更改电机的速度 import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(4, GPIO.OUT) # The higher the number for speed the slower the moto

我正试图编写一个python代码来控制GPIO引脚的电压。 其背后的想法是,在1KHz的频率下,如果引脚一半时间关闭,一半时间打开,则电压将为50%

我需要保持while循环运行,同时等待用户输入,以便我可以更改电机的速度

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT)

# The higher the number for speed the slower the motor will rotate
def motor(speed):
    i = 0
    run = True

    # Runs until user tells it to stop
    while run == True:

        # Pins should vary at every second
        time.sleep(0.001)
        i += 1
        if i % speed == 0:
            GPIO.output(4, GPIO.HIGH)
        else:
            GPIO.output(4, GPIO.LOW)

        # This block is causing the issue
        # I need the loop to keep running while waiting for the input
        if i % speed * 10 == 0:
            com = int(input("Enter 0 to quit"))
            if com == -1:
                GPIO.output(4, GPIO.LOW)
                break

    GPIO.output(4, GPIO.LOW)


run = True;
while (run):
    speed = int(input("Select Speed"))

    if speed == 0:
        run = False
    else:
        motor(speed)

GPIO.cleanup()

你的实际问题是什么?如何在保持循环运行和扫描仪打开的同时,用你在评论中提出的问题更新你的原始帖子,你将更有可能从其他人那里获得帮助。看看这个问题:预期用户的输入是什么,这是一个字符串还是仅仅一次按键?你真正的问题是什么?你如何保持循环运行并同时打开扫描仪?如果你用你在评论中提出的问题更新你的原始帖子,你将更有可能从其他人那里得到帮助。看看这个问题:预期用户的输入是什么,它是一个字符串还是一次按键?