/*
key1:按一下加1,最大到9,再按一下為0(例01234567890....)
key2:按一下減1,最小到9,再按一下為0(例98765432109....)
key3:自動上數0~9
key4:暫停
*/
#define key1 A0
#define key2 A1
#define key3 A2
#define key4 A3
int i;
int j;
int keyval;
bool up_flag=0;
bool stop_flag=0;
const char num[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
const int seg[]={2,3,4,5,6,7,8,9};
void setup()
{
for(i=0;i<8;i++)
{
pinMode(seg[i],OUTPUT);
}
pinMode(key1,INPUT);
pinMode(key2,INPUT);
pinMode(key3,INPUT);
pinMode(key4,INPUT);
}
void loop()
{
if(digitalRead(key1)==0 && up_flag==0)//按鍵1為按一下加1
{
delay(20);
while(digitalRead(key1)==0);
keyval++;
if(keyval>9) keyval=0;
output(num[keyval]);
}
if(digitalRead(key2)==0 && up_flag==0)//按鍵2為按一下減1
{
delay(20);
while(digitalRead(key2)==0);
keyval--;
if(keyval<0) keyval=9;
output(num[keyval]);
}
if(digitalRead(key3)==0 )//按鍵3為自動上數0~9
{
stop_flag=0;
up_flag=1;
}
if(up_flag==1)
{
delay(20);
while(digitalRead(key3)==0);
keyval++;
if(keyval>9) keyval=0;
output(num[keyval]);
delay(1000);
}
if(digitalRead(key4)==0 && stop_flag==0)
{
up_flag=0;
stop_flag=1;
}
if(stop_flag)
{
;
}
delay(20);
}
void output(byte data)
{
for(i=0;i<8;i++)
digitalWrite(seg[i],bitRead(~data,i));
}
沒有留言:
張貼留言