[Diao Ye learns programming] MicroPython hands-on (15) - AB button 2 of the control board

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
1. Key switch
Mainly refers to light touch key switch, also known as light touch switch. The key switch is a kind of electronic switch, which belongs to the category of electronic components. It first appeared in Japan [called: sensitive switch]. When the pressure is on, the switch is disconnected, and its internal structure is realized by the force change of the metal shrapnel. The key switch is composed of an insert, a base, a shrapnel, a button, and a cover. Among them, a waterproof tact switch has a layer of polyimide film on the shrapnel.

Key switches have the advantages of small contact resistance, precise operating force error, and diversified specifications. They are widely used in electronic equipment and white goods, such as audio-visual products, digital products, remote controls, communication products, and household appliances. , security products, toys, computer products, fitness equipment, medical equipment, currency detector pens, laser pointer buttons, etc. Because of the environmental conditions of the key switch (the elastic force less than 2 times the pressure/environmental temperature and humidity conditions and electrical performance), large-scale equipment and high-load buttons are directly replaced by conductive rubber or dome switch metal shrapnel, such as medical equipment, TV remote control etc.

Regarding the pin position of the five-pin key switch: two pins form a group, and when the pressure is applied correctly to the switch body, the four pins are turned on, and the fifth pin is used for grounding.

The key switch is the fourth generation of switch products developed with the requirements of the development of electronic technology. The earliest volume is 12mm 12mm, 8mm 8mm, and now it is 6mm 6mm. There are three types of product structures: vertical, horizontal and horizontal with ground, and now there are two types of combination (3M, 4M, SM, 6M, SM) and potentiometer key switch combination, which meet the requirements of various domestic electronic products. There are three installation sizes: 6.5mm 4.5mm, 5.5mm 4mm and 6mm 4mm. There are 4.5mm*4.5mm small key switches and chip key switches abroad, and chip key switches are suitable for surface assembly.

Now there is a fifth-generation switch—the membrane switch, which has the same function as the key switch and is mainly used in electronic instruments and CNC machine tools, but has large resistance and poor hand feel. In order to overcome the phenomenon of poor hand feeling, there are also contact reeds instead of using silver layer as contact point in the membrane switch.

The key switch is divided into two categories: the key switch that uses the metal reed as the switch contact piece, the contact resistance is small, the hand feels good, and there is a "tick" crisp sound. A switch that uses conductive rubber as a contact path is customarily called a conductive rubber switch. The switch feels good, but the contact resistance is large, generally 100-300n. The structure of the key switch is to move down by the key, so that the contact reed or the conductive rubber block contacts the soldering piece to form a path.

The operating force of the key switch is related to the state of the reed. The starting force is proportional to the compression distance of the reed. When the reed is compressed to 5% to 70%, the operating force suddenly decreases, accompanied by a "tick" sound. Guiding turtle rubber switches generally have two structures. The operating force curve varies greatly with the geometry of the rubber block.

Relative humidity: <95%
Rated voltage: 12V
Rated current: 50mA
Temperature: -25~70℃

The main uses of key switches are color TVs, black and white TVs, audio equipment, video recorders, cameras, computers, game consoles, fax machines, walkie-talkies, batons, machine tool control devices, copying, printers, electronic instruments, meters and other household appliances.

insert image description here

2. There are two pressing buttons A and B on the upper edge of the control board

It is low level when the button is pressed, otherwise high level. The process of pressing the buttons on the control panel A and B is as follows. When pressed, the level changes from high to low, and the moment when the high level (1) changes to low level (0) is called the falling edge. When the button is released, the level changes from low to high, and the moment when the low level (0) changes to high level (1) is called a rising edge. We can obtain the current key state by obtaining the level change.
P5 and P11 are the IO pins of buttons A and B of the control board. In order to avoid conflicts, the P5 and P11 pins of the expansion board can only be used for digital level input, and the expansion board will reverse the input level.

insert image description here
8. Two-way switch light

#MicroPython hands-on (15) - the AB button of the control board
#Two-way switch light

#MicroPython动手做(15)——掌控板之AB按键
#双向开关灯

from mpython import *
import time

def on_button_a_down(_):
    global abc, variable
    time.sleep_ms(10)
    if button_a.value() == 1: return
    variable = variable ^ 1

def on_button_b_down(_):
    global abc, variable
    time.sleep_ms(10)
    if button_b.value() == 1: return
    variable = variable ^ 1

button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)

button_b.irq(trigger=Pin.IRQ_FALLING, handler=on_button_b_down)


variable = 0
while True:
    if variable == 1:
        rgb.fill( (0, 0, 0) )
        rgb.write()
        time.sleep_ms(1)
        oled.fill(0)
        oled.DispChar("关灯", 50, 25, 1)
    else:
        rgb.fill((int(255), int(102), int(0)))
        rgb.write()
        time.sleep_ms(1)
        oled.fill(0)
        oled.DispChar("开灯", 50, 25, 1)
    oled.show()

mPython X graphics programming

insert image description here

Note:
XOR operator found in mPython X "Math" module

insert image description here

In the program, both the A key and the B key can control the light on and off, that is, press the A key or the B key to turn on the light, and then press the A or B key to turn off the light. We can use the bitwise XOR operator "^" to achieve Effect, the bitwise XOR operation is to calculate the corresponding bits of the two operands according to the following rules:

0 ^ 0 = 0, 0 ^ 1 = 1, 1 ^ 0 = 1, 1 ^ 1 = 0

That is, if the values ​​of the corresponding bits are the same, the result is 0, and if they are different, the result is 1.

For example:

a = 60 (60 = 0011 1100)

b = 13 ( 13 = 0000 1101)

c = a ^ b( 49 = 0011 0001)

Whenever button A or button B is pressed, the value of the variable "variable" is ^1 and then assigned to the variable "variable"

1 and 0 in decimal are also 1 and 0 in binary

Initialize the variable deng to 0, and press the button to perform the XOR operation deng^1=1;

At this time, the variable deng is 1, and the button is pressed to perform the XOR operation deng^1=0;

By analogy, the button can control the value of the etc. variable.

a=60,b=13

insert image description here
tips

Bitwise operators are calculated with binary numbers, and can be converted to binary operations through decimal. The difference between decimal and binary is:

The bases are different, the former gets 1 when it reaches 10, and the latter gets 1 when it gets 2

The valid characters are different, the former has 10 valid characters: 0,1,2,3,4,5,5,6,7,8,9; the latter has 2 valid characters: 0,1

insert image description here
9. Simulate the "Turing Test"
Turing published a paper in 1950, "Computing Machines and Intelligence", which proposed a concept. When the tester is separated from the testee (a person and a machine), he asks the testee random questions through some device (such as a keyboard). After multiple tests, if more than 30% of the testers cannot determine whether the subject is a human or a machine, then the machine has passed the test and is considered to have human intelligence.

#MicroPython动手做(15)——掌控板之AB按键
#模拟“图灵测试”

from mpython import * #从mpython中调用所有的库文件
oled.DispChar('图灵测试', 40, 25) #在x=40,y=25的位置显示“图灵测试”
oled.show() #打开显示
while True: #循环执行
    if button_a.value() == 0 or button_b.value() == 0: #如果按钮a或者按钮b被按下
        oled.fill(0) #屏幕熄灭
        oled.DispChar('你会下国际象棋吗?', 0, 0)
        oled.DispChar('A.是的', 0, 16)
        oled.DispChar('B.是的', 0, 32)
        oled.show()
        break#跳出次循环

while True:
    if button_a.value() == 0 or button_b.value() == 0:
        oled.fill(0)
        oled.DispChar('你会下象棋吗?', 0, 0)
        oled.DispChar('A.是的', 0, 16)
        oled.DispChar('B.我不是说过了吗?', 0, 32)
        oled.show()
        break

while True:
    if button_a.value() == 0 or button_b.value() == 0:
        oled.fill(0)
        oled.DispChar('你会下象棋吗?', 0, 0)
        oled.DispChar('A.是的', 0, 16)
        oled.DispChar('B.你烦不烦,干嘛老提', 0, 32)
        oled.DispChar('同样的问题。', 0, 48)
        oled.show()
        break

while True:
    if button_a.value() == 0 or button_b.value() == 0:
        oled.fill(0)
        oled.DispChar('A-A-A,A-B-A', 0, 0)
        oled.DispChar('A-A-B,A-B-B', 0, 16)
        oled.DispChar('你认为哪个是人的回答?', 0, 32)
        oled.DispChar('哪一个是机器人的回答?', 0, 48)
        oled.show()
        break

Alan Mathison Turing (Alan Mathison Turing): British mathematician and logician, known as the father of computer science and the father of artificial intelligence. Turing has made many contributions to the development of artificial intelligence, and proposed a test method for judging whether a machine is intelligent, that is, the Turing test. So far, there are test competitions every year. In addition, the famous Turing machine model proposed by Turing laid the foundation for the way logic works in modern computers.

Turing published a paper in 1950, "Computing Machines and Intelligence", which proposed a vision. When the tester is separated from the testee (a person and a machine), he asks the testee random questions through some device (such as a keyboard). After multiple tests, if more than 30% of the testers cannot determine whether the subject is a human or a machine, then the machine has passed the test and is considered to have human intelligence.

insert image description here

insert image description here

10. AB key and rose curve
Rose curve, press A to add 1, press B to start

The basic meaning of the rose curve is: a curve in which an integer number of sinusoidal petals are evenly distributed around a certain center point in the plane. The mathematical formula is: ρ=a*sin(nθ), a is a fixed length, and n is an integer (not necessarily the number of petals).

Since this graph is not composed of straight lines, all drawing functions on the control panel cannot be used, only the pixel display function can be used. The command is as follows:

display.pixel(50,0,1)

In this command: display.pixel(x, y [,c ]) x, y are point coordinates (x, y). When c is not given, gets the color value of the specified pixel. If c is given, the specified pixel is set to the given color. Since the control board uses a black and white screen, it is enough to use 1 to represent the light.

#MicroPython动手做(15)——掌控板之AB按键
#玫瑰曲线,按A加1,按B开始,


from mpython import *
import math,time#引入库文件

def DrawRoseCurve(a,n):#定义函数,函数名称可以自己命名
    for t in range(0,360):#循环次数,由于是画一圈,所以是360;可以自行设定
        x = math.floor(math.cos(t)*a*math.sin(n*t))#计算x坐标的值,注意:这里需要取整
        y = math.floor(math.sin(t)*a*math.sin(n*t))#计算y坐标的值,并且取整
        display.pixel(x+64,y+32,1) #显示坐标像素点,为什么要+64、+32,哪是因为要把中心坐标(64,32)作为起点
        display.show() #执行

# 按键引脚初始化
  
display.fill(0)#清屏
display.show()#清屏执行
  
BTNA = Pin(0, mode=Pin.OPEN_DRAIN,pull=Pin.PULL_UP,value=1)
BTNB = Pin(2, mode=Pin.OPEN_DRAIN,pull=Pin.PULL_UP,value=1)
  
display.DispChar('玫瑰曲线', 38, 0)
display.DispChar('输入花瓣数量,按A加', 0, 17)
display.DispChar('按B开始', 0, 34)
display.show() #执行
  
n1 = 0
jishu = True
Start = False
while True:
    if jishu:
        if BTNA.value() == 0 and  BTNB.value() == 1 :
            display.fill(0)#清屏
            display.show()#清屏执行
            n1 = n1+1
            display.DispChar('数量:%d' % n1, 0, 0)
            display.show()#执行
            time.sleep_ms(400)
        if BTNA.value() == 1 and  BTNB.value() == 0 :
            jishu = False
            Start = True
        if Start:
            if n1%2 == 0:
                n1=n1/2
                display.fill(0)#清屏
                display.show()#清屏执行
                DrawRoseCurve(30,n1)#调用函数
Break

insert image description here

Guess you like

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