Advertisement
Johanneszockt1

Untitled

Jun 27th, 2023
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 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.  
  72. //calculations
  73. float rpm = 1000.0 / time_inbetween_interrupts;
  74. float rs = (2 * PI * RADIUS) / time_inbetween_interrupts * 10; // Bahngeschwindigkeitsformel (https://www.leifiphysik.de/mechanik/kreisbewegung/grundwissen/bahngeschwindigkeit-und-winkelgeschwindigkeit)
  75. float ws = 0.5921 * rpm + 2.3654; // Windgeschwindigkeit wird berechnet
  76.  
  77. //serial outputs
  78. Serial.println("####################");
  79. Serial.println("rpm:"+(float)rpm));
  80. //Serial.print("Drehzahl: ");
  81. //Serial.println((float)rpm);
  82.  
  83. Serial.print("Umdrehungsgeschwindigkeit: ");
  84. Serial.print((float)rs);
  85. Serial.println("m/s");
  86.  
  87. Serial.print("Kontakte: ");
  88. Serial.println(contacts);
  89.  
  90. Serial.print("Windgeschwindigkeit: ");
  91. Serial.print((float)ws);
  92. Serial.println("m/s");
  93.  
  94. Serial.println(time_inbetween_interrupts);
  95.  
  96. //reset contact counter to 0
  97. contacts = 0;
  98. test();
  99. }
  100. }
  101.  
  102. void printTime(){
  103. DateTime now = rtc.now();
  104. Serial.print("Date & Time: ");
  105. Serial.print(now.year(), DEC);
  106. Serial.print('/');
  107. Serial.print(now.month(), DEC);
  108. Serial.print('/');
  109. Serial.print(now.day(), DEC);
  110. Serial.print(" (");
  111. Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
  112. Serial.print(") ");
  113. Serial.print(now.hour(), DEC);
  114. Serial.print(':');
  115. Serial.print(now.minute(), DEC);
  116. Serial.print(':');
  117. Serial.println(now.second(), DEC);
  118. }
  119.  
  120. //interrupt routine defined in setup
  121. void count() {
  122. static unsigned long last_interrupt_time = 0;
  123. unsigned long interrupt_time = millis();
  124. //skipping impulses if they happen to fast (debouncing)
  125. if(interrupt_time - last_interrupt_time > DEBOUNCETIME){
  126. contacts++; // erhöht die gezählten Kontakte
  127. time_inbetween_interrupts = interrupt_time - last_interrupt_time;
  128. }
  129.  
  130. last_interrupt_time = interrupt_time;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement