#include <Arduino.h>
#include <U8g2lib.h>
#include "RTClib.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);
char time_string[10]; // string to hold the current time to be displayed
const int pirPin = 13;
void setup(void) {
u8g2.begin(); // begin function is required for u8g2 library
u8g2.setContrast(200); // set display contrast 0-255
pinMode(pirPin, INPUT);
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__)));
}
void loop(void) {
DateTime now = rtc.now(); // get current time
sprintf(time_string, "%d:%02d", now.hour(), now.minute()); // add hours, colon symbol and minutes into a string
u8g2.setFont(u8g2_font_t0_11b_tn); // choose a suitable font
u8g2.clearBuffer(); // clear the internal u8g2 memory
u8g2.sendBuffer();
u8g2.drawStr(2, 8, time_string); // draw the time string to the display
if (digitalRead(pirPin) == HIGH) {
u8g2.sendBuffer(); // transfer internal memory to the display
}
delay(1000); // wait one second
}