首页 > 解决方案 > 如何使用 TM1637 和每秒闪烁的冒号实现时钟?

问题描述

我想实现一个带有 ESP-32 和 TM1637 4 位显示器的时钟。我的代码此时工作得很好,但是当时钟到达 23:00 时,它仍然显示 22 小时。当我让时钟运行到 24 点时,显示所有段亮起。

这是我的代码:

//...

String timeStamp;
unsigned long timeNow = 0;
unsigned long timeLast = 0;
int hours;
int minutes;
int seconds;
int value = 1244;
uint8_t colonOn = 0x80 | display.encodeDigit((value / 100)%10);
uint8_t colonOff = 0x00 | display.encodeDigit((value / 100)%10);

//...

display.setBrightness(8);
uint8_t data[] = { 0xff, 0xff, 0xff, 0xff };
display.setSegments(data);

这是我的循环功能:首先是一些设置

void loop()
{
    timeNow = millis()/1000;
    seconds = timeNow - timeLast;

    if ((timeNow % 2) == 0) {
        display.setSegments(&colonOn, 1, 1);  // Turns the colon on
    } else {
        display.setSegments(&colonOff, 1, 1); // Turns the colon off
    }

    if (seconds == 60) {
        timeLast = timeNow;
        minutes += 1;
        display.showNumberDec(minutes, true, 2, 2);
    }

    if (minutes == 60) {
        minutes = 0;
        hours += 1;
        display.showNumberDec(minutes, true, 2, 2);
        display.showNumberDec(hours, true, 2, 0);
    }

    if (hours == 24) {
        hours = 0;
        resetFunc(); // call the resetFunction so the microcontroller restarts
    }

    delay(990);
}

我发现用以下代码设置冒号可以解决这个问题: display.setSegments(&colonOn, 1, 1); 没有这条线,时钟似乎可以工作,但是冒号不会每秒都出现

标签: arduinoarduino-c++

解决方案


推荐阅读