使用arduino无法获得时间吗?

时间:2017-05-12 22:15:37

标签: arduino display arduino-ide led

我有一个小型7段LED显示屏,我想知道我是否可以让它向我展示分钟的最后一位数字。下载了Arduino的时间库,我尝试了几种方法,使用switch case,如果是,但是都得到相同的结果,LED显示只显示0 ...你能找出我做错了什么吗?

#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);
    }





}    

2 个答案:

答案 0 :(得分:1)

你永远不会写任何引脚变低。当下一分钟点击时,正确的LED可能会打开,但旧的LED不会熄灭,所以你不会看到任何变化。

为了避免每个周期关闭和打开所有LED,我将存储最后一个已知数字,并在每次循环迭代时查看数字是否已更改。如果有,请关闭所有指示灯,然后运行您在此处发布的代码。

答案 1 :(得分: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);   
    }
}