//功能:按鈕1按一下上數,按一下暫停,再按一下繼續上數,數到200就暫停
unsigned long time1 = 0;
unsigned long time2 = 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_pin1 = 12;//上數按鈕
int btn_pin2 = 13;//下數按鈕
int btn_status1 = 0; //一開機 0上數 1暫停
int btn_status2=9;//10暫停 11下數
int cnt_val = 0;
int num_val;
int chg_up_low;
int i;
int j;
int read_status1;
int read_status2;
void setup()
{
Serial.begin(9600);
pinMode(btn_pin1, INPUT);
pinMode(btn_pin2, 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_status1 = digitalRead(btn_pin1);//寫這行一定要加btn鍵的線,否則不會亮
//Serial.print("read_status1=");Serial.println(read_status1);除錯用看read_status1是否進來
if (read_status1 == LOW)
{
delay(1000);
while (digitalRead(btn_pin1) == LOW);
chg_up_low=0;
btn_status1++ ; //每按一下0+1=1,就暫停,再按1下就2歸0
if (btn_status1 == 2)
btn_status1 = 0;
}
//--------------------------------------------
read_status2 = digitalRead(btn_pin2);
if (read_status2 == LOW )
{
delay(1000);
Serial.print("read_status2=");Serial.println(read_status2);//除錯用看read_status2是否進來
while (digitalRead(btn_pin2) == LOW);//
chg_up_low=1;
//Serial.print("chg_up_low=");Serial.println(chg_up_low);//除錯用看chg_up_low是否進來
//delay(5000);因為跑太快了,所以delay 5秒中
btn_status2++ ; //每按一下9+1=10,就暫停,再按一下11就歸10
if (btn_status2 == 12)
btn_status2 = 10;
}
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_status1 == 1 && chg_up_low==0)
{
//Serial.println(btn_status);//Serial用來偵錯用
;//按一下,我什麼都不做,再按一下又繼續exe
}
else if (btn_status1 == 0 && chg_up_low==0)
{
Serial.print("btn_status1=");Serial.println(btn_status1);
Serial.print("chg_up_low=");Serial.println(chg_up_low);
if (cnt_val == 200)//數到200就停在200
cnt_val = 200;
else
cnt_val++;
}
if (millis() - time2 >= 1000)//此處不能寫delay(1000)取代
{
time2 = millis();
if (btn_status2 == 10 && chg_up_low==1)
{
Serial.print("btn_status2=");Serial.println(btn_status2);//Serial用來偵錯用
Serial.println("chg_up_low=");Serial.println(chg_up_low);
;//按一下,我什麼都不做,再按一下又繼續exe
}
else if (btn_status2 == 11 && chg_up_low==1)
{
Serial.println(cnt_val);
Serial.println(chg_up_low);
if (cnt_val == 0)//數到0就停在0
cnt_val = 0;
else
cnt_val--;
}
}
}
}
}
沒有留言:
張貼留言