[Diao Ye learns programming] Arduino hands-on (131)---marquee matrix keyboard module

The reference to 37 sensors and actuators 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, according to the concept of practicing true knowledge (must be done), for the purpose of learning and communication, I am going to try a series of experiments one by one, regardless of success (the program goes through) or not, They will be recorded - small progress or unsolvable problems, hoping to inspire others.

[Arduino] 168 kinds of sensor module series experiments (data code + simulation programming + graphics programming)
Experiment 131: YL-004 old style 20-button independent keyboard marquee matrix keyboard module

insert image description here
4X4 matrix keyboard

It is a matrix-like keyboard group used in the peripheral devices of the single-chip microcomputer. The keyboard with matrix structure is obviously more complicated than the direct method, and the identification is also more complicated. The column lines are connected to the positive power supply through resistors, and the I/O ports of the single-chip microcomputer connected to the row lines are used as output terminals, and the I/O ports connected to the column lines are used as output terminals. The /O port is used as an input. Since circuit design requires more external input, a single control of a button needs to waste a lot of IO resources, so there is a matrix keyboard. The commonly used matrix keyboards are 4X4 and 8X8, and the most used one is 4X4.

working principle

Matrix keyboard is also called determinant keyboard, which is a keyboard composed of 4 I/O lines as row lines and 4 I/O lines as column lines. At each intersection of row lines and column lines, set a button. The number of keys in the keyboard is 4×4 like this. This determinant keyboard structure can effectively improve the utilization rate of the I/O port in the single-chip microcomputer system. Since the IO port of the single chip microcomputer has the function of line AND, when any key is pressed, a line in the row and column is ANDed, and the coordinates of the key can be obtained through calculation to determine the key value of the key.

insert image description here
The principle of the row-column scanning method
1. Make the row line the input line of programming, and the column line is the output line. Pull down all the column lines to judge the change of the row line. If a button is pressed, the corresponding row line of the button is pulled down. , otherwise all row lines are high.
2. After judging that a key is pressed in the first step, delay 10ms to eliminate mechanical jitter, and read the line value again. If the line is still in a low state, go to the next step, otherwise return to the first step to re-judge.
3. Start to scan the position of the button, use progressive scanning, and pull down the first column, the second column, the third column, and the fourth column at an interval of 1ms. No matter which column is pulled down, the other three columns are high. , read the row value to find the position of the key, and store the row value and column value in the register respectively.
4. Find the row value and column value from the register and combine them to get the key value, encode the key value, and encode the key value line by line from the first in the first line to the fourth in the fourth line, and encode the value From "0000" to "1111", decode again, and finally display the button number.

insert image description here
4*4 matrix keyboard + 4 independent keys + 8 marquee combination module

insert image description here
insert image description here
4*4 matrix keyboard + 4 independent keys + 8 marquee combination module reference circuit schematic diagram

insert image description here
The 4*4 matrix keyboard on the module has an 8-pin port. In theory, it can be directly plugged into pins 0-7 of the Arduino, but pins 0 and 1 are used for serial communication, so only pins 2~13 can be selected. 2-9 feet.

Keypad Pin L1 –> Arduino Pin 2 (行)

Keypad Pin L2 –> Arduino Pin 3

Keypad Pin L3 –> Arduino Pin 4

Keypad Pin L4 –> Arduino Pin 5

Keypad Pin R1 –> Arduino Pin 6 (列)

Keypad Pin R2 –> Arduino Pin 7

Keypad Pin R3 –> Arduino Pin 8

Keypad Pin R4 –> Arduino Pin 9

Schematic diagram of test matrix keyboard part wiring

insert image description here

