Python 如何为该程序编写循环,使其每次返回顶部?
这是我正在使用的代码。这是使步进电机旋转,但每次通过代码后,它完全停止。我怎样才能让代码重新开始Python 如何为该程序编写循环,使其每次返回顶部?,python,python-2.7,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)
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)
您可以创建如下循环:
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)
为True时:
?