logo

关于openmv跳出循环的问题?

2022-07-26 19:08
K
hxcb
from machine import I2C
from vl53l1x import VL53L1X
from pyb import UART
import time,pyb,sensor,json

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)

uart = UART(1, 9600)
i2c = I2C(2)
distance = VL53L1X(i2c)
led1 = pyb.LED(1)
led2 = pyb.LED(2)
led3 = pyb.LED(3)
clock = time.clock()


while True:
    time.sleep_ms(50)
    dis=distance.read()
    if 610<dis<640:
        data="0"
        data_out = json.dumps(set(data))
        uart.write(data_out +'\n')
        break
    clock.tick()
    img = sensor.snapshot()
    for r in img.find_rects(threshold = 23000):
        img.draw_rectangle(r.rect(), color = (255, 0, 0))
        for p in r.corners(): img.draw_circle(p[0], p[1], 5, color = (0, 255, 0))
        if r.w()>r.h():
            if 470<dis<490:
                led2.on()
                data="1"
                data_out = json.dumps(set(data))
                uart.write(data_out +'\n')
                print('you send:',data_out)
                led2.off()
                break
            elif 320<dis<355:
                led3.on()
                data="2"
                data_out = json.dumps(set(data))
                uart.write(data_out +'\n')
                print('you send:',data_out)
                led3.off()
                break
        elif r.w()<r.h():
            if 470<dis<490:
                led2.on()
                data="3"
                data_out = json.dumps(set(data))
                uart.write(data_out +'\n')
                print('you send:',data_out)
                led2.off()
                break
            elif 320<dis<355:
                led3.on()
                data="4"
                data_out = json.dumps(set(data))
                uart.write(data_out +'\n')
                print('you send:',data_out)
                led3.off()
                break

请问一下,为什么只有0的时候可以跳出,1234都跳不出来

2022-07-26 20:05
5
pimy

可以注意一下缩进

K
hxcb

@pimy 哈?缩进没啥问题把这 它可以运行的 就是跳不出去

2022-07-27 16:09
I
网红小智智 星瞳科技

第27行的break跳出的是20行的while;

第41行,第49行,第58行,第66行,跳出的是第30行的for

2022-07-27 16:15
K
hxcb

@kidswong999 请问一下老师,后边的如何才能跳出while呀

2022-07-27 16:22
I
网红小智智 星瞳科技

@hxcb 如果要直接跳出最外面的while,直接用goto。

2022-07-27 16:32
K
hxcb

@kidswong999 ok谢谢老师 我试试

2022-07-27 18:14
K
hxcb

@kidswong999 老师还请问一下你,就我上边的代码,就您说的跳出去for的语句,如果我想跳到while那么break应该改成goto while么?还是?

2022-07-27 18:26
I
网红小智智 星瞳科技

我搞错了,python没有goto

K
hxcb

@kidswong999 啊哈 那老师您说 我该怎么写才能回到主循环函数嘞

I
网红小智智 星瞳科技

https://www.zhihu.com/question/37076998

参考这个