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 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. private:
  106. int count_per_second = 0;
  107. int saved_seconds = 0;
  108. int saved_minutes = 0;
  109. float wind_speed_per_second[60];
  110. calculations values[60];
  111. Bounce reed_contact = Bounce(pin, 10);
  112. }anemometer_1, anemometer_2, anemometer_3;
  113. // Define Function Prototypes that use User Types below here or use a .h file
  114. //
  115. // Define Functions below here or use other .ino or cpp files
  116. //
  117. void every_second() {
  118. anemometer_1.save_wind_speed();
  119. anemometer_2.save_wind_speed();
  120. anemometer_3.save_wind_speed();
  121. last_second = second();
  122. }
  123. void every_minute() {
  124. anemometer_1.calculate();
  125. anemometer_2.calculate();
  126. anemometer_3.calculate();
  127. last_minute = minute();
  128. }
  129. void every_hour() {
  130. last_hour = hour();
  131. }
  132. // The setup() function runs once each time the micro-controller starts
  133. void setup()
  134. {
  135. //set input and output
  136. pinMode(Windfahne, INPUT);
  137. pinMode(Batterie_input, INPUT);
  138. pinMode(T_sensor_input, INPUT);
  139. pinMode(taster_manuell_speichern, INPUT);
  140. pinMode(LED_Write, OUTPUT);
  141. pinMode(LED_Fail, OUTPUT);
  142. pinMode(LED_Normal, OUTPUT);
  143. pinMode(LED_Batterie, OUTPUT);
  144. pinMode(power_Temp_sensor, OUTPUT);
  145. pinMode(power_Windfahne, OUTPUT);
  146. setSyncProvider((getExternalTime)Teensy3Clock.get());
  147. Serial.begin(9600);
  148. Serial.println("Teensy 4.1-Datalogger gestartet");
  149. if (timeStatus() != timeSet) {
  150. Serial.println("Fehler bei Synchronisieren der Uhrzeit mit der RTC");
  151. digitalWrite(LED_Fail, HIGH);
  152. return;
  153. }
  154. Serial.println("Uhrzeit erfolgreich mit der RTC synchronisiert");
  155. if (!sd.begin(SD_CONFIG)) {
  156. digitalWrite(LED_Fail, HIGH);
  157. sd.initErrorHalt(&Serial);
  158. }
  159. anemometer_1.pin = 2;
  160. anemometer_2.pin = 9;
  161. anemometer_3.pin = 22;
  162. Serial.println("Messung startet");
  163. last_second = second();
  164. while (last_second == second()) {};
  165. last_second = second();
  166. last_minute = minute();
  167. last_hour = hour();
  168. }
  169. // Add the main program code into the continuous loop() function
  170. void loop()
  171. {
  172. anemometer_1.meassure();
  173. anemometer_2.meassure();
  174. anemometer_3.meassure();
  175. if (second() != last_second) {
  176. if (minute() != last_minute) {
  177. if (hour() != last_hour) {
  178. every_hour();
  179. }
  180. every_minute();
  181. }
  182. every_second();
  183. }
  184. }