2017-05-12 60 views
0

我有一個小型7段led顯示屏,我想知道是否可以讓它顯示一分鐘的最後一位數字。下載了Arduino的時間庫,我嘗試了幾種方法,使用switch case和if if if,但都得到相同的結果,LED顯示屏只顯示0 ...你能指出我做錯了什麼嗎?Arduino無法讓時間正確嗎?

#include <Time.h> 
#include <TimeLib.h> 

int led1=2; 
int led2=3; 
int led3=4; 
int led4=5; 
int led5=6; 
int led6=7; 
int led7=8; 
int led8=9; 

void setup() { 

    pinMode(led1,OUTPUT); 
    pinMode(led2,OUTPUT); 
    pinMode(led3,OUTPUT); 
    pinMode(led4,OUTPUT); 
    pinMode(led5,OUTPUT); 
    pinMode(led6,OUTPUT); 
    pinMode(led7,OUTPUT); 
    pinMode(led8,OUTPUT); 

} 

void loop(){ 
    time_t t=minute(); 
    int digit1=t%10; 


    if(digit1==1){ 
    digitalWrite(led5, HIGH); 
    digitalWrite(led3, HIGH); 
    }else if(digit1==2) 

    { 
    digitalWrite(led6, HIGH); 
    digitalWrite(led5, HIGH); 
    digitalWrite(led4, HIGH); 
    digitalWrite(led1, HIGH); 
    digitalWrite(led2, HIGH); 
    } else if(digit1==3) 

    { 
    digitalWrite(led4, HIGH); 
    digitalWrite(led6, HIGH); 
    digitalWrite(led5, HIGH); 
    digitalWrite(led3, HIGH); 
    digitalWrite(led2, HIGH); 
    }else if(digit1==4) 

    { 
    digitalWrite(led4, HIGH); 
    digitalWrite(led7, HIGH); 
    digitalWrite(led5, HIGH); 
    digitalWrite(led3, HIGH); 
    }else if(digit1==5) 

    { 
    digitalWrite(led6, HIGH); 
    digitalWrite(led7, HIGH); 
    digitalWrite(led4, HIGH); 
    digitalWrite(led3, HIGH); 
    digitalWrite(led2, HIGH); 
    }else if(digit1==6) 

    { 
    digitalWrite(led6, HIGH); 
    digitalWrite(led7, HIGH); 
    digitalWrite(led4, HIGH); 
    digitalWrite(led1, HIGH); 
    digitalWrite(led2, HIGH); 
    digitalWrite(led3, HIGH); 
    }else if(digit1==7) 

    { 
    digitalWrite(led5, HIGH); 
    digitalWrite(led6, HIGH); 
    digitalWrite(led3, HIGH); 
    }else if(digit1==8) 

    { 
    digitalWrite(led1, HIGH); 
    digitalWrite(led2, HIGH); 
    digitalWrite(led3, HIGH); 
    digitalWrite(led4, HIGH); 
    digitalWrite(led5, HIGH); 
    digitalWrite(led6, HIGH); 
    digitalWrite(led7, HIGH); 
    }else if(digit1==9) 

    { 
    digitalWrite(led2, HIGH); 
    digitalWrite(led3, HIGH); 
    digitalWrite(led4, HIGH); 
    digitalWrite(led5, HIGH); 
    digitalWrite(led6, HIGH); 
    digitalWrite(led7, HIGH); 
    }else if(digit1==0) 

    { 
    digitalWrite(led1, HIGH); 
    digitalWrite(led2, HIGH); 
    digitalWrite(led3, HIGH); 
    digitalWrite(led5, HIGH); 
    digitalWrite(led6, HIGH); 
    digitalWrite(led7, HIGH); 
    } 





}  

回答

0

您還沒有添加低的條件,因此當引腳變高時它仍然很高。另外,我檢查了一下,我認爲你的佈線不正確。我在下面發佈完整的代碼和相應的連接。

CONNECTIONS FOR 7 SEG

#include <Time.h> 
#include <TimeLib.h> 

int a = 2; 
int b = 3; 
int c = 4; 
int d = 5; 
int e = 6; 
int f = 7; 
int g = 8; 
int dp = 9; 

void setup() { 
    pinMode(led1,OUTPUT); 
    pinMode(led2,OUTPUT); 
    pinMode(led3,OUTPUT); 
    pinMode(led4,OUTPUT); 
    pinMode(led5,OUTPUT); 
    pinMode(led6,OUTPUT); 
    pinMode(led7,OUTPUT); 
    pinMode(led8,OUTPUT); 
} 

