int i;
int j;
int k;
int ledStatus[10];
const int numCols=4;
const int numRows=4;
const int debounceDelay=20;
const int led[]={10,11,12,13,14,15,16,17,18,19};
const int col[]={6,7,8,9};//{9,8,7,6};
const int row[]={2,3,4,5};
const int keyMap[numRows][numCols]=
{
{1, 2, 3, 12},
{4, 5, 6, 13},
{7, 8, 9, 14},
{10,0,11, 15}
};
void setup()
{
for(i=0;i<numRows;i++)
{
pinMode(row[i],OUTPUT);
digitalWrite(row[i],HIGH);
}
for(j=0;j<numCols;j++)
{
pinMode(col[j],INPUT);
digitalWrite(col[j],HIGH);
}
for(k=0;k<10;k++)
{
pinMode(led[k],OUTPUT);
digitalWrite(led[k],LOW);
ledStatus[k]=LOW;
}
}
void loop()
{
int key=getKey();
if(key>=0 && key<=9)
{
ledStatus[key]=!ledStatus[key];
digitalWrite(led[key],ledStatus[key]);
}
}
int getKey()
{
int i;
int j;
int key=-1;
for(i=0;i<numRows;i++)
{
digitalWrite(row[i],LOW);
for(j=0;j<numCols;j++)
{
if(digitalRead(col[j])==LOW)
{
delay(debounceDelay);
while(digitalRead(col[j])==LOW)
;
key=keyMap[i][j];
}
}
digitalWrite(row[i],HIGH);
}
return(key);
}
沒有留言:
張貼留言