Simple clock, how hard can it be?

Well, too hard for me, obviously...

I have a Nano, DS1302 RTC and a TM1637 display. I also have a light sensor (light sensitive resistor with pull up) but I won't both you with that, because that part works. I have everything hooked up and wrote / nicked / plagiarised this code:

// TravelClock with brightness controlled by the ambient light and H+ and M+ buttons to set the time.

#include <Arduino.h>
#include <TM1637Display.h>
#include <virtuabotixRTC.h> //

// RTC module declaration, (CLK,DAT,RST)
virtuabotixRTC MyRTC(A2,A1,A3);

// 4digit display connection pins (Digital Pins)
#define CLK 4
#define DIO 5
// 4 digit display declaration
TM1637Display display(CLK, DIO);

// brightness control set-up
int lightSens = A7;
int brightness;

void setup() {
  
// attachInterrupt(digitalPinToInterrupt(2),Press_A_Button,RISING);
// attachInterrupt(digitalPinToInterrupt(3),Press_B_Button,RISING);

// Set display bright to start with
  display.setBrightness(7);
}

// void
// H-Button(){

// }

// void
// M-Button(){

// }

void loop() {  

brightness = analogRead(lightSens);
Serial.println(brightness);
display.setBrightness(map(brightness,1,900,0,7)); // 1 is dark, 900 is light  

 MyRTC.updateTime();

  // Get the current time
  int hour = MyRTC.hours;
  int minute = MyRTC.minutes;
  int second = MyRTC.seconds;

  // Create a variable to hold the time in 24-hour format
  uint8_t displayTime[] = {hour / 10, hour % 10, minute / 10, minute % 10};

  // Display the time on the TM1637
  display.showNumberDecEx(displayTime[0] * 1000 + displayTime[1] * 100 + displayTime[2] * 10 + displayTime[3], 0x80 >> (second % 2));

  // Wait for a second before updating the display
  delay(1000);

}

And now the display shows the first two digits empty, and then ":63". Which is very creative, but not what I had in mind. I am getting better at simple code like 'if this, than that', but this has me stumped. Please advise / teach me.

Thanks,

Hugo

Print to serial monitor the raw values:

Serial.println(displayTime[0]);
Serial.println(displayTime[1]);
Serial.println(displayTime[2]);
Serial.println(displayTime[3]);

If valid, these are not the data for which you are looking.

Try changing this to uint16_t

Thanks for helping.

I've added the lines (and started the serial) and I get weird results. I get 5 numbers each cycle (I slowed down to 3 seconds per cycle using the delay).

xx
0
0
6
3

And xx varies, lowest I've seen is 16, heighest 19. Does that make sense?

Hugo

Changing uint8_t displayTime[] to uint16_t displayTime[] didn't change results.

Is there some way I can serial print the raw input from the RTC, and make sense of it?

You should only get four values, one for each place. Also, looks like 6 is a decimal, and needs to be modulus 6.. or the last two need to be modulus 60.

Right under here, serial.print the values of hour minute and second.

Parts of your code seem more complex than they need to be. Try this:

  // Create a variable to hold the time in 24-hour format
  uint16_t displayTime = hour * 100 + minute;

  // Display the time on the TM1637
  display.showNumberDecEx(displayTime, 0x80 >> (second % 2), true);

What, you think you are some kind of Jedi, waving your hand around like that? Mind tricks don't work on me. Only money!

2 Likes

I've added this:

// Serial print values to check
Serial.println(MyRTC.hours);
Serial.println(MyRTC.minutes);
Serial.println(MyRTC.seconds);

And despite asking three lines, I get four:
6/7/8 (either one of the three)
0
63
63

So, my guess is the RTC is not doing what it is supposed to. I am running it without the battery, that worked without the battery in a different project. And I have no battery handy at the moment.

Hugo

Changing to this code:

// Create a variable to hold the time in 24-hour format
  uint16_t displayTime = hour * 100 + minute;

  // Display the time on the TM1637
  display.showNumberDecEx(displayTime, 0x80 >> (second % 2), true);

didn't change the outcome.

Hugo

What display do you want? 2 digits, a colon, then 2 digits?
display.showNumberDecEx(displayTime, 64, true);
The second number determines the format. 0b01000000 is 64.

//! For displays with just a colon:
//! * 00:00 (0b01000000)
TM1637/TM1637Display.h at master · avishorp/TM1637 · GitHub

I'll try that code next. In the meantime I found this one:

And tried that code. Same outcome, now with the zeros: 00:63.
And I tried to swap Data and Clock of the RTC, so pins A1 and A2. And nothing changed. So, My feeling the RTC is not TRC-ing is growing steadily. Unfortunately I haven't got a spare, so I guess I need to order that.

Hugo

1 Like

Just before you send it to the display, send it to the serial port.
Serial.println(displayTime);
Does it show the correct time without the colon?

Nope. Nothing works and I am a strong believer the RTC is faulty.

in my sketch the RTC is optional. the time is run with the TimeLib and set with the encoder. RTC is only a backup if the MCU is not powered

Order a better one this time, based on ds3231 chip. Not much difference in price.

Is your display common anode or common cathode, and is your program addressing it as such?