void loop(){ 
    time_t t=minute(); 
    int digit1=t%10; 


    if(digit1==0) { 
     digitalWrite(a, HIGH); 
     digitalWrite(b, HIGH); 
     digitalWrite(c, HIGH); 
     digitalWrite(d, HIGH); 
     digitalWrite(e, HIGH); 
     digitalWrite(f, HIGH); 
     digitalWrite(g, LOW); 
     digitalWrite(dp, LOW); 
    } 

    else if(digit1==1) { 
     digitalWrite(a, LOW); 
     digitalWrite(b, HIGH); 
     digitalWrite(c, HIGH); 
     digitalWrite(d, LOW); 
     digitalWrite(e, LOW); 
     digitalWrite(f, LOW); 
     digitalWrite(g, LOW); 
     digitalWrite(dp, LOW); 
    } 

    else if(digit1==2) { 
     digitalWrite(a, HIGH); 
     digitalWrite(b, HIGH); 
     digitalWrite(c, LOW); 
     digitalWrite(d, HIGH); 
     digitalWrite(e, HIGH); 
     digitalWrite(f, LOW); 
     digitalWrite(g, HIGH); 
     digitalWrite(dp, LOW); 
    } 

    else if(digit1==3) { 
     digitalWrite(a, HIGH); 
     digitalWrite(b, HIGH); 
     digitalWrite(c, HIGH); 
     digitalWrite(d, HIGH); 
     digitalWrite(e, LOW); 
     digitalWrite(f, LOW); 
     digitalWrite(g, HIGH); 
     digitalWrite(dp, LOW); 
    } 

    else if(digit1==4) { 
     digitalWrite(a, LOW); 
     digitalWrite(b, HIGH); 
     digitalWrite(c, HIGH); 
     digitalWrite(d, LOW); 
     digitalWrite(e, LOW); 
     digitalWrite(f, HIGH); 
     digitalWrite(g, HIGH); 
     digitalWrite(dp, LOW); 
    } 

    else if(digit1==5) { 
     digitalWrite(a, HIGH); 
     digitalWrite(b, LOW); 
     digitalWrite(c, HIGH); 
     digitalWrite(d, HIGH); 
     digitalWrite(e, LOW); 
     digitalWrite(f, HIGH); 
     digitalWrite(g, HIGH); 
     digitalWrite(dp, LOW); 
    } 

    else if(digit1==6) { 
     digitalWrite(a, LOW); 
     digitalWrite(b, LOW); 
     digitalWrite(c, HIGH); 
     digitalWrite(d, HIGH); 
     digitalWrite(e, HIGH); 
     digitalWrite(f, HIGH); 
     digitalWrite(g, HIGH); 
     digitalWrite(dp, LOW); 
    } 

    else if(digit1==7) { 
     digitalWrite(a, HIGH); 
     digitalWrite(b, HIGH); 
     digitalWrite(c, HIGH); 
     digitalWrite(d, LOW); 
     digitalWrite(e, LOW); 
     digitalWrite(f, LOW); 
     digitalWrite(g, LOW); 
     digitalWrite(dp, LOW); 
    } 

    else if(digit1==8) { 
     digitalWrite(a, HIGH); 
     digitalWrite(b, HIGH); 
     digitalWrite(c, HIGH); 
     digitalWrite(d, HIGH); 
     digitalWrite(e, HIGH); 
     digitalWrite(f, HIGH); 
     digitalWrite(g, HIGH); 
     digitalWrite(dp, LOW); 
    } 

    else if(digit1==9) { 
     digitalWrite(a, HIGH); 
     digitalWrite(b, HIGH); 
     digitalWrite(c, HIGH); 
     digitalWrite(d, HIGH); 
     digitalWrite(e, LOW); 
     digitalWrite(f, HIGH); 
     digitalWrite(g, HIGH); 
     digitalWrite(dp, LOW); 
    } 
} 
+0

我試過你的方法重新佈線,並將各自的LED拉低,但無濟於事。問題是't'分配的值。我註釋掉了開關盒/是否還有零件,並試圖在串行監視器上打印值,而這個值始終爲零。我還會添加一個串口監視器的照片。 –

+0

是,則該顯示器被共同陰極,顯示實際上是一個4位數字顯示,具有用於每個數字的陰極,然後陽極爲每個LED段(http://raspi.tv/wp-content/uploads/2015/ 11/7SEG-引出線-annotated_1500.jpg)。但問題是不與顯示器,是與時間變量t,以及它返回...... –

+0

即時得到與上述程序正確的輸出。要查看你必須等待一分鐘,然後,然後你會看到從0到1的變化我試圖使用'time_t的T =第二()的變化;'和每月的第二個拿到輸出。 –

1

你從來不寫任何引腳變爲低電平。當下一分鐘命中時,正確的LED可能會亮起,但舊的LED不會熄滅,所以你沒有看到任何改變。

爲了避免將所有的LED關閉並重新每一個週期,我將存儲最後已知的數字,在每次循環迭代,看看數字發生了變化。如果有,請關閉所有燈,然後運行您在此發佈的代碼。

+0

我已經試過你的方法,但無濟於事。問題是't'分配的值。我註釋掉了開關盒/是否還有零件,並試圖在串行監視器上打印值,而這個值始終爲零。我還會添加一個串口監視器的照片。 –