The following might give you some ideas of what is possible.
Note I have compiled it but NOT RUN/TESTED it!
Code:
// Visual Micro is in vMicro>General>Tutorial Mode
//
/*
Name: t4.ino
Created: 14/12/2022 19:50:52
Author:
*/
#include <RV-3028-C7.h>
RV3028 rtc;
typedef struct timeStampType {
uint32_t unixSeconds;
uint32_t microSeconds;
};
timeStampType timeStamp;
uint32_t unixTime;
elapsedMillis secondsX1000;
elapsedMicros microSeconds;
constexpr int clkPin = 42; // some pin
volatile uint32_t currentCount = 0; // volatile since it is changed in an interrupt routine
void onClock()
{
microSeconds = 0;
}
void setup()
{
Serial.begin(9);
while (!Serial && millis() < 5000);
Wire.begin();
if (rtc.begin() == false) {
Serial.println("Something went wrong, check wiring");
while (1);
}
else
Serial.println("RTC online!");
pinMode(clkPin, INPUT_PULLUP);
attachInterrupt(clkPin, onClock, RISING);
rtc.enablePeriodicUpdateInterrupt(true, true);
unixTime = rtc.getUNIX();
secondsX1000 = 0; // Not really necessary
}
timeStampType GetTimeStamp() {
timeStampType ts;
ts.microSeconds = microSeconds;
ts.unixSeconds = unixTime + (secondsX1000 / 1000);
return ts;
}
bool somethingHappened;
// Add the main program code into the continuous loop() function
void loop()
{
if (somethingHappened)
timeStamp = GetTimeStamp();
}