Python>如何:从第二个“ while True”结束到第一个“ while True”?

时间:2019-12-30 16:19:03

标签: python

我为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

3 个答案:

答案 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
  • 提取的主要gpio路径(如果更改)(基于system / os)
  • 为代码进行迭代创建了lights映射(如果您更改图钉或顺序,也可以在一个位置进行调整)
  • 初始化方法处理所有灯光
  • Flash方法可在特定持续时间内打开和关闭单个LED(当前默认值为z秒)
  • 外部while循环确保代码永远运行
  • 内部循环在切换方向之前的特定时间(dir = 5秒)内运行指示灯,如果在运行过​​程中也停止运行

希望这对您有所帮助!

答案 2 :(得分:0)

很棒-.Tested它的工作非常好。 两种代码 再次感谢您的时间。