Library files required for matrix keyboard
In Arduino IDE 1.8.10 or above, search for Keypad in Project->Load Library->Manage Library, and then install it.
You can also download the library (you need to download the library file https://github.com/Chris–A/Keypad), and then manually add it to the IDE.

insert image description here
Arduino experiment open source code

/*

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

实验一百三十一:YL-004老款20按键独立键盘 跑马灯矩阵键盘模块

程序之一:测试矩阵键盘部分

*/



#include <Keypad.h>

const byte ROWS = 4;

const byte COLS = 4;

char hexaKeys[ROWS][COLS] = {
    
    

  {
    
    '1','2','3','A'},

  {
    
    '4','5','6','B'},

  {
    
    '7','8','9','C'},

  {
    
    '*','0','#','D'}

};

byte rowPins[ROWS] = {
    
    2, 3, 4, 5};

byte colPins[COLS] = {
    
    6, 7, 8, 9};

Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup(){
    
    

  Serial.begin(9600);

}

void loop(){
    
    

  char customKey = customKeypad.getKey();

  if (customKey){
    
    

  Serial.println(customKey);

  }

}

Serial port output 16-bit matrix key value

insert image description here

Program 2: The matrix keyboard part tests the pressing and releasing of each key

/*

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

 实验一百三十一:YL-004老款20按键独立键盘 跑马灯矩阵键盘模块

  程序之二:矩阵键盘部分测试每个按键的按下与松开

*/



#include "Adafruit_Keypad.h"

const byte ROWS = 4; // 行

const byte COLS = 4; // 列

//定义键盘按钮上的符号

char hexaKeys[ROWS][COLS] = {
    
    

  {
    
    '1', '2', '3', 'A'},

  {
    
    '4', '5', '6', 'B'},

  {
    
    '7', '8', '9', 'C'},

  {
    
    '*', '0', '#', 'D'}

};

byte rowPins[ROWS] = {
    
    2, 3, 4, 5}; //连接到键盘的行插脚

byte colPins[COLS] = {
    
    6, 7, 8, 9}; //连接到键盘的列插脚

//初始化类NewKeypad的实例

Adafruit_Keypad customKeypad = Adafruit_Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup() {
    
    

  Serial.begin(9600);

  customKeypad.begin();

}

void loop() {
    
    

  //把你的主代码放在这里,重复运行

  customKeypad.tick();

  while (customKeypad.available()) {
    
    

  keypadEvent e = customKeypad.read();

  Serial.print((char)e.bit.KEY);

  if (e.bit.EVENT == KEY_JUST_PRESSED) Serial.println(" pressed");

  else if (e.bit.EVENT == KEY_JUST_RELEASED) Serial.println(" released");

  }

  delay(10);

}

Experimental serial port return

insert image description here
Procedure 3: Test the keys '*' and '#' on the matrix keyboard to control the LED lights (13 pins on board)

/*

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

 实验一百三十一:YL-004老款20按键独立键盘 跑马灯矩阵键盘模块

  程序之三:矩阵键盘部分测试按键'*'和 '#'控制LED灯(板载13脚)

*/



#include <Keypad.h>

const byte ROWS = 4; //四行

const byte COLS = 3; //三列

//定义键盘按钮上的符号

char keys[ROWS][COLS] = {
    
    

  {
    
    '1', '2', '3'},

  {
    
    '4', '5', '6'},

  {
    
    '7', '8', '9'},

  {
    
    '*', '0', '#'}

};

byte rowPins[ROWS] = {
    
    2, 3, 4, 5}; //连接到键盘的行插脚

byte colPins[COLS] = {
    
    6, 7, 8};  //连接到键盘的列插脚

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

byte ledPin = 13;

boolean blink = false;

boolean ledPin_state;

void setup() {
    
    

  Serial.begin(9600);

  pinMode(ledPin, OUTPUT);          // 将数字管脚设置为输出

  digitalWrite(ledPin, HIGH);        // 打开LED

  ledPin_state = digitalRead(ledPin);  // 存储初始LED状态。当LED亮起时为高

  keypad.addEventListener(keypadEvent); // 为此键盘添加事件侦听器

}

void loop() {
    
    

  char key = keypad.getKey();

  if (key) {
    
    

  Serial.println(key);

  }

  if (blink) {
    
    

  digitalWrite(ledPin, !digitalRead(ledPin));  // 将LED引脚从Hi2Lo或Lo2Hi更改

  delay(100);

  }

}

// 处理一些特殊事件

void keypadEvent(KeypadEvent key) {
    
    

  switch (keypad.getState()) {
    
    

  case PRESSED:

    if (key == '#') {
    
    

      digitalWrite(ledPin, !digitalRead(ledPin));

      ledPin_state = digitalRead(ledPin);      // 记住LED状态,亮或不亮

    }

    break;

  case RELEASED:

    if (key == '*') {
    
    

      digitalWrite(ledPin, ledPin_state);  // 从开始闪烁前恢复LED状态

      blink = false;

    }

    break;

  case HOLD:

    if (key == '*') {
    
    

      blink = true;  // 按住*键时,LED闪烁

    }

    break;

  }

}

The '#' key controls the self-locking switch of the LED light, the LED flashes when the '*' key is pressed, and turns off when it is released

insert image description here
Schematic diagram of test marquee part wiring

insert image description here
insert image description here
Procedure 4: Test the marquee part, light up one by one in a cycle, and turn off one by one

/*

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

 实验一百三十一:YL-004老款20按键独立键盘 跑马灯矩阵键盘模块

  程序之四:测试跑马灯部分,循环逐个点亮,逐个熄灭

*/



int BASE = 6 ;

int NUM = 8;

void setup(){
    
    

  for (int i = BASE; i < BASE + NUM; i ++)

  {
    
    

  pinMode(i, OUTPUT);

  }

}

void loop(){
    
    

  for (int i = BASE; i < BASE + NUM; i ++)

  {
    
    

  digitalWrite(i, LOW);

  delay(200);

  }

  for (int i = BASE; i < BASE + NUM; i ++)

  {
    
    

  digitalWrite(i, HIGH);

  delay(200);

  }

}

Arduino experiment scene diagram

insert image description here
Procedure 5: Test the marquee part, turn off one by one in a loop

/*

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

 实验一百三十一:YL-004老款20按键独立键盘 跑马灯矩阵键盘模块

  程序之五:测试跑马灯部分,循环单个熄灭

*/



byte ledPin[] = {
    
    6, 7, 8, 9, 10, 11, 12, 13};

int ledDelay(300);

int direction = 1;

int currentLED = 0;

unsigned long changeTime;

void setup() {
    
    

  for (int x = 0; x <= 7; x++)

  pinMode(ledPin[x], OUTPUT); //数组从0开始分配存储单元,这里要非常注意循环变量的初值和终值,0-7刚好循环至8

  changeTime = millis();

}

void loop() {
    
    

  if ((millis() - changeTime) > ledDelay)

  {
    
     changeLED(); //运行时间如果超过一个数量的延时时间,则由函数changeLED()控制LED点亮的变化

  changeTime = millis();

  }

}

void changeLED() {
    
    

  for (int x = 0; x <= 7; x++)

  {
    
     digitalWrite(ledPin[x], LOW); //8只LED灯低平信号,熄灭

  }

  digitalWrite(ledPin[currentLED], HIGH); //设当前LED高平信号,点亮

  currentLED += direction; //当前值增加为direction表示的值

  if (currentLED == 7) {
    
    

  direction = -1; //当前灯为第8只灯,currentLED当前值改为-1,改变亮灯方向

  }

  if (currentLED == 0) {
    
    

  direction = 1; //当前灯为第1只灯,currentLED当前值为+1,从第1只灯开始计数

  }

}

Test the independent button part + marquee part

insert image description here
Program 6: Test the independent key part + marquee part, the key controls the marquee

/*

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

 实验一百三十一:YL-004老款20按键独立键盘 跑马灯矩阵键盘模块

  程序之六:测试独立按键部分+跑马灯部分,按键控制跑马灯

*/



int buttonPin = 2 ;  // 定义按键引脚

int BASE = 6;      // 定义第一个LED引脚

int NUM = 8;      // LED 的总数

int val;

void setup()

{
    
    

  Serial.begin(9600);

  pinMode(buttonPin, INPUT);

  for (int i = BASE; i < BASE + NUM; i ++)

  {
    
    

  pinMode(i, OUTPUT);  // 设定LED引脚为输出引脚

  }

}

void loop()

{
    
    

  liangdeng();        // 循环亮灯子程序

  delay(200);        // 每次循环间隔时间

  miedeng();          // LED全灭子程序

}

void liangdeng()

{
    
    

  for (int i = BASE; i < BASE + NUM; i ++)

  {
    
    

  digitalWrite(i, LOW);  //设定LED引脚逐个输出高电平逐个点亮

  val = digitalRead(buttonPin);  // 储存按键变换量

  if (val == HIGH)      //判断是否按下按键,弱已按下按键跳出当前循环

  {
    
    

    val = HIGH;

    break;

  }

  delay(200);      //延时

  }

}

void miedeng()

{
    
    

  for (int i = BASE; i < BASE + NUM; i ++)

  {
    
    

  digitalWrite(i, HIGH);  //设定LED引脚逐个输出低电平逐个点亮

  }

}

Experimental open source graphics programming (Mind+, programming while learning)

insert image description here

Experimental open source simulation programming (Linkboy V4.62)
insert image description here

Guess you like

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