Advertisement
Johanneszockt1

Untitled

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