Projektarbeit Datalogger
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Teensy4.1_Datalogger new.ino 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // Visual Micro is in vMicro>General>Tutorial Mode
  2. //
  3. /*
  4. Name: Teensy4.1_Datalogger new.ino
  5. Created: 03.05.2022 12:04:32
  6. Author: GAMINGMASHEEN\Julian Graf
  7. */
  8. #include <SdFat.h>
  9. #include <TimeLib.h>
  10. #include <Bounce.h>
  11. #define SD_FAT_TYPE 3
  12. #ifndef SDCARD_SS_PIN
  13. const uint8_t SD_CS_PIN = SS;
  14. #else // SDCARD_SS_PIN
  15. // Assume built-in SD is used.
  16. const uint8_t SD_CS_PIN = SDCARD_SS_PIN;
  17. #endif // SDCARD_SS_PIN
  18. #if HAS_SDIO_CLASS
  19. #define SD_CONFIG SdioConfig(FIFO_SDIO)
  20. #elif ENABLE_DEDICATED_SPI
  21. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI)
  22. #else // HAS_SDIO_CLASS
  23. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI)
  24. #endif // HAS_SDIO_CLASS
  25. #if SD_FAT_TYPE == 0
  26. SdFat sd;
  27. File file;
  28. #elif SD_FAT_TYPE == 1
  29. SdFat32 sd;
  30. File32 file;
  31. #elif SD_FAT_TYPE == 2
  32. SdExFat sd;
  33. ExFile file;
  34. #elif SD_FAT_TYPE == 3
  35. SdFs sd;
  36. FsFile file;
  37. #else // SD_FAT_TYPE
  38. #error Invalid SD_FAT_TYPE
  39. #endif // SD_FAT_TYPE
  40. // Define User Types below here or use a .h file
  41. //
  42. const char software_name[] = "Software: Teensy_datalog V.2";
  43. const int min_voltage_batterie = 13;
  44. const int fixed_resistor_temperatur = 500;
  45. const int power_Temp_sensor = 34, power_Windfahne = 36, LED_Fail = 24,
  46. LED_Write = 5, LED_Normal = 6, LED_Batterie = 7,
  47. taster_manuell_speichern = 28, Windfahne = 20, T_sensor_input = 17, Batterie_input = 38;
  48. int last_second, last_minute, last_hour;
  49. struct calculations {
  50. private:
  51. float summ;
  52. float square_summ;
  53. float cubic_summ;
  54. public:
  55. void calculate(float speed_per_second[60], int amount_saved) {
  56. summ = 0;
  57. square_summ = 0;
  58. cubic_summ = 0;
  59. for (int i = 0; i < amount_saved; i++) {
  60. summ = summ + speed_per_second[i];
  61. square_summ = square_summ + pow(speed_per_second[i], 2);
  62. cubic_summ = cubic_summ + pow(speed_per_second[i], 3);
  63. }
  64. arithmetic_mean = summ / float(amount_saved);
  65. square_mean = pow((square_summ / float(amount_saved)), (1 / 2.0));
  66. cubic_mean = pow((cubic_mean / float(amount_saved)), (1 / 3.0));
  67. summ = 0;
  68. square_summ = 0;
  69. cubic_summ = 0;
  70. for (int i = 0; i < amount_saved; i++) {
  71. summ = summ + pow((speed_per_second[i] - arithmetic_mean), 2);
  72. square_summ = square_summ + pow((speed_per_second[i] - square_mean), 2);
  73. cubic_summ = cubic_summ + pow((speed_per_second[i] - cubic_mean), 2);
  74. }
  75. arithmetic_deviation = pow((summ / float(amount_saved - 1)), (1 / 2.0));
  76. square_deviation = pow((square_summ / float(amount_saved - 1)), (1 / 2.0));
  77. cubic_deviation = pow((cubic_summ / float(amount_saved - 1)), (1 / 2.0));
  78. }
  79. float arithmetic_mean;
  80. float arithmetic_deviation;
  81. float square_mean;
  82. float square_deviation;
  83. float cubic_mean;
  84. float cubic_deviation;
  85. };
  86. struct anemomenter_maessurement {
  87. public:
  88. int pin = 0;
  89. int seconds_saved = 0;
  90. int minutes_saved = 0;
  91. void meassure() {
  92. if (reed_contact.update() && reed_contact.fallingEdge()) {
  93. count_per_second++;
  94. }
  95. }
  96. void save_wind_speed() {
  97. wind_speed_per_second[seconds_saved] = 0.4 * count_per_second;
  98. seconds_saved++;
  99. }
  100. void calculate() {
  101. values[minutes_saved].calculate(wind_speed_per_second, seconds_saved);
  102. seconds_saved = 0;
  103. minutes_saved++;
  104. }
  105. void print() {
  106. }
  107. private:
  108. int count_per_second = 0;
  109. int saved_seconds = 0;
  110. int saved_minutes = 0;
  111. float wind_speed_per_second[60];
  112. calculations values[60];
  113. Bounce reed_contact = Bounce(pin, 10);
  114. }anemometer_1, anemometer_2, anemometer_3;
  115. // Define Function Prototypes that use User Types below here or use a .h file
  116. //
  117. // Define Functions below here or use other .ino or cpp files
  118. //
  119. void dateTime(uint16_t* date, uint16_t* time, uint8_t* ms10) {
  120. // Return date using FS_DATE macro to format fields.
  121. *date = FS_DATE(year(), month(), day());
  122. // Return time using FS_TIME macro to format fields.
  123. *time = FS_TIME(hour(), minute(), second());
  124. // Return low time bits in units of 10 ms.
  125. *ms10 = second() & 1 ? 100 : 0;
  126. }
  127. void write_sd() {
  128. char file_name[50];
  129. FsDateTime::setCallback(dateTime);
  130. sprintf(file_name, "Windmessmast-%d.%d.%d_%d:%d.txt", year(), month(), day(), hour(), minute());
  131. sd.begin(SD_CONFIG);
  132. if (file.open(file_name, FILE_WRITE)) {
  133. Serial.println("Start SD schreiben");
  134. file.println("Messdaten von Windmessmasst");
  135. file.println();
  136. file.println("Data logger : Teensy 4.2");
  137. file.println(software_name);
  138. file.println();
  139. file.println("");
  140. }
  141. }
  142. void every_second() {
  143. anemometer_1.save_wind_speed();
  144. anemometer_2.save_wind_speed();
  145. anemometer_3.save_wind_speed();
  146. last_second = second();
  147. }
  148. void every_minute() {
  149. anemometer_1.calculate();
  150. anemometer_2.calculate();
  151. anemometer_3.calculate();
  152. last_minute = minute();
  153. }
  154. void every_hour() {
  155. last_hour = hour();
  156. }
  157. // The setup() function runs once each time the micro-controller starts
  158. void setup()
  159. {
  160. //set input and output
  161. pinMode(Windfahne, INPUT);
  162. pinMode(Batterie_input, INPUT);
  163. pinMode(T_sensor_input, INPUT);
  164. pinMode(taster_manuell_speichern, INPUT);
  165. pinMode(LED_Write, OUTPUT);
  166. pinMode(LED_Fail, OUTPUT);
  167. pinMode(LED_Normal, OUTPUT);
  168. pinMode(LED_Batterie, OUTPUT);
  169. pinMode(power_Temp_sensor, OUTPUT);
  170. pinMode(power_Windfahne, OUTPUT);
  171. setSyncProvider((getExternalTime)Teensy3Clock.get());
  172. Serial.begin(9600);
  173. Serial.println("Teensy 4.1-Datalogger gestartet");
  174. if (timeStatus() != timeSet) {
  175. Serial.println("Fehler bei Synchronisieren der Uhrzeit mit der RTC");
  176. digitalWrite(LED_Fail, HIGH);
  177. return;
  178. }
  179. Serial.println("Uhrzeit erfolgreich mit der RTC synchronisiert");
  180. if (!sd.begin(SD_CONFIG)) {
  181. digitalWrite(LED_Fail, HIGH);
  182. sd.initErrorHalt(&Serial);
  183. }
  184. anemometer_1.pin = 2;
  185. anemometer_2.pin = 9;
  186. anemometer_3.pin = 22;
  187. Serial.println("Messung startet");
  188. last_second = second();
  189. while (last_second == second()) {};
  190. last_second = second();
  191. last_minute = minute();
  192. last_hour = hour();
  193. }
  194. // Add the main program code into the continuous loop() function
  195. void loop()
  196. {
  197. anemometer_1.meassure();
  198. anemometer_2.meassure();
  199. anemometer_3.meassure();
  200. if (second() != last_second) {
  201. if (minute() != last_minute) {
  202. if (hour() != last_hour) {
  203. every_hour();
  204. }
  205. every_minute();
  206. }
  207. every_second();
  208. }
  209. }