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.

135 line
3.9KB

  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 = 200;
  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. while (!Serial) {} // wait for Leonardo
  61. delay(1000);
  62. Serial.println(F("Type any character to start"));
  63. while (!Serial.available()) {}
  64. // Initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  65. // breadboards. use SPI_FULL_SPEED for better performance.
  66. if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
  67. // Find an unused file name.
  68. if (BASE_NAME_SIZE > 6) {
  69. error("FILE_BASE_NAME too long");
  70. }
  71. while (sd.exists(fileName)) {
  72. if (fileName[BASE_NAME_SIZE + 1] != '9') {
  73. fileName[BASE_NAME_SIZE + 1]++;
  74. } else if (fileName[BASE_NAME_SIZE] != '9') {
  75. fileName[BASE_NAME_SIZE + 1] = '0';
  76. fileName[BASE_NAME_SIZE]++;
  77. } else {
  78. error("Can't create file name");
  79. }
  80. }
  81. if (!file.open(fileName, O_CREAT | O_WRITE | O_EXCL)) error("file.open");
  82. do {
  83. delay(10);
  84. } while (Serial.read() >= 0);
  85. Serial.print(F("Logging to: "));
  86. Serial.println(fileName);
  87. Serial.println(F("Type any character to stop"));
  88. // Write data header.
  89. writeHeader();
  90. // Start on a multiple of the sample interval.
  91. logTime = micros()/(1000UL*SAMPLE_INTERVAL_MS) + 1;
  92. logTime *= 1000UL*SAMPLE_INTERVAL_MS;
  93. }
  94. //------------------------------------------------------------------------------
  95. void loop() {
  96. // Time for next record.
  97. logTime += 1000UL*SAMPLE_INTERVAL_MS;
  98. // Wait for log time.
  99. int32_t diff;
  100. do {
  101. diff = micros() - logTime;
  102. } while (diff < 0);
  103. // Check for data rate too high.
  104. if (diff > 10) error("Missed data record");
  105. logData();
  106. // Force data to SD and update the directory entry to avoid data loss.
  107. if (!file.sync() || file.getWriteError()) error("write error");
  108. if (Serial.available()) {
  109. // Close file and stop.
  110. file.close();
  111. Serial.println(F("Done"));
  112. while(1) {}
  113. }
  114. }