Help with TM1637 6-digit And Button

Please, post the camera picture of your display unit.

OK, please try what should mean turn on all the decimal points

  display.showNumberDec(12, 0xff, true, 2, 0); // 12. at position 0, 1
  display.showNumberDec(45, 0xff, true, 2, 2); //45. at position 2, 3              
  display.showNumberDec(23, 0xff, true, 2, 4); //23. at postion 4, 5

Please post the code or say where you did that let you know the decimal points are working.

I only have TM1638 modules, which I always thought were basically the same but they are enough different that I was unable to advance your issue.

a7

Please, post the sketch that works for you without the decimal point but showing the HH MM SS. Are you using UNOR3?

I am not sure if the showNumberDec() method should be in the loop() function or not.

It shouldn't matter. The OP reports your sketch works except for the decimal points. Which is why I suggested 0xff which should turn on all decimal points. And why I want to see the code that did light them up.

In setup(), the functions run once and display a constant time.

In loop() the same lines would display the same time and refresh that time without visible differences thousand of times a second.

a7

@Dancopy

You may try the following sketch to see that decimal points do appear on the display:

#include <TM1637TinyDisplay6.h>

// TM1637 pins
#define CLK 2
#define DIO 3

TM1637TinyDisplay6 display(CLK, DIO);

void setup() 
{
  display.begin();
  display.setBrightness(7);     // 0–7
  //-----------------------------------------
   
   uint8_t segs[6];

  segs[0] = display.encodeDigit(1);                     // 1
  segs[1] = display.encodeDigit(2) | 0b10000000;        // 2 + DP
  segs[2] = display.encodeDigit(4);                     // 4
  segs[3] = display.encodeDigit(5) | 0b10000000;        // 5 + DP
  segs[4] = display.encodeDigit(2);                     // 2
  segs[5] = display.encodeDigit(3);                     // 3

  // Send to display
  display.setSegments(segs);
}

void loop() {)

Please, report if the display shows decimal points.

Your button code looks like a common debouncing pattern but is incorrect and does not function.

I see this alla time. By the time I had fixed it, it became another common pattern that does things slightly differently. It's the one I use eveywhere, so I'm not surprised it eventually emerged.

I never did exactly see why your version does not work. If AI wrote it, there's all you need to know. AI is good at making things that look plausible but have flaws. Life is too short to figure out how it went wrong.

Here's the hour changing button code I ended up with. It works to advance the hour and immediately display the updated time.

# define PRESSED LOW

void handleHourButton(unsigned long now) {
 if ((now - lastHourChangeMs) < DEBOUNCE_MS) return;  // too soon to look at button again

 bool isPressed = digitalRead(BTN_HOUR) == PRESSED;

 if (isPressed != lastHourBtnState) {
   if (isPressed) {
     incrementHour();
     updateDisplay();
   }

   lastHourChangeMs = now;  // don't look again for a bit
   lastHourBtnState = isPressed;
 }
}

The code for the other button can be the same with edits as appropriate.

Once you have a few buttons and repeated code, you should think about moving to a general button handler function that woukd deal with any button, or consider using a button library to just take care of it for you.

a7

@alto777 @Dancopy
For the benefit of OP, I am integrating your codes of #27 (along with BTN_MIN) with OP's sketch of #1. Now, OP can try the following complete sketch.

#include <TM1637TinyDisplay6.h>

#define CLK 2
#define DIO 3

#define BTN_HOUR 4
#define BTN_MIN  5

TM1637TinyDisplay6 display(CLK, DIO);

uint8_t hours   = 12;  //0C = 0001 1100
uint8_t minutes = 0;
uint8_t seconds = 0;

unsigned long lastTickMs = 0;
const unsigned long DEBOUNCE_MS = 200;

bool lastHourBtnState = true; //HIGH; bool returns true and NOT HIGH
bool lastMinBtnState  = true;; //HIGH;

unsigned long lastHourChangeMs = 0;
unsigned long lastMinChangeMs  = 0;
# define PRESSED LOW

void setup()
{
  display.begin();
  display.setBrightness(7);  // 0–7 (7 = mais brilhante)

  pinMode(BTN_HOUR, INPUT_PULLUP);
  pinMode(BTN_MIN,  INPUT_PULLUP);

  updateDisplay();
}

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

  // --- 1) Timekeeping: tick every 1000 ms ---
  if (nowTime - lastTickMs >= 1000)
  {
    lastTickMs = nowTime;
    tickClock();

    updateDisplay();
  }

  // --- 2) Botão de ajuste de horas (incrementa horas) ---
  handleHourButton(nowTime);

  // --- 3) Botão de ajuste de minutos (incrementa minutos) ---
  handleMinuteButton(nowTime);
}

void tickClock()
{
  seconds++;
  if (seconds >= 60)
  {
    seconds = 0;
    minutes++;
    if (minutes >= 60)
    {
      minutes = 0;
      hours++;
      if (hours >= 24)
      {
        hours = 0;
      }
    }
  }
}

