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