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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // Visual Micro is in vMicro>General>Tutorial Mode
  2. //
  3. /*
  4. Name: Teensy4.1_Datalogger new.ino
  5. Created: 31.08.2022 18:39: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, R_Temp_fix = 500,
  46. LED_Write = 5, LED_Normal = 6, LED_Batterie = 7, Grenz_U_Batterie = 13,
  47. taster_manuell_speichern = 28, Windfahne = 20, T_sensor_input = 17, Batterie_input = 38;
  48. int last_second, last_minute, last_hour, seconds_for_blink;
  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. speed_min = min(speed_min, speed_per_second[i]);
  75. speed_max = max(speed_max, speed_per_second[i]);
  76. }
  77. arithmetic_deviation = pow((summ / float(amount_saved - 1)), (1 / 2.0));
  78. square_deviation = pow((square_summ / float(amount_saved - 1)), (1 / 2.0));
  79. cubic_deviation = pow((cubic_summ / float(amount_saved - 1)), (1 / 2.0));
  80. seconds_skipped = 60 - amount_saved;
  81. }
  82. float arithmetic_mean;
  83. float arithmetic_deviation;
  84. float square_mean;
  85. float square_deviation;
  86. float cubic_mean;
  87. float cubic_deviation;
  88. float speed_min;
  89. float speed_max;
  90. int seconds_skipped;
  91. };
  92. struct anemomenter_maessurement {
  93. public:
  94. void setup(int pin) {
  95. this->reed_contact = Bounce(pin, 10);
  96. }
  97. void meassure() {
  98. if (reed_contact.update() && reed_contact.fallingEdge()) {
  99. count_per_second++;
  100. }
  101. }
  102. void save_wind_speed() {
  103. wind_speed_per_second[saved_seconds] = 0.4 * count_per_second;
  104. saved_seconds++;
  105. }
  106. void calculate() {
  107. values[saved_minutes].calculate(wind_speed_per_second, saved_seconds);
  108. saved_seconds = 0;
  109. saved_minutes++;
  110. }
  111. void file_print() {
  112. for (int i = 0; i < saved_minutes; i++) {
  113. file.printf("Min: %f,\tMax: %f,\t", values[i].speed_min, values[i].speed_max);
  114. file.printf("Arith. Mittel: % f,\tStandard Abw.: %f\t", values[i].arithmetic_mean, values[i].arithmetic_deviation);
  115. file.printf("Quadr. Mittel: % f,\tStandard Abw.: %f\t", values[i].square_mean, values[i].square_deviation);
  116. file.printf("Kub. Mittel: %f,\tStandard Abw.: %f\t", values[i].cubic_mean, values[i].cubic_deviation);
  117. file.printf("Übersprungene Sek.: %i\n", values[i].seconds_skipped);
  118. }
  119. file.printf("Übersprungene Min.: %i\n", 60 - saved_minutes);
  120. saved_minutes = 0;
  121. }
  122. private:
  123. int count_per_second = 0;
  124. int saved_seconds = 0;
  125. int saved_minutes = 0;
  126. float wind_speed_per_second[60];
  127. Bounce reed_contact;
  128. calculations values[60];
  129. }anemometer_1, anemometer_2, anemometer_3;
  130. struct temp_sensor {
  131. private:
  132. int U_Temp;
  133. int R_Temp;
  134. float Temp = 0;
  135. short int array_Temp_datenblatt[20] = { -30, -20, -10, 0, 10, 20, 25, 30, 40, 50,
  136. 391, 424, 460, 498, 538, 581, 603, 626, 672, 722};
  137. public:
  138. void measure() {
  139. digitalWrite(power_Temp_sensor, HIGH);
  140. U_Temp = analogRead(T_sensor_input);
  141. digitalWrite(power_Temp_sensor, LOW);
  142. R_Temp = R_Temp_fix / (1023 - U_Temp);
  143. for (int t = 0; t < 9; t++) {
  144. if ((R_Temp >= array_Temp_datenblatt[t + 10]) && (R_Temp <= array_Temp_datenblatt[t + 11])) {
  145. Temp = array_Temp_datenblatt[t] + ((R_Temp - array_Temp_datenblatt[t + 10]) * (array_Temp_datenblatt[t + 1] - array_Temp_datenblatt[t]) / (array_Temp_datenblatt[t + 11] - array_Temp_datenblatt[t + 10]));
  146. }
  147. }
  148. }
  149. void file_print() {
  150. printf("Temperatur: %f.2 °C\n", Temp);
  151. }
  152. } temp_sensor_1;
  153. struct wind_vain {
  154. private:
  155. float wind_sec;
  156. float wind_summ = 0;
  157. int values[60];
  158. int saved_minutes = 0;
  159. public:
  160. void measure() {
  161. digitalWrite(power_Windfahne, HIGH);
  162. wind_sec = map(analogRead(Windfahne), 0, 1023, 20, 350);
  163. digitalWrite(power_Windfahne, LOW);
  164. wind_summ += wind_sec;
  165. }
  166. void calculate() {
  167. values[saved_minutes] = wind_summ;
  168. wind_summ = 0;
  169. saved_minutes++;
  170. }
  171. void file_print() {
  172. for (int i = 0; i < saved_minutes; i++) {
  173. printf("Windrichtung in ° Winkel: %f.2\n", values[i]);
  174. }
  175. saved_minutes = 0;
  176. }
  177. }wind_vain_1;
  178. void dateTime(uint16_t* date, uint16_t* time, uint8_t* ms10) {
  179. // Return date using FS_DATE macro to format fields.
  180. *date = FS_DATE(year(), month(), day());
  181. // Return time using FS_TIME macro to format fields.
  182. *time = FS_TIME(hour(), minute(), second());
  183. // Return low time bits in units of 10 ms.
  184. *ms10 = second() & 1 ? 100 : 0;
  185. }
  186. void write_sd() {
  187. digitalWrite(LED_Write, HIGH);
  188. char file_name[50];
  189. FsDateTime::setCallback(dateTime);
  190. sprintf(file_name, "Windmessmast-%d.%d.%d_%d:%d.txt", year(), month(), day(), hour(), minute());
  191. sd.begin(SD_CONFIG);
  192. if (file.open(file_name, FILE_WRITE)) {
  193. Serial.println("Start SD schreiben");
  194. file.println("Messdaten von Windmessmasst");
  195. file.println();
  196. file.println("Data logger : Teensy 4.1");
  197. file.println(software_name);
  198. file.println();
  199. file.println("Anemometer_1 Werte:");
  200. anemometer_1.file_print();
  201. file.println("Anemometer_2 Werte:");
  202. anemometer_2.file_print();
  203. file.println("Anemometer_3 Werte:");
  204. anemometer_3.file_print();
  205. file.println("Temperatursensor_1 Werte:");
  206. temp_sensor_1.file_print();
  207. file.println("Windfahne_1 Werte:");
  208. wind_vain_1.file_print();
  209. file.close();
  210. Serial.println("Ende des Schreibvorgangs");
  211. }
  212. digitalWrite(LED_Write, LOW);
  213. }
  214. void every_second() {
  215. anemometer_1.save_wind_speed();
  216. anemometer_2.save_wind_speed();
  217. anemometer_3.save_wind_speed();
  218. wind_vain_1.measure();
  219. if (digitalRead(taster_manuell_speichern) == HIGH){
  220. write_sd();
  221. }
  222. digitalWrite(LED_Normal, LOW);
  223. seconds_for_blink++;
  224. if (seconds_for_blink % 10 == 0) {
  225. digitalWrite(LED_Normal, HIGH);
  226. }
  227. last_second = second();
  228. }
  229. void every_minute() {
  230. anemometer_1.calculate();
  231. anemometer_2.calculate();
  232. anemometer_3.calculate();
  233. wind_vain_1.calculate();
  234. if((analogRead(Batterie_input) * 15.3 / float(1023)) < Grenz_U_Batterie) {
  235. digitalWrite(LED_Batterie, HIGH);
  236. }
  237. last_minute = minute();
  238. }
  239. void every_hour() {
  240. write_sd();
  241. last_hour = hour();
  242. }
  243. // The setup() function runs once each time the micro-controller starts
  244. void setup()
  245. {
  246. //set input and output
  247. pinMode(Windfahne, INPUT);
  248. pinMode(Batterie_input, INPUT);
  249. pinMode(T_sensor_input, INPUT);
  250. pinMode(taster_manuell_speichern, INPUT);
  251. pinMode(LED_Write, OUTPUT);
  252. pinMode(LED_Fail, OUTPUT);
  253. pinMode(LED_Normal, OUTPUT);
  254. pinMode(LED_Batterie, OUTPUT);
  255. pinMode(power_Temp_sensor, OUTPUT);
  256. pinMode(power_Windfahne, OUTPUT);
  257. setSyncProvider((getExternalTime)Teensy3Clock.get());
  258. Serial.begin(9600);
  259. Serial.println("Teensy 4.1-Datalogger gestartet");
  260. if (timeStatus() != timeSet) {
  261. Serial.println("Fehler bei Synchronisieren der Uhrzeit mit der RTC");
  262. digitalWrite(LED_Fail, HIGH);
  263. return;
  264. }
  265. Serial.println("Uhrzeit erfolgreich mit der RTC synchronisiert");
  266. if (!sd.begin(SD_CONFIG)) {
  267. digitalWrite(LED_Fail, HIGH);
  268. sd.initErrorHalt(&Serial);
  269. }
  270. anemometer_1.setup(2);
  271. anemometer_2.setup(9);
  272. anemometer_3.setup(22);
  273. seconds_for_blink = 0;
  274. Serial.println("Messung startet");
  275. last_second = second();
  276. while (last_second == second()) {};
  277. last_second = second();
  278. last_minute = minute();
  279. last_hour = hour();
  280. }
  281. // Add the main program code into the continuous loop() function
  282. void loop()
  283. {
  284. anemometer_1.meassure();
  285. anemometer_2.meassure();
  286. anemometer_3.meassure();
  287. if (second() != last_second) {
  288. every_second();
  289. if (minute() != last_minute) {
  290. every_minute();
  291. if (hour() != last_hour) {
  292. every_hour();
  293. }
  294. }
  295. }
  296. }