void handleMinuteButton(unsigned long nowTime)
{
  if ((nowTime - lastMinChangeMs) < DEBOUNCE_MS) return;  // too soon to look at button again

  bool isPressed = digitalRead(BTN_MIN) == PRESSED;

  if (isPressed != lastMinBtnState)
  {
    if (isPressed) 
    {
      incrementMinute();
      updateDisplay();
    }

    lastMinChangeMs = nowTime;  // don't look again for a bit
    lastMinBtnState = isPressed;
  }
}

void handleHourButton(unsigned long nowTime)
{
  if ((nowTime - lastHourChangeMs) < DEBOUNCE_MS) return;  // too soon to look at button again

  bool isPressed = digitalRead(BTN_HOUR) == PRESSED;

  if (isPressed != lastHourBtnState) //prevents re-triggering 
  {
    if (isPressed) 
    {
      incrementHour();
      updateDisplay();
    }

    lastHourChangeMs = nowTime;  // don't look again for a bit
    lastHourBtnState = isPressed;
  }
}

void incrementHour()
{
  hours = (hours + 1) % 24;
  // Opcional: faz o reset dos segundos ao configurar a hora
  // seconds = 0;
}

void incrementMinute()
{
  minutes = (minutes + 1) % 60;
  // Opcional: faz o reset dos segundos ao configurar a hora
  // seconds = 0;
}

// Atualiza o display de 6 dígitos para HHMMSS.
void updateDisplay()
{
  uint8_t segs[6];

  segs[0] = hours / 10; //storing decimal digit
  segs[1] = hours % 10;  //

  segs[2] = minutes / 10;
  segs[3] = minutes % 10;

  segs[4] = seconds / 10;
  segs[5] = seconds % 10;
  //-------------------------
  segs[0] = display.encodeDigit(segs[0])             // storing 7-segment code 
  segs[1] = display.encodeDigit(segs[1]) | 0b10000000;        // " + decimal point
  segs[2] = display.encodeDigit(segs[2]);                     // 
  segs[3] = display.encodeDigit(segs[3]) | 0b10000000;        // 
  segs[4] = display.encodeDigit(segs[4]);                     // 
  segs[5] = display.encodeDigit(segs[5]);                     // 

  // Send to display
  display.setSegments(segs);
}

Question @alto777
We have TM1367TinyDisplay.h file in the IDE and not the TM1367TinyDisplay6.h; there is no error/warning during compilation -- file is not found. Why?

What is the purpose of the above line? The lastHourBtnState has been intialized to true.

Are not the followig lines enough?

Using wokwi required moving to a four digit device, and dropping '6' from the library name and object name.

Then this exact function was the only change to @Dancopy's sketch from #1 to see the colon the device has:

void updateDisplay() {
  display.showNumberDec(hours, 0x80, true, 2, 0);
  display.showNumberDec(minutes, 0, true, 2, 2);
}

Curiously both 0x80 and 0x40, in either call to showNumberDec() lights up the one colon on the display; no value in either call makes any decimal points light up.

So your updateDisplay() should read

void updateDisplay() {
  display.showNumberDec(hours, 0, true, 2, 0);
  display.showNumberDec(minutes, 0, true, 2, 2);
  display.showNumberDec(minutes, 0, true, 2, 4);
}

Again with one (or more) of the first 0 in each of those three lines should be replaced with something non-zero. I'd first try

void updateDisplay() {
  display.showNumberDec(hours, 0x80, true, 2, 0);
  display.showNumberDec(minutes, 0, true, 2, 2);
  display.showNumberDec(minutes, 0, true, 2, 4);
}

Then try seeing what changing the next few lines does for you.

Without either a real or simulated six digit display, this is as far as I can go.

The landscape is littered with TM16XX libraries possibly worth investigating. I might do that if I had the same hardware, otherwise all I could do is post untested speculation.

a7

a7

What happened when you tried that?

a7

Cleaning up the mess this made on my desktop, I noticed a comment

// Library: "TM1637_RT"  https://GitHub.com/RobTillaart/TM1637_RT

which caught my eye in a flashy demo that blinked the colon on a display time of day.

If I ever, I will start with @robtillaart's stuff.

See

a7

I will test.

I would like to thank everyone very much for the great help!
Especially to @GolamMostafa and @alto777.

  1. Part of the code, sent by @GolamMostafa in 'post #26', worked with the decimal points.
  2. @GolamMostafa corrected the code, making the 2 buttons work correctly, which were not responding.
    Finally, in 'post #28' the complete code was sent by @GolamMostafa, working with the 2 buttons and the 2 decimal points but, with a difference:
    a) The first time, when the buttons are held down, the Hour and Minute advance quickly (my preference).

