问题相关代码
#include "Keypad.h"
const int ROWS = 1;
const int COLS = 3;
char keys[ROWS][COLS] = {
{'1','2','3'},
};
byte rowPins[ROWS] = {3};
byte colPins[COLS] = {A0,4,2};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
//arduino uno两个中断引脚
int pinInterrupt1 = 2;
int pinInterrupt2 = 3;
void onChange()
{
char key =keypad.getKey();
if (key != NO_KEY){
Serial.println(key);
}
}
void setup()
{
Serial.begin(9600);
pinMode( pinInterrupt1, INPUT);
pinMode( pinInterrupt2, INPUT);
//Enable中断管脚, 中断服务程序为onChange(), 监视引脚变化
attachInterrupt( digitalPinToInterrupt(pinInterrupt1), onChange, CHANGE);
attachInterrupt( digitalPinToInterrupt(pinInterrupt2), onChange, CHANGE);
//为什么2、3中断引脚控制的按键(3)仍有延迟,似乎没执行中断程序
}
void loop()
{
// 模拟长时间运行的进程/复杂的任务。
for (int i = 0; i < 100; i++)
{
delay(20);
}
}
运行结果
2、3中断引脚控制的按键按下后似乎没执行中断程序
而其他两个按键按下后正常执行中断程序
我想要达到的结果
请教一下如何让2、3中断引脚控制的按键按下后也能执行中断程序,顺便问一下,为什么会发生这种情况?我查询了相关矩阵按键的原理,发现按下按键后应该是行或列对应的引脚电平会发生变化。