// Example for the TM1637 display with TM1637_RT library.
// By Koepel, 28 February 2023.
//
// Library: "TM1637_RT", https://github.com/RobTillaart/TM1637_RT

#include <TM1637.h>

TM1637 TM1, TM2, TM3;       // Each display needs its own object

unsigned long previousMillisClock; // for the millis timer of the first display
const unsigned long intervalClock = 1000;
bool colon = false;         // for a toggling of the colon in the middle.

unsigned long previousMillisCountDown;  // for a millis timer of second display
const unsigned long intervalCountDown = 100;

int clockSeconds;           // the seconds counter for the first display
int clockMinutes;           // the minute counter for the first display
int countDown = 9999;       // start value of down counter for second display

char buffer[5];             // buffer for third display

void setup()
{
  Serial.begin(115200);
  Serial.println("Type your text for the third display.");
  Serial.println("Now displaying \"----\".");

  // -----------------------------------------
  // First display
  // -----------------------------------------
  TM1.begin(8, 9, 4);       //  clockpin, datapin, #digits
  TM1.displayClear();
  TM1.setBrightness(7);     // full brightness, default is 3
  
  // -----------------------------------------
  // Second display
  // -----------------------------------------
  TM2.begin(6, 7, 4);       //  clockpin, datapin, #digits
  TM2.displayClear();
  TM2.setBrightness(7);     // full brightness, default is 3

  // -----------------------------------------
  // Third display
  // -----------------------------------------
  TM3.begin(4, 5, 4);       //  clockpin, datapin, #digits
  TM3.displayClear();
  TM3.setBrightness(7);     // full brightness, default is 3
  TM3.displayPChar("----"); // Initial text on third display
}

void loop()
{
  unsigned long currentMillis = millis();

  // -----------------------------------------
  // First display
  // -----------------------------------------
  if( currentMillis - previousMillisClock >= intervalClock)
  {
    previousMillisClock += intervalClock; // for a clock-accurate millis timer

    TM1.displayTime(clockMinutes,clockSeconds, colon); // display the numbers
    colon = !colon;         // toggle colon
 
    // increment time
    clockSeconds++;
    if (clockSeconds >= 60)
    {
      clockSeconds = 0;
      clockMinutes++;
      if (clockMinutes >= 60)
      {
        clockMinutes = 0;
      }
    }
  }

  // -----------------------------------------
  // Second display
  // -----------------------------------------
  if( currentMillis - previousMillisCountDown >= intervalCountDown)
  {
    previousMillisCountDown += intervalCountDown; // for a clock-accurate millis timer

    // decrement second display
    TM2.displayInt(countDown); // display the count down value

    countDown--;            // count down
    if (countDown < 0)
    {
      countDown = 9999;
    }
  }

  // -----------------------------------------
  // Third display
  // -----------------------------------------
  if (Serial.available() > 0)
  {
    int inChar = Serial.read();
    if (isprint(inChar))
    {
      for( int i=0; i<3; i++) // shift buffer to the left
        buffer[i] = buffer[i+1];

      buffer[3] = (char) inChar; // add new character
      buffer[4] = '\0';      // add zero-terminator
    }
    TM3.displayPChar(buffer); // display the new text
  }
}
4-Digit Display
4-Digit Display
4-Digit Display