例程讲解-02-led_control LED控制

# LED 控制例子
#
# 这个例子展示了如何控制你的OpenMV的内置的LED。使用你的手机才能看到红外IR LED。

import time
from pyb import LED

red_led   = LED(1)
green_led = LED(2)
blue_led  = LED(3)
ir_led    = LED(4)

def led_control(x):
    if   (x&1)==0: red_led.off()
    elif (x&1)==1: red_led.on()
    if   (x&2)==0: green_led.off()
    elif (x&2)==2: green_led.on()
    if   (x&4)==0: blue_led.off()
    elif (x&4)==4: blue_led.on()
    if   (x&8)==0: ir_led.off()
    elif (x&8)==8: ir_led.on()

while(True):
    for i in range(16):
        led_control(i)
        time.sleep_ms(500)

pyb模块内的pyb.LED()函数文档: 星瞳科技OpenMV官方中文文档函数讲解:

在 OpenMV RT 上不能用pyb模块,只能使用以下machine模块:

# LED 控制例子
#
# 这个例子展示了如何控制你的OpenMV的内置的LED。
import time
from machine import LED

red_led = LED("LED_RED")
green_led = LED("LED_GREEN")
blue_led = LED("LED_BLUE")


def led_control(x):
    if (x & 1) == 0:
        red_led.off()
    elif (x & 1) == 1:
        red_led.on()
    if (x & 2) == 0:
        green_led.off()
    elif (x & 2) == 2:
        green_led.on()
    if (x & 4) == 0:
        blue_led.off()
    elif (x & 4) == 4:
        blue_led.on()


while True:
    for i in range(8):
        led_control(i)
        time.sleep_ms(500)

在 OpenMV RT 上使用PWM控制LED灯:

# LED 控制例子
#
# 这个例子展示了如何控制你的OpenMV的内置的LED。

import time
from machine import PWM

r = PWM("LED_RED", freq=200, duty_u16=65535)
b = PWM("LED_BLUE", freq=200, duty_u16=65535)
g = PWM("LED_GREEN", freq=200, duty_u16=65535)


while True:
    for i in range(0, 65536, 256):
        r.duty_u16(65535 - i)
        time.sleep_ms(10)
    r.duty_u16(65535)

    for i in range(0, 65536, 256):
        g.duty_u16(65535 - i)
        time.sleep_ms(10)
    g.duty_u16(65535)

    for i in range(0, 65536, 256):
        b.duty_u16(65535 - i)
        time.sleep_ms(10)
    b.duty_u16(65535)

results matching ""

    No results matching ""