[Diao Ye learns programming] Arduino hands-on (54)---big button micro button module 3

insert image description here

The reference to 37 sensors and modules has been widely circulated on the Internet. In fact, there must be more than 37 sensor modules compatible with Arduino. In view of the fact that I have accumulated some sensor and actuator modules on hand, in accordance with the concept of true knowledge (must be hands-on), for the purpose of learning and communication, here I am going to try and do more experiments one by one. Whether it is successful or not, it will be recorded ——Small progress or unsolvable problems, I hope to be able to throw bricks and spark jade.

[Arduino] 168 kinds of sensor module series experiment (data code + simulation programming + graphics programming)
Experiment 54: button module electronic building blocks light touch switch big button micro button compatible with arduino raspberry pie

insert image description here
Program 4: Open source simulation programming (Linkboy V4.2)

insert image description here
Program 5: open source graphics programming (Mind+, learning while editing)

insert image description here

Program 6 Open source graphics programming (Mind+, learning while editing)

insert image description here
Experimental results
When the switch is not pressed, the onboard pin 13 outputs low level, and the onboard LED goes out; Jog switch function.

Knowledge point: Jog switch (Jog switch)
is relative to self-locking. The characteristic of the jog switch is that the load works after being pressed, and the switch springs back after being released (the switch can be automatically reset), and the load stops. In layman's terms, it is one of the motor control methods to press the switch to start, and release it to stop and move. Jogging, because there is no self-protection in this control loop, and there is no other automatic device connected in parallel. Only by pressing the start button of the control loop, the main loop is powered on, and when the start button is released, the main loop is out of power.

LED lights (LED lights)
LED lights are an electroluminescent semiconductor material chip, which is cured on the bracket with silver glue or white glue, and then connected to the chip and the circuit board with silver or gold wires, and sealed with epoxy resin around it. To protect the inner core wire, the shell is finally installed, so the LED lamp has good shock resistance. LED (Light Emitting Diode), light-emitting diode, is a solid-state semiconductor device that can convert electrical energy into visible light, and it can directly convert electricity into light. The heart of LED is the chip of a semiconductor, and one end of chip is attached on a support, and one end is negative pole, and the other end connects the positive pole of power supply, and whole chip is encapsulated by epoxy resin. Semiconductor wafer is made up of two parts, and a part is P-type semiconductor, and hole occupies an leading position in it, and the other end is N-type semiconductor, and here mainly is electron. But time these two kinds of semiconductors couple together, between them, just form a PN junction. When electric current acts on this chip by wire time, electron will be pushed to P district, and in P district, electron is recombined with hole, then will send energy with the form of photon, the principle of LED light that Here it is. The wavelength of light is the color of light, which is determined by the material forming the PN junction. LEDs can directly emit red, yellow, blue, green, blue, orange, purple, and white light.

Program 7: Use the key switch module to self-lock and control the
onboard LED (D13 pin of the development board). ), realize the bistable self-locking switch function. The button is judged to be released and pressed by the No. 2 pin. When the button is not pressed, the No. 2 pin always receives a low-level signal, and when the button is pressed, it receives a high-level signal. Inverts the state of the onboard LED whenever the button is pressed.

Experimental reference open source code (Arduino):

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  程序七:通过按键开关模块,自锁(双稳态)控制板载LED灯的亮灭
  使用:下拉电阻按键开关模块
  接线:按键开关接D2
*/

void setup() {
    
    
  pinMode(LED_BUILTIN , OUTPUT);    //使用板载LED作为控制对象.
}

void loop() {
    
    
  if (HIGH == digitalRead(2))  //如果按钮2按下
{
    
        
    digitalWrite(LED_BUILTIN , LOW == digitalRead(LED_BUILTIN)   ?  HIGH  : LOW );
    //则反转 LED的状态
    delay(600);
  }
}

Program 8 Open source simulation programming (Linkboy V4.2)
Note: Here you need to use the pull-up resistor key switch module

insert image description here

Program 9 Open source graphics programming (Mind+, learning while editing)
Note: Here you need to use the pull-up resistor key switch module and connect it to the A0 pin.

insert image description here

Knowledge point: Self-locking switch (Self-locking switch)
is divided into mechanical self-locking and circuit self-locking. When mechanical self-locking, even if the button is released after being pressed, it will not rebound, and the load will always work at this time. Press the button again and it pops out and the load stops working. The circuit is self-locking. After the button is pressed, the load works. After the button is released, although the button pops up, the load still works. Only when the button is pressed again, the load stops working. This experiment realizes the function of the electronic self-locking switch.

Program 10: Button debounce control LED on and off (development board D13 pin)
Experimental description: When you press the button, the result you may expect is the change from 1 to 0 (from the pull-up resistor to the button press). But the actual operation may not be the same as you imagined. If the jitter is not eliminated, the switch of the LED depends on whether the number of jitters is odd or even, and there is chance. If you use the delay to eliminate jitter, after adding the delay, nothing will happen within 200ms. For such a long time, no matter how many times the jitter is enough. Use the key switch to switch the LED on and off. That is: when the button is pressed, the LED is lit and stays on, and if the button is pressed again, the LED is turned off. There's also an interesting side effect: the LED blinks if the button is held down.

Experimental reference open source code (Arduino):

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  程序十:按键消抖,自锁控制LED亮灭(开发板D13脚)
  使用:上拉电阻按键开关模块
  接线:按键开关接D2
*/

int buttonPin = 2;  // 定义按键的针脚号为2
int ledPin = 13;  //定义LED输入针脚号为13号针脚
int ledValue = LOW;

void setup() {
    
    
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}

void loop() {
    
    
  if (digitalRead(buttonPin) == LOW)
  {
    
    
    ledValue = !ledValue;  //判断当前按键是否为按下状态
    digitalWrite(ledPin, ledValue);
    delay(200);  //延时消抖
  }
}

Program 11 Open source simulation programming (Linkboy V4.2)
Note: Here you need to use the pull-up resistor key switch module

insert image description here
Knowledge point: button debounce (removing jo ggle for push button)
The switch used by the usual button is a mechanical elastic switch. When the mechanical contact is disconnected and closed, due to the elastic effect of the mechanical contact, a button switch will not be closed when it is closed. It connects immediately and stably, and does not disconnect suddenly when it is disconnected. Therefore, there is a series of vibrations at the moment of closing and disconnecting. The measure to prevent this phenomenon is to eliminate the vibration of the key.

When the contact of the mechanical button is closed and disconnected, there will be jitter. In order to ensure that the system can correctly identify the switch of the button, the jitter of the button must be processed. The vibration of the button is imperceptible to human beings, but it can be completely sensed by the single-chip microcomputer, and it is a very "long" process, because the processing speed of the single-chip microcomputer is at the "microsecond" level, while The key shake time is at least "milliseconds" level. If the single-chip microcomputer detects the on-off state of the button during the contact jitter, it may lead to a judgment error, that is, one press or release of the button is mistakenly considered as multiple operations, thereby causing mishandling. Therefore, in order to ensure that the single-chip microcomputer only responds once to a button action, it is necessary to consider how to eliminate the influence of button shaking.

insert image description here

The length of the shaking time is determined by the mechanical characteristics of the key, generally 5ms ~ 10ms. This is a very important time parameter that is used on many occasions. The length of the button's stable closing time is determined by the operator's button action, generally ranging from a few tenths of a second to several seconds. Key chatter can cause a key to be misread multiple times. In order to ensure that the CPU processes only one key closure once, key jitter must be removed. The state of the key is read when the key is closed and stable, and it must be judged that the key is released and stable before processing.

The commonly used software method is to debounce, that is, to execute a delay program after detecting that the key is closed, with a delay of 5ms to 10ms, so that the front edge jitter disappears and then detect the state of the key again. If the closed state level is still maintained, it is confirmed that there is a real key. key is pressed. When it is detected that the key is released, a delay of 5 ms to 10 ms is also required, and the processing program of the key can only be transferred after the jitter of the trailing edge disappears. The basic principle of software debounce is: when a button is pressed, it is not immediately determined that the button has been pressed, but after a delay program of about 10ms (the specific time should be adjusted according to the button used) , and then confirm whether the key level is still closed. If it is still maintained, confirm that the key is really pressed.

insert image description here

Guess you like

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