Advertisement
Johanneszockt1

Untitled

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