[Diao Ye learns programming] MicroPython hands-on (13) - RGB three-color lights on the control board 2

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

6. RGB blue breathing light

i = None

from mpython import *

import time
while True:
    for i in range(256):
        rgb.fill( (int(0), int(0), int(i)) )
        rgb.write()
        time.sleep_ms(1)
        time.sleep_ms(20)
    for i in range(255, -1, -1):
        rgb.fill( (int(0), int(0), int(i)) )
        rgb.write()
        time.sleep_ms(1)
        time.sleep_ms(20)

Note:
The for...in... loop statement (abbreviated as the for loop)
The for statement is a loop control statement in microPython, and the elements in any ordered sequence object can be traversed, such as strings, lists, tuples, etc.

Simple for loop code format:

for i in [1,2,3,4,5]:
    print(i*5)

Traverse the list [1,2,3,4,5], execute i*5 in a loop if satisfied;

Don't forget the English colon after the for statement;

Don't forget the indentation in front of the print() function.

12345

The above code can be understood as follows:
¹. A group of people are queuing up for business, that is, the list [1,2,3,4,5];
². When each of them is called (for i in), it will Take turns to enter an empty room i to do business;
³. After each number enters the room, they all say to the computer: "I want to do this business", that is, print(i), and then the computer provides printing services for each number, and will 1,2,3,4,5 are all printed on the screen.

The three main points of the for loop are: 1. Empty room; 2. A group of people waiting to do business; 3. Business process
for loop: empty room

The i in the above code is an empty room, its scientific name is an item, and you can treat it as a variable. Give the room a name, which is the "variable name".

for loop: a group of people queuing up to do business
Data types such as lists, dictionaries, and strings can often be understood as "a group of people queuing up to do business", but integers and floating-point numbers cannot. This process, the scientific name in Python is called traversal.

mPthon graphics programming

insert image description here
insert image description here

7. Onboard three-digit changing RGB rainbow lights

#MicroPython动手做(13)——掌控板之RGB三色灯
#板载三位变幻RGB彩虹灯

from mpython import *
import neopixel
import time

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

def make_rainbow(_neopixel, _num, _bright, _offset):
    _rgb = ((255,0,0), (255,127,0), (255,255,0), (0,255,0), (0,255,255), (0,0,255), (136,0,255), (255,0,0))
    for i in range(_num):
        t = 7 * i / _num
        t0 = int(t)
        r = round((_rgb[t0][0] + (t-t0)*(_rgb[t0+1][0]-_rgb[t0][0]))*_bright)>>8
        g = round((_rgb[t0][1] + (t-t0)*(_rgb[t0+1][1]-_rgb[t0][1]))*_bright)>>8
        b = round((_rgb[t0][2] + (t-t0)*(_rgb[t0+1][2]-_rgb[t0][2]))*_bright)>>8
        _neopixel[(i + _offset) % _num] = (r, g, b)


offset = 0
while True:
    make_rainbow(my_rgb, 3, 10, offset)
    offset = offset + 1
    my_rgb.write()
    time.sleep_ms(100)

mPythonX graphics programming

insert image description here

8. External eight-bit RGB rainbow light ring

#MicroPython动手做(13)——掌控板之RGB三色灯
#外接八位RGB彩虹灯环

from mpython import *
import neopixel
import time

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

def make_rainbow(_neopixel, _num, _bright, _offset):
    _rgb = ((255,0,0), (255,127,0), (255,255,0), (0,255,0), (0,255,255), (0,0,255), (136,0,255), (255,0,0))
    for i in range(_num):
        t = 7 * i / _num
        t0 = int(t)
        r = round((_rgb[t0][0] + (t-t0)*(_rgb[t0+1][0]-_rgb[t0][0]))*_bright)>>8
        g = round((_rgb[t0][1] + (t-t0)*(_rgb[t0+1][1]-_rgb[t0][1]))*_bright)>>8
        b = round((_rgb[t0][2] + (t-t0)*(_rgb[t0+1][2]-_rgb[t0][2]))*_bright)>>8
        _neopixel[(i + _offset) % _num] = (r, g, b)


offset = 0
while True:
    make_rainbow(my_rgb, 8, 6, offset)
    offset = offset + 1
    my_rgb.write()
    time.sleep_ms(100)

mPython X graphics programming

insert image description here

9. 24-bit mobile RGB rainbow light ring

#MicroPython动手做(13)——掌控板之RGB三色灯
#24位流动RGB彩虹灯环

from mpython import *
import neopixel
import time

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

def make_rainbow(_neopixel, _num, _bright, _offset):
    _rgb = ((255,0,0), (255,127,0), (255,255,0), (0,255,0), (0,255,255), (0,0,255), (136,0,255), (255,0,0))
    for i in range(_num):
        t = 7 * i / _num
        t0 = int(t)
        r = round((_rgb[t0][0] + (t-t0)*(_rgb[t0+1][0]-_rgb[t0][0]))*_bright)>>8
        g = round((_rgb[t0][1] + (t-t0)*(_rgb[t0+1][1]-_rgb[t0][1]))*_bright)>>8
        b = round((_rgb[t0][2] + (t-t0)*(_rgb[t0+1][2]-_rgb[t0][2]))*_bright)>>8
        _neopixel[(i + _offset) % _num] = (r, g, b)


offset = 0
while True:
    make_rainbow(my_rgb, 24, 10, offset)
    offset = offset + 1
    my_rgb.write()
    time.sleep_ms(100)

mPython X graphics programming

insert image description here

10. neopixel — WS2812 Light (Ring) Strip
NeoPixels, also known as WS2812 LED Ribbon (Ring), are strings of full-color LED lights connected together. You can set their red, green and blue values ​​between 0 and 255. The neopixel module can generate WS2812 control signals through precise timing control.

Build object
class NeoPixel(pin, n, bpp=3, timing=0, brightness=1.0)
pin: output pin, available pins see below
n Number of ED lights
bpp:
3: Default is 3-tuple RGB
4 : For LEDs with more than 3 colors, such as RGBW pixels or RGBY pixels, use 4-tuple RGBY or RGBY pixel
timing: default equal to 0, the rate is 400KHz; equal to 1, the rate is
800KHz , the default is 1.0

Note that
the pins available for NeoPixel are P5, P6, P7 (on-board RGB), P8, P9, P11, P13, P14, P15, P16, P19, P20 of the control board, – the experiment is connected to P8

method

NeoPixel.write(),

Write data into LED.

Example:

np[0] = (255, 255, 255) # 设置第一个LED像素为白色
np.write()
NeoPixel.fill(rgb_buf)

Fills all LED pixels.

rgb_buf :rgb

Color
example:

np.fill( (255, 255, 255) )

NeoPixel.brightness(brightness)

Brightness adjustment, range 0~1.0

The scene diagram of this experiment

insert image description here

Guess you like

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