Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define DEFAULT_MONITOR_SPEED 115200
- #define MISCORS_IN_MILLIS 1000
- #define MILLIS_IN_SEC 1000
- #define SECS_IN_MIN 60
- #define MINS_IN_HOUR 60
- #define HOURS_IN_DAY 24
- #include <Arduino.h>
- #include <string.h>
- #include <math.h>
- struct Time
- {
- unsigned short int days;
- byte hours;
- byte mins;
- byte secs;
- unsigned short int millis;
- };
- void tickTime(Time &time);
- String getFormattedTime(Time &time);
- Time time = {0, 0, 0, 0};
- Time oldTime = time;
- unsigned long usTime;
- void setup()
- {
- Serial.begin(DEFAULT_MONITOR_SPEED);
- usTime = micros();
- }
- void loop()
- {
- if (abs(micros() - usTime) >= MISCORS_IN_MILLIS)
- {
- tickTime(time);
- usTime = micros();
- }
- if (oldTime.secs != time.secs)
- {
- Serial.println(getFormattedTime(time));
- oldTime = time;
- }
- }
- void tickTime(Time &time)
- {
- time.millis += 1;
- if (time.hours >= HOURS_IN_DAY)
- {
- time.hours -= HOURS_IN_DAY;
- time.days += 1;
- }
- if (time.mins >= MINS_IN_HOUR)
- {
- time.mins -= MINS_IN_HOUR;
- time.hours += 1;
- }
- if (time.secs >= SECS_IN_MIN)
- {
- time.secs -= SECS_IN_MIN;
- time.mins += 1;
- }
- if (time.millis >= MILLIS_IN_SEC)
- {
- time.millis -= MILLIS_IN_SEC;
- time.secs += 1;
- }
- }
- String getFormattedTime(Time &time)
- {
- String formattedTime = "Timer: ";
- formattedTime.concat(time.days);
- formattedTime.concat("d : ");
- formattedTime.concat(time.hours);
- formattedTime.concat("h : ");
- formattedTime.concat(time.mins);
- formattedTime.concat("m : ");
- formattedTime.concat(time.secs);
- formattedTime.concat("s. ");
- formattedTime.concat("System timer value: ");
- formattedTime.concat(micros());
- formattedTime.concat("us");
- return formattedTime;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement