Advertisement
Johanneszockt1

Untitled

Jun 27th, 2023
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 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.json"
  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. //open the logfile
  92. myFile = SD.open(LOGFILE_NAME, FILE_WRITE);
  93.  
  94. // if the file opened okay, write to it:
  95. if (myFile) {
  96. Serial.print("Writing to logfile");
  97. myFile.println("testing 1, 2, 3.");
  98. // close the file:
  99. myFile.close();
  100. Serial.println("done.");
  101. } else {
  102. // if the file didn't open, print an error:
  103. Serial.println("error opening logfile");
  104. }
  105. while(1);
  106. }
  107.  
  108. void loop() {
  109. //function runs if the time since the last run is greater than the configured interval
  110. if (lastmillis + INTERVAL <= millis()) {
  111. //store time of running the function
  112. lastmillis = millis();
  113. DateTime now = rtc.now();
  114.  
  115. //date calculations
  116. uint32_t timestamp = now.unixtime();
  117.  
  118. //calculations
  119. float rpm = 1000.0 / time_inbetween_interrupts;
  120. float rs = (2 * PI * RADIUS) / time_inbetween_interrupts * 10; // Bahngeschwindigkeitsformel (https://www.leifiphysik.de/mechanik/kreisbewegung/grundwissen/bahngeschwindigkeit-und-winkelgeschwindigkeit)
  121. float ws = 0.5921 * rpm + 2.3654; // Windgeschwindigkeit wird berechnet
  122.  
  123. //serial outputs
  124. Serial.print("timestamp:"); Serial.print(String(timestamp)); Serial.print(",");
  125. Serial.print("rpm:"); Serial.print(String(rpm)); Serial.print(",");
  126. Serial.print("rs:"); Serial.print(String(rs)); Serial.print(",");
  127. Serial.print("ws:"); Serial.print(String(ws)); Serial.print(",");
  128. Serial.print("contacts:"); Serial.print(String(contacts)); Serial.print(",");
  129. Serial.print("delay:"); Serial.print(String(time_inbetween_interrupts)); Serial.println();
  130.  
  131. //json output
  132. doc["timestamp"] = timestamp;
  133. doc["rpm"] = rpm;
  134. doc["rs"] = rs;
  135. doc["ws"] = ws;
  136. doc["contacts"] = contacts;
  137. doc["delay"] = time_inbetween_interrupts;
  138.  
  139. // serialize json and send it via the Serial interface
  140. serializeJson(doc, Serial);
  141. Serial.println();
  142.  
  143. //open the logfile
  144. myFile = SD.open(LOGFILE_NAME, FILE_WRITE);
  145.  
  146. // if the file opened okay, write to it:
  147. if (myFile) {
  148. Serial.print("Writing to logfile");
  149. myFile.println("testing 1, 2, 3.");
  150. // close the file:
  151. myFile.close();
  152. Serial.println("done.");
  153. } else {
  154. // if the file didn't open, print an error:
  155. Serial.println("error opening logfile");
  156. }
  157.  
  158. //reset contact counter to 0
  159. contacts = 0;
  160. }
  161. }
  162.  
  163. //interrupt routine defined in setup
  164. void count() {
  165. static unsigned long last_interrupt_time = 0;
  166. unsigned long interrupt_time = millis();
  167. //skipping impulses if they happen to fast (debouncing)
  168. if (interrupt_time - last_interrupt_time > DEBOUNCETIME) {
  169. contacts++; // erhöht die gezählten Kontakte
  170. time_inbetween_interrupts = interrupt_time - last_interrupt_time;
  171. }
  172.  
  173. last_interrupt_time = interrupt_time;
  174. }
  175.  
  176.  
  177.  
  178. /* graveyard
  179. int year = now.year();
  180. int month = now.month();
  181. int day = now.month();
  182. int hour = now.month();
  183. int minute = now.month();
  184. int second = now.month()
  185.  
  186. Serial.print("year:"); Serial.print(String(year)); Serial.print(",");
  187. Serial.print("month:"); Serial.print(String(month)); Serial.print(",");
  188. Serial.print("day:"); Serial.print(String(day)); Serial.print(",");
  189. Serial.print("hour:"); Serial.print(String(hour)); Serial.print(",");
  190. Serial.print("minute:");Serial.print(String(minute)); Serial.print(",");
  191. Serial.print("second:");Serial.print(String(second)); Serial.println();
  192. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement