3700 views|32 replies

3386

Posts

0

Resources
The OP
 

MicroPython Hands-on (15) - AB Buttons on the Control Panel [Copy link]

 
 

1. Push button switch
Mainly refers to the light touch push button switch, also known as the light touch switch. The push button switch is an electronic switch belonging to the category of electronic components. It first appeared in Japan [called: sensitive switch]. When in use, the switch function is closed and connected by applying pressure in the direction of the switch operation under the condition of satisfying the operating force. When the pressure is removed, the switch is disconnected. Its internal structure relies on the change of force on the metal spring to achieve on and off. The push button switch is composed of an insert, a base, a spring, a button, and a cover. Among them, the waterproof light touch switch has a layer of polyimide film on the spring.

The key switch has the advantages of small contact resistance, precise operating force error, and diversified specifications. It is widely used in electronic equipment and white appliances, such as audio-visual products, digital products, remote controls, communication products, household appliances, security products, toys, computer products, fitness equipment, medical equipment, banknote detectors, laser pen keys, etc. Because the key switch is sensitive to environmental conditions (the applied pressure is less than 2 times the elastic force/environmental temperature and humidity conditions and electrical performance), large equipment and high-load buttons are directly replaced by conductive rubber or pot switch metal shrapnel, such as medical equipment, TV remote controls, etc.

Regarding the pin position of the five-pin push button switch: two pins are in a group. When the switch body is properly pressurized, the four pins are connected, and the fifth pin serves as a ground.

The push button switch is the fourth generation switch product developed in line with the requirements of the development of electronic technology. The earliest size was 12mm*12mm and 8mm*8mm, and now it is 6mm*6mm. The product structure has three types: vertical, horizontal and horizontal with ground terminal. Now there are two types: combined (3M, 4M, SM, 6M, SM) and potentiometer push button switch combination, which meet the requirements of various domestic electronic products. The installation size is 6.5mm*4.5mm, 5.5mm*4mm and 6mm*4mm. There are already 4.5mm*4.5mm small push button switches and chip push button switches abroad. The chip push button switch is suitable for surface assembly.

Now there is the fifth generation switch - 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 feel. In order to overcome the poor feel, there are also contact springs instead of silver layers as contact points in the membrane switch.

There are two types of key switches: the key switches that use metal reeds as switch contacts have low contact resistance, good feel, and a crisp "ticking" sound. The switches that use conductive rubber as contact paths are usually called conductive rubber switches. The switch feels good, but the contact resistance is large, generally between 100 and 300 n. The key switch structure relies on the key to move downward, so that the contact reed or conductive rubber block contacts the solder piece to form a path.

The operating force of the key switch is related to the state of the reed. The initial force is proportional to the compression distance of the reed. When the reed is compressed to 5%-70%, the operating force suddenly decreases, accompanied by a "ticking" sound. There are generally two structures of guide rubber switches. The operating force curve varies greatly depending on the geometric shape of the rubber block.

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

The main uses of push button switches include color TVs, black and white TVs, audio equipment, video recorders, cameras, computers, game consoles, fax machines, intercoms, batons, machine tool control devices, copiers, printers, electronic instruments, meters and other household appliances.


This content is originally created by eagler8 , a user of EEWORLD forum. If you want to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source

Latest reply

Great! It looks very interesting. I will help you compile a summary later.  Details Published on 2020-4-20 06:38
 
 

3386

Posts

0

Resources
2
 
This post was last edited by eagle8 on 2020-4-19 18:52

2. There are two push-buttons A and B on the upper edge of the control panel.

When the button is pressed, it is at a low level, otherwise it is at a high level. The process of pressing the A and B buttons on the control board is as follows. When pressed, the level changes from high to low. The moment when the high level (1) changes to the low level (0) is called the falling edge. When the button is released, the level changes from low to high. The moment when the low level (0) changes to the high level (1) is called the rising edge. We can obtain the current button status by obtaining the level change.

P5 and P11 are the IO pins of buttons A and B on the control board. 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 flip the input level.

 
 
 

3386

Posts

0

Resources
3
 

3. Use the A and B buttons to control the OLED screen to display A or B.

#MicroPython动手做(13)——掌控板之RGB三色灯
#通过A、B按键,控制OLED屏幕显示A或B

from mpython import *
import framebuf
import font.dvsm_16

def display_font(_font, _str, _x, _y, _wrap, _z=0):
    _start = _x
    for _c in _str:
        _d = _font.get_ch(_c)
        if _wrap and _x > 128 - _d[2]: _x = _start; _y += _d[1]
        if _c == '1' and _z > 0: oled.fill_rect(_x, _y, _d[2], _d[1], 0)
        oled.blit(framebuf.FrameBuffer(bytearray(_d[0]), _d[2], _d[1],
        framebuf.MONO_HLSB), (_x+int(_d[2]/_z)) if _c=='1' and _z>0 else _x, _y)
        _x += _d[2]


