// simple project using Arduino UNO and Matrix LED Display MAX7219 with u8g2 library
// and RTC (real time clock) module to display time
// created by upir, 2023
//test
#include <Arduino.h>
#include <RTClib.h>
#include <TM1637.h>
RTC_DS1307 rtc; // set the real time clock module
//U8G2_MAX7219_32X8_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 11, /* data=*/ 12, /* cs=*/ 10, /* dc=*/ U8X8_PIN_NONE, /* reset=*/ U8X8_PIN_NONE);
const int CLK = 12;
const int DIO = 11;
TM1637 tm(CLK, DIO);
char time_string[30]; // string to hold the current time to be displayed
void setup(void) {
Serial.begin(9600);
pinMode(4, OUTPUT); // we are using pin 4 to power the real time clock module,
digitalWrite(4, HIGH); // since the +5V pin is already used for the display
pinMode(7, INPUT_PULLUP);
tm.init();
tm.set(BRIGHT_TYPICAL);
if (! rtc.begin()) { // start the RTC module
abort();
}
// following line sets the RTC to the date & time this sketch was compiled
// uncomment this when uploading to Arduino, otherwise the time will be 0:00 and not increasing
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
int getTime(DateTime time){
uint32_t unix = time.unixtime() + 946684800;
sprintf(time_string, "unix: %d\n", unix);
Serial.print(time_string);
double jdn = unix / double(86400.0) + 2440587.5;
double jsn = ((jdn - 2308805.27785) / 1.0274912517) - 0.000185185185185185 - 122349.0;
int jsn_floor = int(jsn);
int marsClock = (jsn - (double)jsn_floor) * 100000;
sprintf(time_string, "jsn: %d\n", jsn);
Serial.print(time_string);
return marsClock;
}
void loop(void) {
DateTime now = rtc.now(); // get current time
int currTime = getTime(now);
//unsigned long unixMilli = ((now.unixtime() + 946684800) * 1000 + milli_sec);
//sprintf(time_string, "currTime: %d\n", currTime);
//Serial.print(time_string);
int HourDis = now.hour(); //currTime[0]; //now.hour();
int MinuteDis = now.minute(); //currTime[1]; //now.minute();
if (digitalRead(7) == LOW) { // pressed da button
HourDis = now.day();
MinuteDis = now.month();
}
tm.display(0, (HourDis / 10) % 10);
tm.display(1, HourDis % 10);
tm.display(2, (MinuteDis / 10) % 10);
tm.display(3, MinuteDis % 10);
delay(1000); // wait 0.1 second
}