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

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