while True:
    if button_a.value() == 0:
        oled.fill(0)
        display_font(font.dvsm_16, "A", 62, 26, False)
        oled.show()
    if button_b.value() == 0:
        oled.fill(0)
        display_font(font.dvsm_16, "B", 62, 26, False)
        oled.show()

 
 
 

3386

Posts

0

Resources
4
 

mPython X Graphical Programming

 
 
 

1w

Posts

204

Resources
5
 
Great! It looks very interesting. I will help you compile a summary later.
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle

Comments

Thank you for the encouragement  Details Published on 2020-4-20 08:58
 
 
 

3386

Posts

0

Resources
6
 
okhxyyo posted on 2020-4-20 06:38 Like!! Looks very interesting, I will help you compile a summary later.

Thank you for the encouragement

 
 
 

3386

Posts

0

Resources
7
 

4. Buttons A and B light up the RGB lights

#MicroPython动手做(15)——掌控板之AB按键
#A、B按键点亮DEB灯

from mpython import *

import time

def on_button_a_down(_):
    time.sleep_ms(10)
    if button_a.value() == 1: return
    rgb.fill((int(0), int(153), int(0)))
    rgb.write()
    time.sleep_ms(1)
    time.sleep(0.5)
    rgb.fill( (0, 0, 0) )
    rgb.write()
    time.sleep_ms(1)

def on_button_b_down(_):
    time.sleep_ms(10)
    if button_b.value() == 1: return
    rgb.fill((int(155), int(0), int(0)))
    rgb.write()
    time.sleep_ms(1)
    time.sleep(0.5)
    rgb.fill( (0, 0, 0) )
    rgb.write()
    time.sleep_ms(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)

 
 
 

3386

Posts

0

Resources
8
 

mPython Graphics Programming

 
 
 

3386

Posts

0

Resources
9
 

5. Press the A and B keys to play different notes.

Press the A and B pins of the control panel at the same time to display A and B. Pressing only the A (all black) or B (all white) key will clear the

#MicroPython动手做(15)——掌控板之AB按键
#按下A、B键播放播放不同音符

from mpython import *
import music

def callback_button_ab_pressed():
  music.pitch(262, 250);
  oled.fill(0)
  oled.DispChar("AB", 55, 26)
  oled.show()


def callback_button_a_pressed():
  music.pitch(165, 250);
  oled.fill(0)
  oled.show()


def callback_button_b_pressed():
  music.pitch(523, 250);
  oled.fill(1)
  oled.show()


def on_button_ab_pressed():
  while True:
    if button_a.value() == 0 and button_b.value() == 0:
      yield callback_button_ab_pressed()
    else:
      yield

def on_button_a_pressed():
  while True:
    if button_a.value() == 0:
      yield callback_button_a_pressed()
    else:
      yield

def on_button_b_pressed():
  while True:
    if button_b.value() == 0:
      yield callback_button_b_pressed()
    else:
      yield

func_button_ab_pressed = on_button_ab_pressed()
func_button_a_pressed = on_button_a_pressed()
func_button_b_pressed = on_button_b_pressed()
while True:
  next(func_button_ab_pressed)
  next(func_button_a_pressed)
  next(func_button_b_pressed)

 
 
 

3386

Posts

0

Resources
10
 

Mind+ Graphics Programming

 
 
 

3386

Posts

0

Resources
11
 

6. Get the button status, press A to turn on the light, press B to turn off the light

#MicroPython动手做(15)——掌控板之AB按键
#按A键开灯,按B键关灯

from mpython import *

while True:
    # 按键A按下为低电平
    if button_a.value() == 0 :    
        # 按键延时
        sleep_ms(20)
        # 再次检测是否按下
        if button_a.value()==0:
            # 设置为三色
            rgb[0] = (130,0,0)  
            rgb[1] = (0,100,0)
            rgb[2] = (0,0,255)
        rgb.write()
    # 按键B按下为低电平
    if button_b.value() == 0 :    
        # 按键延时
        sleep_ms(20)
        # 再次检测是否按下
        if button_b.value()==0:
            # 关灯
            rgb[0] = (0, 0, 0)    
            rgb[1] = (0, 0, 0)
            rgb[2] = (0, 0, 0)
            rgb.write()


Before use, import the mpython module:
from mpython import *

Button A and Button B are pressed:
button_a.value() == 0 #Button A is pressed
button_b.value() == 0 #Button B is pressed

