Advertisement
Johanneszockt1

Untitled

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