[Diao Ye learns programming] MicroPython hands-on (13) - RGB three-color light of the control board 3

Knowledge points: What is a control board?
The control board is an open source intelligent hardware that popularizes STEAM maker education, artificial intelligence education, and robot programming education. It integrates ESP-32 high-performance dual-core chip, supports WiFi and Bluetooth dual-mode communication, and can be used as an IoT node to realize IoT applications. At the same time, the control board integrates OLED display, RGB lights, accelerometers, microphones, light sensors, buzzers, key switches, touch switches, and gold finger external expansion interfaces. It supports graphics and MicroPython code programming, which can realize intelligent robots, Smart control applications such as Maker Smart Works.

insert image description here
insert image description here
insert image description here

Control board hardware features:
ESP-32 main control
Processor: Tensilica LX6 dual-core processor (one core handles high-speed connection; one core independent application development)
main frequency: up to 240MHz clock frequency
SRAM: 520KB
Flash: 8MB
Wi-Fi standard: FCC/CE/TELEC/KCC
Wi-Fi protocol: 802.11 b/g/n/d/e/i/k/r (802.11n, speed up to 150 Mbps), A-MPDU and A-MSDU aggregation, support 0.4us Protection interval
Frequency range: 2.4~2.5 GHz
Bluetooth protocol: Compliant with Bluetooth v4.2 BR/EDR and BLE standards
Bluetooth audio: CVSD and SBC audio Low power consumption: 10uA
Power supply: Micro USB Power supply
Operating voltage: 3.3V
Maximum operating current: 200mA
maximum load current: 1000mA
Onboard
three-axis accelerometer MSA300, measuring range: ±2/4/8/16G
geomagnetic sensor MMC5983MA, measuring range: ±8 Gauss; accuracy 0.4mGz, electronic compass error ±0.5°
Light sensor
Microphone
3 full-color ws2812 lamp beads
1.3-inch OLED display, support 16*16 character display, resolution 128x64
passive buzzer
, support 2 physical buttons (A/B), 6 touch buttons,
support 1 alligator clip interface, Easy access to various resistive sensors
Expansion interface
20-channel digital I/O, (including 12-channel PWM, 6-channel touch input)
5-channel 12bit analog input ADC, P0~P4
1-channel external input alligator clip interface: EXT/GND
supports I2C, UART, SPI communication protocol

insert image description here
insert image description here

11. Program 2 of 24-bit bouncing RGB rainbow light ring

#MicroPython动手做(13)——掌控板之RGB三色灯
#24位弹跳RGB彩虹灯环程序之二

from mpython import *
import neopixel
np = neopixel.NeoPixel(Pin(Pin.P8), n=24,bpp=3,timing=1)


def wheel(pos):
    # 通过改变在0和255之间的每个颜色参数产生彩虹色光谱
    # Input a value 0 to 255 to get a color value.
    # The colours are a transition r - g - b - back to r.
    if pos < 0 or pos > 255:
        r = g = b = 0
    elif pos < 85:
        r = int(pos * 3)
        g = int(255 - pos*3)
        b = 0
    elif pos < 170:
        pos -= 85
        r = int(255 - pos*3)
        g = 0
        b = int(pos*3)
    else:
        pos -= 170
        r = 0
        g = int(pos*3)
        b = int(255 - pos*3)
    return (r, g, b)

def cycle(np,r,g,b,wait=20):
    # 循环效果,有一个像素在所有灯带位置上运行,而其他像素关闭。
    for i in range(4 * np.n):
        for j in range(np.n):
            np[j] = (0, 0, 0)
        np[i % np.n] = (r, g, b)
        np.write()
        sleep_ms(wait)


