|
#define SL 4
#define G1 13
#define ROW 4
#define COL 2
#define NoTouch -1
const int scan[COL] = {10,11};
const int row[ROW] = {2,3,4,5};
const int keyScan[SL] = {6,7,8,9};
const int keyRow[SL] = {A0,A1,A2,A3};
void setup() {
for (int i = 0;i<COL;i++) pinMode(scan[i], OUTPUT);
for (int i = 0;i<SL;i++) {
pinMode(keyScan[i], OUTPUT);
pinMode(keyRow[i], INPUT);
}
for (int i = 0;i<ROW;i++) pinMode(row[i], OUTPUT);
pinMode(G1, OUTPUT);
}
void loop() {
int i;
i=KeyCode();
if(i!=NoTouch) prnLed(i); //有按才輸出
}
int KeyCode(){ //掃描鍵盤
int i,j,k;
for(i=0;i<SL;i++) {
for(j=0;j<SL;j++)
digitalWrite(keyScan[j],(i!=j));
for(k=0;k<SL;k++)
if(!digitalRead(keyRow[k])) return(k*SL+i);
}
return(NoTouch);
}
void prnLed(int x){
int i,xi,xj,k;
xi = x / SL;
xj = x % SL;
digitalWrite(G1,0);
for(i=0,k=1;i<COL;i++, k<<=1)
digitalWrite(scan[i],xi & k);
for(i=0;i<ROW;i++, k<<=1)
digitalWrite(row[i],(i==xj));
digitalWrite(G1,1);
}
|