//功能:按鈕1按一下上數,按一下暫停,再按一下繼續上數,數到200就暫停
unsigned long time1 = 0;
int seg7_pin[] = {2, 3, 4, 5, 6, 7, 8};
unsigned char seg7_table[] = {~0xc0, ~0xf9, ~0xa4, ~0xb0, ~0x99, ~0x92, ~0x82, ~0xf8, ~0x80, ~0x90};
unsigned char scan_line_pin[] = {9, 10,11};//掃描線從com0個位數開始掃描
//3 2 1 0所以每認-1即依續scan個十百千
int btn_pin = 13;
int btn_status = 0; //
int num_val;
int cnt_val = 0;
int i;
int j;
int read_status;
void setup()
{
Serial.begin(9600);
pinMode(btn_pin, INPUT);
for (int m = 0; m < 3; m++)
{
pinMode(scan_line_pin[m], OUTPUT);
//digitalWrite(scan_line_pin[m], LOW);
}
for (int s = 0; s < 7; s++)
{
pinMode(seg7_pin[s], OUTPUT);
digitalWrite(seg7_pin[s], LOW);
}
}
//----------------------------------
void outport(byte data)//呼叫個別數字副程式
{
for (int k = 0; k < 7; k++)
{
if (data % 2 == 1)//整數部分連除法,從a開始取到g
digitalWrite(seg7_pin[k], HIGH);
else
digitalWrite(seg7_pin[k], LOW);
data = data / 2;//每除完一次除2
}
}
void loop()
{
read_status = digitalRead(btn_pin);//寫這行一定要加btn鍵的線,否則不會亮
if (read_status == LOW)
{
delay(1000);
while (digitalRead(btn_pin) == LOW);
if (btn_status == 1)
btn_status = 0;
else
btn_status++ ; //每按一下,就暫停
}
num_val = cnt_val;//因為每認數字不同,所以要有兩個變數一個為原有的,一個為新加1的數值
for ( i = 0; i < 3; i++) //scan從index=1為第二個數開始掃描,9(右1) 10(右2)
{
for ( j = 0; j < 7; j++)//此loop只是在跑7段顯示器的腳位
{
outport(seg7_table[num_val % 10]);//呼叫副程式,先取mod 10 餘數,從個位數開始取,
}//即個位數的com0高電位,送字
digitalWrite(scan_line_pin[i], HIGH); delay(2);//亮個位數2ms 七段顯示數字,再十位數,七段顯示數字
digitalWrite(scan_line_pin[i], LOW);//在把個十百千
num_val = num_val / 10;//取餘數完再除10
if (millis() - time1 >= 1000)//此處不能寫delay(1000)取代
{
time1 = millis();
if (btn_status == 1)
{
//Serial.println(btn_status);//Serial用來偵錯用
;//按一下,我什麼都不做,再按一下又繼續exe
}
else if (btn_status == 0)
{
//Serial.println(btn_status);
if (cnt_val == 200)//數到200就停在200
cnt_val = 200;
else
cnt_val++;
}
}
}
}
沒有留言:
張貼留言