int i;
int j;
int k;
bool up_flag=0;
bool down_flag=0;
bool stop_flag=0;
const int numCols=4;
const int numRows=4;
const int debounceDelay=20;
int showKey=0;
const char num[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};
const int seg7[]={6,7,8,9,10,11,12};
const int col[]={A0,A1,A2,A3};//類比輸入當數位腳(行)
const int row[]={2,3,4,5};//{9,8,7,6};
const int keyMap[numRows][numCols]=
{
{1, 2, 3, 10},
{4, 5, 6, 11},
{7, 8, 9, 12},
{14,0,15, 13}
};
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<7;k++)
{
pinMode(seg7[k],OUTPUT);
digitalWrite(seg7[k],LOW);
}
}
void loop()
{
int pressedKey=getKey();
if(up_flag==1)
{
showKey++;
if(showKey>=10) showKey=0;
output(num[showKey]);
delay(1000);
}
if(down_flag==1)
{
showKey--;
if(showKey<0) showKey=9;
output(num[showKey]);
delay(1000);
}
if(stop_flag==1)
{
;
}
if(pressedKey==10)
{
down_flag=0;
up_flag=1;
}
else if(pressedKey>=0 && pressedKey<=9)
{
showKey=pressedKey;
output(num[showKey]);
delay(1000);
}
else if(pressedKey==11)
{
up_flag=0;
down_flag=1;
}
else if(pressedKey==12)
{
up_flag=0;
down_flag=0;
stop_flag=1;
}
}
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);
}
void output(byte data)
{
for(i=0;i<7;i++)
digitalWrite(seg7[i],bitRead(~data,i));
}
沒有留言:
張貼留言