Advertisement
Johanneszockt1

Untitled

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