我为Orange PI编写了一个简单的while循环,以控制5个LED,我不知道如何在true的情况下从第一个循环返回。 现在第二个循环程序结束后,我想回到第一个循环
您能建议怎么做吗?
import os
import time
os.system("echo 37 > /sys/class/gpio/export,echo out > /sys/class/gpio/gpio37/direction,echo 38 > /sys/class/gpio/export,echo out > /sys/class/gpio/gpio38/direction,echo 39 > /sys/class/gpio/export,echo out > /sys/class/gpio/gpio39/direction,echo 101 > /sys/class/gpio/export,echo out > /sys/class/gpio/gpio101/direction,echo 36 > /sys/class/gpio/export,echo out > /sys/class/gpio/gpio36/direction")
#zmienne
sl ="sleep 0.5"
i = 1
imax = 10
y = 0.2
z = 0.1
val = 10
value = 1
L1_On = "echo 1 > /sys/class/gpio/gpio37/value"
L1_Off = "echo 0 > /sys/class/gpio/gpio37/value"
L2_On = "echo 1 > /sys/class/gpio/gpio38/value"
L2_Off = "echo 0 > /sys/class/gpio/gpio38/value"
L3_On = "echo 1 > /sys/class/gpio/gpio39/value"
L3_Off = "echo 0 > /sys/class/gpio/gpio39/value"
L4_On = "echo 1 > /sys/class/gpio/gpio101/value"
L4_Off = "echo 0 > /sys/class/gpio/gpio101/value"
L5_On = "echo 1 > /sys/class/gpio/gpio36/value"
L5_Off = "echo 0 > /sys/class/gpio/gpio36/value"
timeout = time.time() + 5 # 5 seconds
while True:
print ("2 prog", i)
os.system(L1_On)
time.sleep (z)
os.system(L1_Off)
#time.sleep (y)
os.system(L2_On)
time.sleep (z)
os.system(L2_Off)
#time.sleep (y)
os.system(L3_On)
time.sleep (z)
os.system(L3_Off)
#time.sleep (y)
os.system(L4_On)
time.sleep (z)
os.system(L4_Off)
#time.sleep (y)
os.system(L5_On)
time.sleep (z)
os.system(L5_Off)
#time.sleep(y)
if time.time() > timeout:
break
continue
#right follow
timeout = time.time() + 5 # 5 seconds
while True:
print ("2 prog", i)
os.system(L5_On)
time.sleep (z)
os.system(L5_Off)
#time.sleep (y)
os.system(L4_On)
time.sleep (z)
os.system(L4_Off)
#time.sleep (y)
os.system(L3_On)
time.sleep (z)
os.system(L3_Off)
#time.sleep (y)
os.system(L2_On)
time.sleep (z)
os.system(L2_Off)
#time.sleep (y)
os.system(L1_On)
time.sleep (z)
os.system(L1_Off)
#time.sleep(y)
if time.time() > timeout:
break
continue
答案 0 :(得分:2)
尝试将两个循环放入另一个while True:
循环中,即:
import os
import time
os.system("echo 37 > /sys/class/gpio/export,echo out > /sys/class/gpio/gpio37/direction,echo 38 > /sys/class/gpio/export,echo out > /sys/class/gpio/gpio38/direction,echo 39 > /sys/class/gpio/export,echo out > /sys/class/gpio/gpio39/direction,echo 101 > /sys/class/gpio/export,echo out > /sys/class/gpio/gpio101/direction,echo 36 > /sys/class/gpio/export,echo out > /sys/class/gpio/gpio36/direction")
#zmienne
sl ="sleep 0.5"
i = 1
imax = 10
y = 0.2
z = 0.1
val = 10
value = 1
L1_On = "echo 1 > /sys/class/gpio/gpio37/value"
L1_Off = "echo 0 > /sys/class/gpio/gpio37/value"
L2_On = "echo 1 > /sys/class/gpio/gpio38/value"
L2_Off = "echo 0 > /sys/class/gpio/gpio38/value"
L3_On = "echo 1 > /sys/class/gpio/gpio39/value"
L3_Off = "echo 0 > /sys/class/gpio/gpio39/value"
L4_On = "echo 1 > /sys/class/gpio/gpio101/value"
L4_Off = "echo 0 > /sys/class/gpio/gpio101/value"
L5_On = "echo 1 > /sys/class/gpio/gpio36/value"
L5_Off = "echo 0 > /sys/class/gpio/gpio36/value"
while True:
timeout = time.time() + 5 # 5 seconds
while True:
print ("2 prog", i)
os.system(L1_On)
time.sleep (z)
os.system(L1_Off)
#time.sleep (y)
os.system(L2_On)
time.sleep (z)
os.system(L2_Off)
#time.sleep (y)
os.system(L3_On)
time.sleep (z)
os.system(L3_Off)
#time.sleep (y)
os.system(L4_On)
time.sleep (z)
os.system(L4_Off)
#time.sleep (y)
os.system(L5_On)
time.sleep (z)
os.system(L5_Off)
#time.sleep(y)
if time.time() > timeout:
break
continue
#right follow
timeout = time.time() + 5 # 5 seconds
while True:
print ("2 prog", i)
os.system(L5_On)
time.sleep (z)
os.system(L5_Off)
#time.sleep (y)
os.system(L4_On)
time.sleep (z)
os.system(L4_Off)
#time.sleep (y)
os.system(L3_On)
time.sleep (z)
os.system(L3_Off)
#time.sleep (y)
os.system(L2_On)
time.sleep (z)
os.system(L2_Off)
#time.sleep (y)
os.system(L1_On)
time.sleep (z)
os.system(L1_Off)
#time.sleep(y)
if time.time() > timeout:
break
continue
答案 1 :(得分:1)
我知道这是一个简单的程序,但是我认为我可以帮助减少代码中的冗余:
import os
import time
#zmienne
dir = 5
i = 1
y = 0.2
z = 0.1
path = "/sys/class/gpio/"
lights = [37, 38, 39, 101, 36]
def initialize():
cmds = []
for light in lights:
cmds.append("echo %d > %sexport" % (light, path))
cmds.append("echo out > %sgpio%d/direction" % (path, light))
os.system(", ".join(cmds))
def flash(light, on=z):
os.system("echo 1 > %sgpio%d/value" % (path, lights[light]))
time.sleep(on)
os.system("echo 0 > %sgpio%d/value" % (path, lights[light]))
initialize()
while True:
#left follow
timeout = time.time() + dir
while time.time() < timeout:
print ("1 prog", i)
for l in range(len(lights)):
flash(l)
#time.sleep(y)
if time.time() > timeout:
break
#right follow
timeout = time.time() + dir
while time.time() < timeout:
print ("2 prog", i)
for l in range(len(lights) - 1, -1, -1):
flash(l)
#time.sleep(y)
if time.time() > timeout:
break
lights
映射(如果您更改图钉或顺序,也可以在一个位置进行调整)z
秒)while
循环确保代码永远运行dir = 5
秒)内运行指示灯,如果在运行过程中也停止运行希望这对您有所帮助!
答案 2 :(得分:0)
很棒-.Tested它的工作非常好。 两种代码 再次感谢您的时间。