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.
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?
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.
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);
// 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.
// 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 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.
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.