Advertisement
Johanneszockt1

Untitled

Jun 27th, 2023
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.54 KB | None | 0 0
  1. /**
  2. Windmesser, Arduino Programm
  3. Name: windmesseer.ino
  4. Purpose: Counts the impulses from a reed contact and logs that data to the sd card
  5. Arduino IDE Version: 2.1.0
  6.  
  7. @author Johannes Röring
  8. @version 1.0.0 28/06/23
  9. */
  10.  
  11. /*
  12. --User Settings and Constants--
  13. this part may be edited by the user if needed
  14. */
  15. #define SET_RTC_DATE true //set this to true to initialize the RTC clock with the time of the code upload
  16.  
  17. //settings that can to be adjusted
  18. #define SERIAL_BAUD 9600
  19. #define REEDPIN 2 //io pin for reed contact (with exterenal 10k pullup resistor to vcc)
  20. #define LOGFILE_NAME "log.txt"
  21. #define RADIUS 10 // definiert den Rotorradius in cm
  22. #define DEBOUNCETIME 200 //debouncing interval
  23. #define INTERVAL 1000 //calculation interval in which the values get sent to serial
  24.  
  25. //constant numbers
  26. #define PI 3.1415926535897932384626433832795 // definiere PI
  27.  
  28. /*
  29. --Including and Setting up Libraries--
  30. !!!Dont edit below this point unless you know what you are doing!!!
  31. */
  32. //includes
  33. #include <RTClib.h>
  34. #include <ArduinoJson.h>
  35. #include <SPI.h>
  36. #include <SD.h>
  37.  
  38. //library declarations
  39. RTC_DS3231 rtc;
  40. File myFile;
  41.  
  42. /*
  43. --Variables for the code--
  44. */
  45. //variables
  46. volatile unsigned int contacts; // counter for reed contact pulses
  47. volatile unsigned long time_inbetween_interrupts; // time since the last interrupt trigger
  48. unsigned long lastmillis; // last timestamp that data was proceessed at
  49. unsigned int lastcontacts; // rolling counter of contacts
  50. StaticJsonDocument<200> doc; // space for the json array to fit into
  51.  
  52. /*
  53. --Setup Function--
  54. this Function runs only once at powerup and reset
  55. it initializes all relevant pieces before the cyclic operation starts
  56. */
  57. void setup() {
  58. //attatch interrupt for handling the reed contact
  59. attachInterrupt(digitalPinToInterrupt(REEDPIN), count, FALLING);
  60.  
  61. //start the serial interface (BAUD rate is defined at the top)
  62. Serial.begin(SERIAL_BAUD);
  63.  
  64. init_rtc(); //calls init function for rtc
  65. init_sd(); //calls init function for sd
  66. }
  67.  
  68. /*
  69. --Loop Function--
  70. this Function runs in a constant loop and does everything that is not done through interrupts
  71. */
  72. void loop() {
  73. //function runs if the time since the last run is greater than the configured interval
  74. if (lastmillis + INTERVAL <= millis()) {
  75. process_data();
  76. }
  77. }
  78.  
  79. /*
  80. --Interrupt routine for Reed contact--
  81. this Function runs every time the reed contact gets triggered
  82. it procecsses the impulse
  83. */
  84. void count() {
  85. static unsigned long last_interrupt_time = 0;
  86. unsigned long interrupt_time = millis();
  87. //skipping impulses if they happen to fast (debouncing)
  88. if (interrupt_time - last_interrupt_time > DEBOUNCETIME) {
  89. contacts++; // erhöht die gezählten Kontakte
  90. time_inbetween_interrupts = interrupt_time - last_interrupt_time;
  91. }
  92. last_interrupt_time = interrupt_time;
  93. }
  94.  
  95. /*
  96. --Processing Function for the Loop--
  97. this Function gets called from loop roughly every second
  98. it procecsses the counted data and writes it to serial and the sd card
  99. */
  100. void process_data() {
  101. //store time of running the function
  102. lastmillis = millis();
  103. DateTime now = rtc.now();
  104.  
  105. //date calculations
  106. uint32_t timestamp = now.unixtime();
  107.  
  108. //calculations
  109. float rpm = 1000.0 / time_inbetween_interrupts;
  110. float rs = (2 * PI * RADIUS) / time_inbetween_interrupts * 10; // Bahngeschwindigkeitsformel (https://www.leifiphysik.de/mechanik/kreisbewegung/grundwissen/bahngeschwindigkeit-und-winkelgeschwindigkeit)
  111. float ws = 0.5921 * rpm + 2.3654; // Windgeschwindigkeit wird berechnet
  112.  
  113. //serial outputs
  114. Serial.print("timestamp:"); Serial.print(String(timestamp)); Serial.print(",");
  115. Serial.print("rpm:"); Serial.print(String(rpm)); Serial.print(",");
  116. Serial.print("rs:"); Serial.print(String(rs)); Serial.print(",");
  117. Serial.print("ws:"); Serial.print(String(ws)); Serial.print(",");
  118. Serial.print("contacts:"); Serial.print(String(contacts)); Serial.print(",");
  119. Serial.print("delay:"); Serial.print(String(time_inbetween_interrupts)); Serial.println();
  120.  
  121. //json output
  122. doc["timestamp"] = timestamp;
  123. doc["rpm"] = rpm;
  124. doc["rs"] = rs;
  125. doc["ws"] = ws;
  126. doc["contacts"] = contacts;
  127. doc["delay"] = time_inbetween_interrupts;
  128.  
  129. //open the logfile
  130. myFile = SD.open(LOGFILE_NAME, FILE_WRITE);
  131.  
  132. // if the file opened okay, write to it:
  133. if (myFile) {
  134. serializeJson(doc, myFile);
  135. myFile.println(","); //add a newline after the json data
  136. // close the file:
  137. myFile.close();
  138. } else {
  139. // if the file didn't open, print an error:
  140. Serial.println("error opening logfile");
  141. }
  142.  
  143. //reset contact counter to 0
  144. contacts = 0;
  145. }
  146. /*
  147. --INIT Function for the RTC Module--
  148. this Function runs only once in the void setup
  149. it initializes the RTC Module
  150. */
  151. void init_rtc() {
  152. // set up rtc module
  153. if (! rtc.begin()) {
  154. Serial.println("Couldn't find RTC");
  155. Serial.println("halting program, reset to retry");
  156. Serial.flush();
  157. while (1);
  158. }
  159.  
  160. //if this is the first time you use the RTC module you can set the time of the module with this function,
  161. //disable afterwards in the #define of SET_RTC_DATE
  162. if (SET_RTC_DATE) {
  163. adjust_rtc_date();
  164. }
  165. }
  166.  
  167.  
  168. /*
  169. --Setting Function for the RTC Module--
  170. this Function runs only once in the void setup if the flag is set to true
  171. it initializes the RTC Module with the timestamp that was provided during compilation
  172. */
  173. void adjust_rtc_date() {
  174. // get the compile time
  175. DateTime compileTime = DateTime(F(__DATE__), F(__TIME__));
  176.  
  177. // define your time offset (for example, Berlin is UTC+2 or UTC+1 depending on daylight saving)
  178. int offsetHours = 2; // change this to your offset
  179.  
  180. // adjust the compile time to UTC
  181. DateTime UTCTime = compileTime.unixtime() - offsetHours * 3600;
  182.  
  183. // set the RTC time
  184. rtc.adjust(UTCTime);
  185. }
  186.  
  187.  
  188. /*
  189. --INIT Function for the SD Card--
  190. this Function runs only once in the void setup
  191. it initializes all the sd card module
  192. */
  193. void init_sd() {
  194. // set up sd card
  195. Serial.print("Initializing SD card...");
  196. if (!SD.begin(4)) {
  197. Serial.println("initialization failed!");
  198. Serial.println("halting program, reset to retry");
  199. while (1);
  200. }
  201. Serial.println("SD Card initialization done.");
  202. }
  203.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement