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[] = {11, 12};//掃描線從com0個位數開始掃描
//3 2 1 0所以每認-1即依續scan個十百千
int btn_pin = 13;
int odd_even = 0; //0一開機為上數,1一開機為下數
int num_val;
int cnt_val = 0;
int i;
int j;
int read_status;
void setup()
{
pinMode(btn_pin, INPUT);
for (int m = 0; m < 4; 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);
if (read_status == LOW)
{
delay(50);
while (digitalRead(btn_pin) == LOW);
odd_even++;//每按一下,由奇變偶,或偶變奇
}
num_val = cnt_val;//因為每認數字不同,所以要有兩個變數一個為原有的,一個為新加1的數值
for ( i = 1; i >= 0; i--)//scan從index=1為第二個數開始掃描,12(右1) 11(右2) 10(左2) 9(左1)
{
for ( j = 0; j < 7; j++)//此loop只是在跑7段顯示器的腳位
{
outport(seg7_table[num_val % 10]);//呼叫副程式,先取mod 10 餘數,從個位數開始取,
// Serial.println(j);debug結束後要把Serial刪掉,否則會有閃樂現象,因為他會佔用時間
// Serial.println(num_val);
}//即個位數的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 (odd_even % 2 == 0)
{
if (cnt_val == 99)
cnt_val = 0;
else
cnt_val++;
}
else
{
if (cnt_val == 0)
cnt_val = 99;
else
cnt_val--;
}
}
}
}
沒有留言:
張貼留言