Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <string.h>
- unsigned int day;
- byte hour, min, sec;
- unsigned short int mills;
- unsigned long mcsTime;
- void tick_timer()
- {
- mills += 1;
- // in range [0..23]
- if (hour >= 23)
- {
- hour -= 23;
- day += 1;
- }
- // in range [0..59]
- if (min >= 59)
- {
- min -= 59;
- hour += 1;
- }
- // in range [0..59]
- if (sec >= 59)
- {
- sec -= 59;
- min += 1;
- }
- // in range [0..999]
- if (mills >= 999)
- {
- mills -= 999;
- sec += 1;
- }
- }
- String get_format_time()
- {
- String timeStr = "Days: ";
- timeStr.concat(day);
- timeStr.concat(" Hours: ");
- timeStr.concat(hour);
- timeStr.concat(" Minutes: ");
- timeStr.concat(min);
- timeStr.concat(" Seconds: ");
- timeStr.concat(sec);
- timeStr.concat(" micros: ");
- timeStr.concat(mcsTime);
- return timeStr;
- }
- void setup()
- {
- Serial.begin(115200);
- mcsTime = micros();
- }
- byte oldSec = 0;
- void loop()
- {
- if (abs(micros() - mcsTime) >= 1000)
- {
- tick_timer();
- mcsTime = micros();
- if (oldSec == sec)
- {
- Serial.println(get_format_time());
- oldSec = sec;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement