Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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