Advertisement
Johanneszockt1

Untitled

Jun 27th, 2023
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. /*
  2. Master script combine functions
  3. -> Rotation measurement
  4. -> Time reading from RTC
  5. */
  6. //library includes
  7. #include <RTClib.h>
  8.  
  9. //library inits
  10. RTC_DS3231 rtc;
  11.  
  12. //test options
  13. #define SET_RTC_DATE true //set this to true to initialize the RTC clock with the time of the code upload
  14.  
  15. //settings that need to be adjusted
  16. #define RADIUS 10 // definiert den Rotorradius in cm
  17.  
  18. //static settings
  19. #define BAUD 9600
  20. #define REEDPIN 2 //io pin for reed contact (with exterenal 10k pullup resistor to vcc)
  21. #define DEBOUNCETIME 200 //debouncing interval
  22. #define INTERVAL 1000 //calculation interval in which the values get sent to serial
  23.  
  24.  
  25. //constant numbers
  26. #define PI 3.1415926535897932384626433832795 // definiere PI
  27.  
  28. //mapping arrays
  29. char daysOfTheWeek[7][12] = {
  30. "Sunday",
  31. "Monday",
  32. "Tuesday",
  33. "Wednesday",
  34. "Thursday",
  35. "Friday",
  36. "Saturday"
  37. };
  38.  
  39. //variables
  40. volatile unsigned int contacts;
  41. volatile unsigned long time_inbetween_interrupts; // Periodendauer in ms
  42. unsigned long lastmillis;
  43. unsigned int lastcontacts;
  44.  
  45.  
  46. void setup() {
  47. attachInterrupt(digitalPinToInterrupt(REEDPIN), count, FALLING); // setzt ISR fürs hochzählen
  48. Serial.begin(BAUD);
  49.  
  50. // set up rtc module
  51. if (! rtc.begin()) {
  52. Serial.println("Couldn't find RTC");
  53. Serial.println("halting program, reset to retry");
  54. Serial.flush();
  55. while (1);
  56. }
  57.  
  58. //if this is the first time you use the RTC module you can set the time of the module with this function,
  59. //disable afterwards in the #define of SET_RTC_DATE
  60. if(SET_RTC_DATE){
  61. // automatically sets the RTC to the date & time on PC this sketch was compiled
  62. rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  63. }
  64. }
  65.  
  66. void loop() {
  67. //function runs if the time since the last run is greater than the configured interval
  68. if (lastmillis + INTERVAL <= millis()) {
  69. //store time of running the function
  70. lastmillis = millis();
  71. DateTime now = rtc.now();
  72.  
  73. //date calculations
  74. uint32_t timestamp = now.unixtime());
  75. int year = now.year();
  76. int month = now.month();
  77. int day = now.month();
  78. int hour = now.month();
  79. int minute = now.month();
  80. int second = now.month();
  81.  
  82. //calculations
  83. float rpm = 1000.0 / time_inbetween_interrupts;
  84. float rs = (2 * PI * RADIUS) / time_inbetween_interrupts * 10; // Bahngeschwindigkeitsformel (https://www.leifiphysik.de/mechanik/kreisbewegung/grundwissen/bahngeschwindigkeit-und-winkelgeschwindigkeit)
  85. float ws = 0.5921 * rpm + 2.3654; // Windgeschwindigkeit wird berechnet
  86.  
  87. //serial outputs
  88. Serial.println("####################");
  89. Serial.print("rpm:"); Serial.print(String(rpm)); Serial.print(",");
  90. Serial.print("rs:"); Serial.print(String(rs)); Serial.print(",");
  91. Serial.print("ws:"); Serial.print(String(ws)); Serial.print(",");
  92. Serial.print("contacts:"); Serial.print(String(contacts)); Serial.print(",");
  93. Serial.print("delay:"); Serial.print(String(time_inbetween_interrupts)); Serial.println();
  94.  
  95.  
  96. Serial.print("timestamp:"); Serial.print(String(timestamp)); Serial.print(",");
  97. Serial.print("year:"); Serial.print(String(year)); Serial.print(",");
  98. Serial.print("month:"); Serial.print(String(month)); Serial.print(",");
  99. Serial.print("day:"); Serial.print(String(day)); Serial.print(",");
  100. Serial.print("hour:"); Serial.print(String(hour)); Serial.print(",");
  101. Serial.print("minute:");Serial.print(String(minute)); Serial.print(",");
  102. Serial.print("second:");Serial.print(String(second)); Serial.println();
  103.  
  104.  
  105. //reset contact counter to 0
  106. contacts = 0;
  107. printTime();
  108. }
  109. }
  110.  
  111. void printTime(){
  112. DateTime now = rtc.now();
  113. Serial.print("Date & Time: ");
  114. Serial.print(now.year(), DEC);
  115. Serial.print('/');
  116. Serial.print(now.month(), DEC);
  117. Serial.print('/');
  118. Serial.print(now.day(), DEC);
  119. Serial.print(" (");
  120. Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
  121. Serial.print(") ");
  122. Serial.print(now.hour(), DEC);
  123. Serial.print(':');
  124. Serial.print(now.minute(), DEC);
  125. Serial.print(':');
  126. Serial.println(now.second(), DEC);
  127. }
  128.  
  129. //interrupt routine defined in setup
  130. void count() {
  131. static unsigned long last_interrupt_time = 0;
  132. unsigned long interrupt_time = millis();
  133. //skipping impulses if they happen to fast (debouncing)
  134. if(interrupt_time - last_interrupt_time > DEBOUNCETIME){
  135. contacts++; // erhöht die gezählten Kontakte
  136. time_inbetween_interrupts = interrupt_time - last_interrupt_time;
  137. }
  138.  
  139. last_interrupt_time = interrupt_time;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement