import sensor, image, time,pyb
from pyb import Pin, Timer
thresholds = [(24, 71, 2, 63, 5, 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()
i=0
def beer():
p = Pin('P7') # P7 has TIM4, CH1
tim = Timer(4, freq=1000)
ch = tim.channel(1, Timer.PWM, pin=p)
ch.pulse_width_percent(50)
def timee():
tim=Timer(2,freq=1)
tim.counter()
def tick(timer):
p = Pin('P7') # P7 has TIM4, CH1
tim = Timer(4, freq=1000)
ch = tim.channel(1, Timer.PWM, pin=p)
ch.pulse_width_percent(50)
tim = Timer(2, freq=1) # 使用定时器2创建定时器对象-以1Hz触发
while(True):
clock.tick()
flag = [1,1]
img = sensor.snapshot().lens_corr(1.8)
blob1 = img.find_blobs(thresholds, roi=(123,51,50,70),pixels_threshold=330, area_threshold=500, merge=False)
img.draw_rectangle((123,51,50,70), color=(0,255,0))
blob2 = img.find_blobs(thresholds, roi=(211,51,50,70),pixels_threshold=330, area_threshold=500, merge=False)
img.draw_rectangle((211,51,50,70), color=(0,255,0))
if blob1:
flag[0]=0
if blob2:
flag[1]=0
if(flag[0]==0 and flag[1]==0 ):
print('两个')
beer()
if(flag[0]==1 and flag[1]==1):
print('无')
在死循环里判断一下时间。
import sensor, image, time
import pyb
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()
flag = True
while(True):
if flag:
sec = pyb.millis()//1000 % 6
if sec == 0:
print("响")
else:
print("不响")
clock.tick()
img = sensor.snapshot()