Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

dataLogger.ino 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Simple data logger.
  3. */
  4. #include <SPI.h>
  5. #include "SdFat.h"
  6. // SD chip select pin. Be sure to disable any other SPI devices such as Enet.
  7. const uint8_t chipSelect = SS;
  8. // Interval between data records in milliseconds.
  9. // The interval must be greater than the maximum SD write latency plus the
  10. // time to acquire and write data to the SD to avoid overrun errors.
  11. // Run the bench example to check the quality of your SD card.
  12. const uint32_t SAMPLE_INTERVAL_MS = 1000;
  13. // Log file base name. Must be six characters or less.
  14. #define FILE_BASE_NAME "Data"
  15. //------------------------------------------------------------------------------
  16. // File system object.
  17. SdFat sd;
  18. // Log file.
  19. SdFile file;
  20. // Time in micros for next data record.
  21. uint32_t logTime;
  22. //==============================================================================
  23. // User functions. Edit writeHeader() and logData() for your requirements.
  24. const uint8_t ANALOG_COUNT = 4;
  25. //------------------------------------------------------------------------------
  26. // Write data header.
  27. void writeHeader() {
  28. file.print(F("micros"));
  29. for (uint8_t i = 0; i < ANALOG_COUNT; i++) {
  30. file.print(F(",adc"));
  31. file.print(i, DEC);
  32. }
  33. file.println();
  34. }
  35. //------------------------------------------------------------------------------
  36. // Log a data record.
  37. void logData() {
  38. uint16_t data[ANALOG_COUNT];
  39. // Read all channels to avoid SD write latency between readings.
  40. for (uint8_t i = 0; i < ANALOG_COUNT; i++) {
  41. data[i] = analogRead(i);
  42. }
  43. // Write data to file. Start with log time in micros.
  44. file.print(logTime);
  45. // Write ADC data to CSV record.
  46. for (uint8_t i = 0; i < ANALOG_COUNT; i++) {
  47. file.write(',');
  48. file.print(data[i]);
  49. }
  50. file.println();
  51. }
  52. //==============================================================================
  53. // Error messages stored in flash.
  54. #define error(msg) sd.errorHalt(F(msg))
  55. //------------------------------------------------------------------------------
  56. void setup() {
  57. const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
  58. char fileName[13] = FILE_BASE_NAME "00.csv";
  59. Serial.begin(9600);
  60. // Wait for USB Serial
  61. while (!Serial) {
  62. SysCall::yield();
  63. }
  64. delay(1000);
  65. Serial.println(F("Type any character to start"));
  66. while (!Serial.available()) {
  67. SysCall::yield();
  68. }
  69. // Initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  70. // breadboards. use SPI_FULL_SPEED for better performance.
  71. if (!sd.begin(chipSelect, SPI_HALF_SPEED)) {
  72. sd.initErrorHalt();
  73. }
  74. // Find an unused file name.
  75. if (BASE_NAME_SIZE > 6) {
  76. error("FILE_BASE_NAME too long");
  77. }
  78. while (sd.exists(fileName)) {
  79. if (fileName[BASE_NAME_SIZE + 1] != '9') {
  80. fileName[BASE_NAME_SIZE + 1]++;
  81. } else if (fileName[BASE_NAME_SIZE] != '9') {
  82. fileName[BASE_NAME_SIZE + 1] = '0';
  83. fileName[BASE_NAME_SIZE]++;
  84. } else {
  85. error("Can't create file name");
  86. }
  87. }
  88. if (!file.open(fileName, O_CREAT | O_WRITE | O_EXCL)) {
  89. error("file.open");
  90. }
  91. do {
  92. delay(10);
  93. } while (Serial.read() >= 0);
  94. Serial.print(F("Logging to: "));
  95. Serial.println(fileName);
  96. Serial.println(F("Type any character to stop"));
  97. // Write data header.
  98. writeHeader();
  99. // Start on a multiple of the sample interval.
  100. logTime = micros()/(1000UL*SAMPLE_INTERVAL_MS) + 1;
  101. logTime *= 1000UL*SAMPLE_INTERVAL_MS;
  102. }
  103. //------------------------------------------------------------------------------
  104. void loop() {
  105. // Time for next record.
  106. logTime += 1000UL*SAMPLE_INTERVAL_MS;
  107. // Wait for log time.
  108. int32_t diff;
  109. do {
  110. diff = micros() - logTime;
  111. } while (diff < 0);
  112. // Check for data rate too high.
  113. if (diff > 10) {
  114. error("Missed data record");
  115. }
  116. logData();
  117. // Force data to SD and update the directory entry to avoid data loss.
  118. if (!file.sync() || file.getWriteError()) {
  119. error("write error");
  120. }
  121. if (Serial.available()) {
  122. // Close file and stop.
  123. file.close();
  124. Serial.println(F("Done"));
  125. SysCall::halt();
  126. }
  127. }