|
程序功能:实现下图这样的矩阵键盘控制,将按键代表的数字或字母输出到上位机。
[mw_shl_code=cpp,true]/***************horizontal port******************/
#define hor1 30
#define hor2 31
#define hor3 32
#define hor4 33
int horizontal[] = {30, 31, 32, 33};
/***************vertical port****************/
#define ver1 34
#define ver2 35
#define ver3 36
#define ver4 37
int vertical[] = {34, 35, 36, 37};
int i = 0, j = 0;
int num = 0;
int count = 0;
char sym = '0';
int height=0;
void setup()
{
Serial.begin(9600);
for (i = 0; i < 4; i++)
{
pinMode(13, OUTPUT);
pinMode(horizontal, OUTPUT);
pinMode(vertical, INPUT_PULLUP);
}
}
void loop()
{
for (i = 0; i < 4; i++)
{
digitalWrite(hor1, HIGH);
digitalWrite(hor2, HIGH);
digitalWrite(hor3, HIGH);
digitalWrite(hor4, HIGH);
digitalWrite(horizontal, LOW);
for (j = 0; j < 4; j++)
{
if (digitalRead(vertical[j]) == LOW)
{
delay(10);
if (digitalRead(vertical[j]) == LOW)
{
if (i != 3 && j != 3)
{
num = j + 3 * i + 1;
Serial.println(num);
count++;
if (count == 1)
//The first Number We take
{
height = num * 10;
}
else if (count == 2)
{
height += num;
}
else
{
count = 0;
num=0;
}
}
if (j == 3)
{
switch (i)
{
case 0:
sym = 'A';
num = 0;
count = 0;
break;
case 1:
sym = 'B';
break;
case 2:
sym = 'C';
break;
case 3:
sym = 'D';
if(count == 1)
Serial.println(height/10);
else
Serial.println(height);
num = 0;
count = 0;
break;
}
Serial.println(sym);
}
if (i == 3 && j != 3)
{
if (j == 0)
sym = '*';
if (j == 2)
sym = '#';
Serial.println(sym);
if (j == 1)
{
num = 0;
Serial.println(num);
count++;
if (count == 1)
{
height = num * 10;
}
else if (count == 2)
{
height += num;
}
else
{
count = 0;
num=0;
}
}
}
}
while (digitalRead(vertical[j]) == LOW);
delay(10);
}
}
}
}
[/mw_shl_code]
|
|