DokanBoy

Untitled

Oct 19th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #define DEFAULT_MONITOR_SPEED 115200
  2.  
  3. #define MISCORS_IN_MILLIS 1000
  4. #define MILLIS_IN_SEC 1000
  5. #define SECS_IN_MIN 60
  6. #define MINS_IN_HOUR 60
  7. #define HOURS_IN_DAY 24
  8.  
  9. #include <Arduino.h>
  10. #include <string.h>
  11. #include <math.h>
  12.  
  13. struct Time
  14. {
  15.   unsigned short int days;
  16.   byte hours;
  17.   byte mins;
  18.   byte secs;
  19.   unsigned short int millis;
  20. };
  21.  
  22. void tickTime(Time &time);
  23. String getFormattedTime(Time &time);
  24.  
  25. Time time = {0, 0, 0, 0};
  26. Time oldTime = time;
  27. unsigned long usTime;
  28.  
  29. void setup()
  30. {
  31.   Serial.begin(DEFAULT_MONITOR_SPEED);
  32.   usTime = micros();
  33. }
  34.  
  35. void loop()
  36. {
  37.   if (abs(micros() - usTime) >= MISCORS_IN_MILLIS)
  38.   {
  39.     tickTime(time);
  40.     usTime = micros();
  41.   }
  42.  
  43.   if (oldTime.secs != time.secs)
  44.   {
  45.     Serial.println(getFormattedTime(time));
  46.     oldTime = time;
  47.   }
  48. }
  49.  
  50. void tickTime(Time &time)
  51. {
  52.   time.millis += 1;
  53.  
  54.   if (time.hours >= HOURS_IN_DAY)
  55.   {
  56.     time.hours -= HOURS_IN_DAY;
  57.     time.days += 1;
  58.   }
  59.  
  60.   if (time.mins >= MINS_IN_HOUR)
  61.   {
  62.     time.mins -= MINS_IN_HOUR;
  63.     time.hours += 1;
  64.   }
  65.  
  66.   if (time.secs >= SECS_IN_MIN)
  67.   {
  68.     time.secs -= SECS_IN_MIN;
  69.     time.mins += 1;
  70.   }
  71.  
  72.   if (time.millis >= MILLIS_IN_SEC)
  73.   {
  74.     time.millis -= MILLIS_IN_SEC;
  75.     time.secs += 1;
  76.   }
  77. }
  78.  
  79. String getFormattedTime(Time &time)
  80. {
  81.   String formattedTime = "Timer: ";
  82.   formattedTime.concat(time.days);
  83.   formattedTime.concat("d : ");
  84.   formattedTime.concat(time.hours);
  85.   formattedTime.concat("h : ");
  86.   formattedTime.concat(time.mins);
  87.   formattedTime.concat("m : ");
  88.   formattedTime.concat(time.secs);
  89.   formattedTime.concat("s. ");
  90.   formattedTime.concat("System timer value: ");
  91.   formattedTime.concat(micros());
  92.   formattedTime.concat("us");
  93.  
  94.   return formattedTime;
  95. }
  96.  
Add Comment
Please, Sign In to add comment