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

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