如何为此程序编写循环,以便每次都返回顶部?

时间:2017-02-03 17:33:20

标签: python python-2.7

这是我正在使用的代码。它是使步进电机旋转但在每次经过代码后它完全停止。我怎样才能让代码重新开始?

import RPi.GPIO as GPIO, time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
p = GPIO.PWM(16, 500)


def SpinMotor(direction, num_steps):
    GPIO.output(18, direction)
    while num_steps > 0:
        p.start(1)
        time.sleep(0.01)
        num_steps -= 1
    p.stop()
    GPIO.cleanup()
    return True

direction_input = raw_input('Please enter O or C for Open or Close')
num_steps = input('Please enter the number of steps:')
if direction_input == 'O':
    SpinMotor(True, num_steps)

else:
    SpinMotor(False, num_steps)

1 个答案:

答案 0 :(得分:0)

你可以创建一个这样的循环:

while True:
    direction_input = raw_input('Please enter O or C for Open or Close. Q to quit.')
    if direction_input == 'Q':
        break
    num_steps = input('Please enter the number of steps:')
    if direction_input == 'O':
        SpinMotor(True, num_steps)

    else:
        SpinMotor(False, num_steps)