Advertisement
DokanBoy

Untitled

Oct 27th, 2020
755
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <string.h>
  3.  
  4. unsigned int day;
  5. byte hour, min, sec;
  6. unsigned short int mills;
  7. unsigned long mcsTime;
  8.  
  9. void tick_timer()
  10. {
  11.   mills += 1;
  12.  
  13.   // in range [0..23]
  14.   if (hour >= 23)
  15.   {
  16.     hour -= 23;
  17.     day += 1;
  18.   }
  19.  
  20.   // in range [0..59]
  21.   if (min >= 59)
  22.   {
  23.     min -= 59;
  24.     hour += 1;
  25.   }
  26.  
  27.   // in range [0..59]
  28.   if (sec >= 59)
  29.   {
  30.     sec -= 59;
  31.     min += 1;
  32.   }
  33.  
  34.   // in range [0..999]
  35.   if (mills >= 999)
  36.   {
  37.     mills -= 999;
  38.     sec += 1;
  39.   }
  40. }
  41.  
  42. String get_format_time()
  43. {
  44.   String timeStr = "Days: ";
  45.   timeStr.concat(day);
  46.   timeStr.concat(" Hours: ");
  47.   timeStr.concat(hour);
  48.   timeStr.concat(" Minutes: ");
  49.   timeStr.concat(min);
  50.   timeStr.concat(" Seconds: ");
  51.   timeStr.concat(sec);
  52.   timeStr.concat(" Micros: ");
  53.   timeStr.concat(mcsTime);
  54.  
  55.   return timeStr;
  56. }
  57.  
  58. void setup()
  59. {
  60.   Serial.begin(115200);
  61.   mcsTime = micros();
  62. }
  63.  
  64. byte oldSec = 0;
  65. void loop()
  66. {
  67.   if (abs(micros() - mcsTime) >= 1000)
  68.   {
  69.     tick_timer();
  70.     mcsTime = micros();
  71.  
  72.     if (oldSec != sec)
  73.     {
  74.       Serial.println(get_format_time());
  75.       oldSec = sec;
  76.     }
  77.   }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement