PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

110 lines
3.1KB

  1. /*
  2. * TimeRTCLogger.ino
  3. * example code illustrating adding and subtracting Time.
  4. *
  5. * this sketch logs pin state change events
  6. * the time of the event and time since the previous event is calculated and sent to the serial port.
  7. */
  8. #include <TimeLib.h>
  9. #include <Wire.h>
  10. #include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
  11. const int nbrInputPins = 6; // monitor 6 digital pins
  12. const int inputPins[nbrInputPins] = {2,3,4,5,6,7}; // pins to monitor
  13. boolean state[nbrInputPins] ; // the state of the monitored pins
  14. time_t prevEventTime[nbrInputPins] ; // the time of the previous event
  15. void setup() {
  16. Serial.begin(9600);
  17. setSyncProvider(RTC.get); // the function to sync the time from the RTC
  18. for (int i=0; i < nbrInputPins; i++) {
  19. pinMode( inputPins[i], INPUT);
  20. // uncomment these lines if pull-up resistors are wanted
  21. // pinMode( inputPins[i], INPUT_PULLUP);
  22. // state[i] = HIGH;
  23. }
  24. }
  25. void loop()
  26. {
  27. for (int i=0; i < nbrInputPins; i++) {
  28. boolean val = digitalRead(inputPins[i]);
  29. if (val != state[i]) {
  30. time_t duration = 0; // the time since the previous event
  31. state[i] = val;
  32. time_t timeNow = now();
  33. if (prevEventTime[i] > 0) {
  34. // if this was not the first state change, calculate the time from the previous change
  35. duration = timeNow - prevEventTime[i];
  36. }
  37. logEvent(inputPins[i], val, timeNow, duration ); // log the event
  38. prevEventTime[i] = timeNow; // store the time for this event
  39. }
  40. }
  41. }
  42. void logEvent( int pin, boolean state, time_t timeNow, time_t duration)
  43. {
  44. Serial.print("Pin ");
  45. Serial.print(pin);
  46. if (state == HIGH) {
  47. Serial.print(" went High at ");
  48. } else {
  49. Serial.print(" went Low at ");
  50. }
  51. showTime(timeNow);
  52. if (duration > 0) {
  53. // only display duration if greater than 0
  54. Serial.print(", Duration was ");
  55. showDuration(duration);
  56. }
  57. Serial.println();
  58. }
  59. void showTime(time_t t)
  60. {
  61. // display the given time
  62. Serial.print(hour(t));
  63. printDigits(minute(t));
  64. printDigits(second(t));
  65. Serial.print(" ");
  66. Serial.print(day(t));
  67. Serial.print(" ");
  68. Serial.print(month(t));
  69. Serial.print(" ");
  70. Serial.print(year(t));
  71. }
  72. void printDigits(int digits){
  73. // utility function for digital clock display: prints preceding colon and leading 0
  74. Serial.print(":");
  75. if(digits < 10)
  76. Serial.print('0');
  77. Serial.print(digits);
  78. }
  79. void showDuration(time_t duration)
  80. {
  81. // prints the duration in days, hours, minutes and seconds
  82. if (duration >= SECS_PER_DAY) {
  83. Serial.print(duration / SECS_PER_DAY);
  84. Serial.print(" day(s) ");
  85. duration = duration % SECS_PER_DAY;
  86. }
  87. if (duration >= SECS_PER_HOUR) {
  88. Serial.print(duration / SECS_PER_HOUR);
  89. Serial.print(" hour(s) ");
  90. duration = duration % SECS_PER_HOUR;
  91. }
  92. if (duration >= SECS_PER_MIN) {
  93. Serial.print(duration / SECS_PER_MIN);
  94. Serial.print(" minute(s) ");
  95. duration = duration % SECS_PER_MIN;
  96. }
  97. Serial.print(duration);
  98. Serial.print(" second(s) ");
  99. }