Note:
button_a is the object name of button A, and button B is the object name of button_b. They are derived classes of machine.Pin and inherit the methods of Pin, so the value function can be used to read the pin value. It returns 1 for a high-level signal and 0 for a low-level signal. Therefore, when the button is not pressed, value==1, and when it is pressed, value==0.

 
 
 

3386

Posts

0

Resources
12
 

7. Key interruption
Press key A to turn on the onboard light and buzzer, and press key B to turn off the onboard light and buzzer

#MicroPython动手做(15)——掌控板之AB按键
#按键中断

from mpython import *     #导入mpython模块
import music              #导入music模块

def ledon(_):             #先定义中断处理函数:开灯和蜂鸣器
    rgb.fill((128,0,0))   #打开板载灯,全部设置为红色,半亮度
    rgb.write()           #将颜色输出到灯
    music.pitch(1000)     #打开蜂鸣器:1000赫兹

def ledoff(_):            #先定义中断处理函数:关灯和蜂鸣器
    rgb.fill((0,0,0))     #关闭全部板载灯
    rgb.write()           #将颜色输出到灯
    music.pitch(0)        #关闭蜂鸣器

button_a.irq(trigger=Pin.IRQ_FALLING, handler=ledon)     #设置按键 A 中断,下降沿触发,开灯和蜂鸣器

button_b.irq(trigger=Pin.IRQ_FALLING, handler=ledoff)    #设置按键 B 中断,下降沿触发,关灯和蜂鸣器

while True:                            #在没有中断时,程序执行在循环内
    pass

Note:
By default, the above program will wait in the loop without executing any instructions. When the a or b key is detected, the corresponding function will be called back.

button_a.irq(trigger=Pin.IRQ_FALLING, handler=ledon) is the function corresponding to the interrupt handler called. trigger configures the event that can trigger the interrupt, and the possible values are: Pin.IRQ_FALLING falling edge interrupt; Pin.IRQ_RISING rising edge interrupt; Pin.IRQ_LOW_LEVEL low level interrupt; Pin.IRQ_HIGH_LEVEL high level interrupt. handler is an optional function that is called when the interrupt is triggered and returns a callback object. For detailed usage, please refer to button_[a,b].irq.

Notice

When defining an interrupt handler function, the function must contain any one parameter, otherwise it cannot be used. The parameter in ledon() and ledoff() functions is _.

 
 
 

3386

Posts

0

Resources
13
 

8. Two-way switch light

#MicroPython Hands-on (15) - 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()

 
 
 

3386

Posts

0

Resources
14
 

mPython X Graphical Programming

 
 
 

3386

Posts

0

Resources
15
 

Note:
The XOR operator is found in the mPython X "math" module

In the program, both the A and B keys can control the light on and off. That is, press the A or B key to turn on the light, and press the A or B key again to turn off the light. We can use the bitwise XOR operator "^" to achieve this effect. The bitwise XOR operation calculates 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)

 
 
 

3386

Posts

0

Resources
16
 

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

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

Initialize the deng variable to 0, and when the button is pressed, 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;

And so on, you can realize the value of variables such as key control.

a=60,b=13

Tips

Bitwise operators are calculated based on binary numbers. They can be converted from decimal to binary. The difference between decimal and binary is:

The bases are different. The former increases by 10 when it reaches 10, and the latter increases by 1 when it reaches 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

 
 
 

3386

Posts

0

Resources
17
 

 
 
 

3386

Posts

0

Resources
18
 

9. Simulate the "Turing Test"
Turing published a paper in 1950, "Computing Machinery and Intelligence", which proposed a concept. The tester and the testee (a person and a machine) are separated, and the testee is asked random questions through some devices (such as a keyboard). After multiple tests, if more than 30% of the testers cannot determine whether the testee is a person 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

 
 
 

3386

Posts

0

Resources
19
 

Alan Mathison Turing: British mathematician and logician, known as the father of computer science and artificial intelligence. Turing has made many contributions to the development of artificial intelligence. He proposed a test method for determining whether a machine has intelligence, namely the Turing test. To this day, there are annual test competitions. In addition, the famous Turing machine model proposed by Turing laid the foundation for the logical working method of modern computers.

In 1950, Turing published a paper, "Computing Machinery and Intelligence", which proposed a concept. The tester and the tested person (a person and a machine) are separated, and the tested person is asked random questions through some devices (such as a keyboard). After multiple tests, if more than 30% of the testers cannot determine whether the tested person is a person or a machine, then the machine has passed the test and is considered to have human intelligence.

 
 
 

3386

Posts

0

Resources
20
 

 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list