b) The last code (post #28), it is necessary to press the Hour and/or Minute buttons once for each advance, that is, if the button is held down, the time does not advance; It is necessary to release the button and press it again; that is, if the minute is at 00 and it is necessary to advance to 59, it is necessary to press the minute button 59 times (much more time-consuming).

I await a response to my last comment, in option "b".

This would appear to be a "luxury" feature.
Anyway, please post the complete working code that has put the displays in the state that you have just shown in the picture (post #35). It could help others.

The button handling codes of sketch #28 are from @alto777. Yes! you have to press and release the button to advance HH/MM fields. This is the logic @alto777 has implemented which I like; otherwise, there will be re-triggering. If you want quick advancing, then try the following sketch risking re-triggering.

#include <TM1637TinyDisplay6.h>

#define CLK 2
#define DIO 3

#define BTN_HOUR 4
#define BTN_MIN  5

TM1637TinyDisplay6 display(CLK, DIO);

uint8_t hours   = 12;  //0C = 0001 1100
uint8_t minutes = 0;
uint8_t seconds = 0;

unsigned long lastTickMs = 0;
const unsigned long DEBOUNCE_MS = 200;

bool lastHourBtnState = true; //HIGH; bool returns true and NOT HIGH
bool lastMinBtnState  = true;; //HIGH;

unsigned long lastHourChangeMs = 0;
unsigned long lastMinChangeMs  = 0;
# define PRESSED LOW

void setup()
{
  display.begin();
  display.setBrightness(7);  // 0–7 (7 = mais brilhante)

  pinMode(BTN_HOUR, INPUT_PULLUP);
  pinMode(BTN_MIN,  INPUT_PULLUP);

  updateDisplay();
}

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

  // --- 1) Timekeeping: tick every 1000 ms ---
  if (nowTime - lastTickMs >= 1000)
  {
    lastTickMs = nowTime;
    tickClock();

    updateDisplay();
  }

  // --- 2) Botão de ajuste de horas (incrementa horas) ---
  handleHourButton(nowTime);

  // --- 3) Botão de ajuste de minutos (incrementa minutos) ---
  handleMinuteButton(nowTime);
}

void tickClock()
{
  seconds++;
  if (seconds >= 60)
  {
    seconds = 0;
    minutes++;
    if (minutes >= 60)
    {
      minutes = 0;
      hours++;
      if (hours >= 24)
      {
        hours = 0;
      }
    }
  }
}

void handleMinuteButton(unsigned long nowTime)
{
  if ((nowTime - lastMinChangeMs) < DEBOUNCE_MS) return;  // too soon to look at button again

  bool isPressed = digitalRead(BTN_MIN) == PRESSED;

 //if (isPressed != lastMinBtnState)
  //{
    if (isPressed) 
    {
      incrementMinute();
      updateDisplay();
    }

    lastMinChangeMs = nowTime;  // don't look again for a bit
   // lastMinBtnState = isPressed;
  }
//}

void handleHourButton(unsigned long nowTime)
{
  if ((nowTime - lastHourChangeMs) < DEBOUNCE_MS) return;  // too soon to look at button again

  bool isPressed = digitalRead(BTN_HOUR) == PRESSED;

//  if (isPressed != lastHourBtnState) //prevents re-triggering 
  //{
    if (isPressed) 
    {
      incrementHour();
      updateDisplay();
    }

    lastHourChangeMs = nowTime;  // don't look again for a bit
   // lastHourBtnState = isPressed;
  }
//}

void incrementHour()
{
  hours = (hours + 1) % 24;
  // Opcional: faz o reset dos segundos ao configurar a hora
  // seconds = 0;
}

void incrementMinute()
{
  minutes = (minutes + 1) % 60;
  // Opcional: faz o reset dos segundos ao configurar a hora
  // seconds = 0;
}

// Atualiza o display de 6 dígitos para HHMMSS.
void updateDisplay()
{
  uint8_t segs[6];

  segs[0] = hours / 10; //storing decimal digit
  segs[1] = hours % 10;  //

  segs[2] = minutes / 10;
  segs[3] = minutes % 10;

  segs[4] = seconds / 10;
  segs[5] = seconds % 10;
  //-------------------------
  segs[0] = display.encodeDigit(segs[0])             // storing 7-segment code 
  segs[1] = display.encodeDigit(segs[1]) | 0b10000000;        // " + decimal point
  segs[2] = display.encodeDigit(segs[2]);                     // 
  segs[3] = display.encodeDigit(segs[3]) | 0b10000000;        // 
  segs[4] = display.encodeDigit(segs[4]);                     // 
  segs[5] = display.encodeDigit(segs[5]);                     // 

  // Send to display
  display.setSegments(segs);
}

Report the result.

Did you test this? I'm looking at it and I don't think it will work.

Normally I wouldn't just say that, I'd test it.

It looks awfully like you just crippled the button code, as you suggested would not hurt (but did not bother to test).

@Dancopy - saying you await a response is more likely to put us off than to motivate us to write more code for the code you already had written for you by AI.

It's time to shelve this project and start learning how to program; it will be some time (and some hard-ish work) before you can write an auto-repeat button handler. Or use one that some library provides.

a7

Please, see #29. I could not yet collect display unit to test the sketches.

@alto777

Sorry. Yes, that part was from @alto777.