Counting on a TM1637

This is what I have come up with regards to using the TM1637 as a counter.

Two questions:

  • Is there a better way to do this?
  • How do a I blank a digit i.e turn the leading zeros off?
#include <Arduino.h>
#include <TM1637Display.h>

// Module connection pins (Digital Pins)
#define CLK 2
#define DIO 3

int k = 0;
uint8_t data[] = { 0xff, 0xff, 0xff, 0xff };

int intdata0;
int intdata1;
int intdata2;
int intdata3;

// The amount of time (in milliseconds) between tests
#define TEST_DELAY   1000

TM1637Display display(CLK, DIO);

void setup()
{
  Serial.begin(9600); 
}

void loop()
{
  
  
  display.setBrightness(0x0f);
  intdata0 = k/1000;
  data[0] = display.encodeDigit(intdata0);
  
  intdata1 = k-intdata0*1000;
  data[1] = display.encodeDigit(intdata1/100);
  
  intdata2 = intdata1-(intdata1/100)*100;
  data[2] = display.encodeDigit(intdata2/10);
  
  data[3]= display.encodeDigit(intdata2%10);
  
  Serial.print(intdata0);
  Serial.print("\t");
  Serial.print(intdata1);
  Serial.print("\t");
  Serial.print(intdata2);
  Serial.print("\t");
  Serial.println(intdata3);
  display.setSegments(data);

  delay(TEST_DELAY);
  k=k+1;

}

Is there a better way to do this?

Yes. Tell us what a TM1627 is. Don't expect us to google whatever it is you have.

Most of your variables do not need to be global, and, therefore, shouldn't be.

The TM1637 library you're using should have documentation to tell you how to blank a digit. If it's at all like the TM1638 library I'm using, it will also have a single function to send a decimal value to the display, using all the digits. So you wouldn't have to go through all those machinations.

Alas I don't appear to be able to find it. I am using GitHub - avishorp/TM1637: Arduino library for TM1637 (LED Driver). If you are using a different one, could you post the link?