Advertisement
Johanneszockt1

Untitled

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