def bounce(np,r,g,b,wait=20):
    # 弹跳效果,等待时间决定了弹跳效果的速度
    n=np.n
    for i in range(4 * n):
        for j in range(n):
            np[j] = (r, g, b)
        if (i // n) % 2 == 0:
            np[i % n] = (0, 0, 0)
        else:
            np[n - 1 - (i % n)] = (0, 0, 0)
        np.write()
        sleep_ms(wait)


def rainbow_cycle(np,wait_us):
    # 彩虹效果
    n=np.n
    for j in range(255):
        for i in range(n):
            pixel_index = (i * 256 // n) + j
            np = wheel(pixel_index & 255)
        np.write()
        sleep_us(wait_us)

while True:
    cycle(np,50,50,50,wait=20)
    bounce(np,50,0,0,wait=20)
    rainbow_cycle(np,20)

Onboard RGB

RGB LED control commands are used to control the three RGB ws2812 lamp beads on the control board. The rgb object is a derivative class of neopixel and inherits the method of neopixel

rgb[n] = (r, g, b)

Description: Set the color of the corresponding lamp beads, n is the number of onboard RGB lights, the first light is 0, r, g, b are the color brightness values, and the range value is 0~255

rgb.write()

Description: Write data into RGB lamp beads

rgb.fill( (r, g, b) )

Description: Fill in the color and brightness of all lamp beads, r, g, b are color brightness values, and the range value is 0~255

external RGB

External RGB lights with light ring control commands

class NeoPixel(pin, n, bpp=3, timing=0)

Description: build object

insert image description here

parameter:

pin - output pin

n - the number of LED lights

bpp - bpp=3, the default is 3-tuple RGB; bpp=4, for LEDs with more than 3 colors, such as RGBW pixels or RGBY pixels, use 4-tuples RGBY or RGBY pixels

timing - the default is equal to 0, which is 400KHz rate; equal to 1, which is 800KHz rate

NeoPixel.write()

Description: Write data into RGB lamp beads

NeoPixel.fill( (r, g, b) )

Description: Fill in the color and brightness of all lamp beads, r, g, b are color brightness values, and the range value is 0~255

12. Recommended RGB color reference table (very detailed)
https://tool.oschina.net/commons?type=3

insert image description here

13. Light up the brighter water lights in turn

#MicroPythonhands-on (13)——RGB three-color light of the control board
#Light up the brighter running water lights one by one

#MicroPython动手做(13)——掌控板之RGB三色灯
#依次点亮更亮的流水灯

from mpython import *

import time
while True:
    rgb.fill((int(102), int(102), int(102)))
    rgb.write()
    time.sleep_ms(1)
    time.sleep_ms(500)
    rgb.fill( (0, 0, 0) )
    rgb.write()
    time.sleep_ms(1)
    for k in range(3):
        rgb[k] = (int(((k + 1) * 66)), int(0), int(0))
        rgb.write()
        time.sleep_ms(1)
        time.sleep_ms(500)
    for k in range(3):
        rgb[k] = (int(0), int(((k + 1) * 45)), int(0))
        rgb.write()
        time.sleep_ms(1)
        time.sleep_ms(500)
    for k in range(3):
        rgb[k] = (int(0), int(0), int(((k + 1) * 85)))
        rgb.write()
        time.sleep_ms(1)
        time.sleep_ms(500)

mPython graphics programming

insert image description here

insert image description here
insert image description here

14. P24 light ring 4-color running water clock pendulum light

#MicroPython hands-on (13) - RGB three-color light of the control board
#P24 light ring 4-color running water pendulum light

#MicroPython动手做(13)——掌控板之RGB三色灯
#P24灯环4色流水钟摆灯

from mpython import *
import neopixel
import time

my_rgb = neopixel.NeoPixel(Pin(Pin.P8), n=24, bpp=3, timing=1)


while True:
    for i in range(23, -1, -1):
        my_rgb = (30, 30, 30)
        my_rgb.write()
        time.sleep_ms(30)
    my_rgb.fill( (0, 0, 0) )
    my_rgb.write()
    for i in range(24):
        my_rgb = (0, 30, 0)
        my_rgb.write()
        time.sleep_ms(30)
    my_rgb.fill( (0, 0, 0) )
    my_rgb.write()
    for i in range(23, -1, -1):
        my_rgb = (50, 0, 0)
        my_rgb.write()
        time.sleep_ms(30)
    my_rgb.fill( (0, 0, 0) )
    my_rgb.write()
    for i in range(24):
        my_rgb = (0, 0, 180)
        my_rgb.write()
        time.sleep_ms(30)
    my_rgb.fill( (0, 0, 0) )
    my_rgb.write()

mPython X graphics programming

insert image description here

15. Sound-activated RGB light ring

The volume of the music sound is detected by the sound sensor and converted into the number of lights.

#MicroPython动手做(13)——掌控板之RGB三色灯
#声控RGB灯环

from mpython import *
import neopixel

my_rgb = neopixel.NeoPixel(Pin(Pin.P8), n=24, bpp=3, timing=1)

def upRange(start, stop, step):
    while start <= stop:
        yield start
        start += abs(step)

def downRange(start, stop, step):
    while start >= stop:
        yield start
        start -= abs(step)


while True:
    oled.fill(0)
    oled.DispChar("声音大小", 0, 0, 1)
    oled.DispChar((str(sound.read())), 0, 16, 1)
    oled.show()
    sheng = sound.read() // 140
    if sheng == 0:
        my_rgb.fill( (0, 0, 0) )
        my_rgb.write()
    else:
        for i in (0 <= int(sheng)) and upRange(0, int(sheng), 1) or downRange(0, int(sheng), 1):
            my_rgb = (0, 50, 0)
            my_rgb.write()

mPython X graphics programming

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/weixin_41659040/article/details/131998714