logo

如何让openmv识别到色块后点亮相应颜色的led灯(点亮固定的时间),识别不到后又自己熄灭?

2022-06-28 19:56
Z
vze3

不用延时函数的话,延时函数会导致帧率变低,用定时器该怎么写代码呢?

2022-06-30 12:48
I
网红小智智 星瞳科技

@vze3 使用millis来获得时间,不用sleep函数。

2022-06-30 16:45
Z
vze3

@kidswong999 在 [如何让openmv识别到色块后点亮相应颜色的led灯(点亮固定的时间),识别不到后又自己熄灭?](/post/26320) 中说:

> @vze3 使用millis来获得时间,不用sleep函数。

那代码该怎么写呢?

2022-07-04 21:01
I
网红小智智 星瞳科技

@vze3 思路就是记录最新的找到色块的时间,然后判断当前时间是否超过1000ms。

import sensor, image, time, math
import pyb
led = pyb.LED(1)

thresholds = [(30, 100, 15, 127, 15, 127)]

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()

last = -1000
while(True):
    clock.tick()
    img = sensor.snapshot()
    blobs = img.find_blobs(thresholds, pixels_threshold=200, area_threshold=200, merge=True)
    print(blobs)
    if blobs:
        last = pyb.millis()
    if pyb.millis() - last > 1000:
        led.off()
    else:
        led.on()