選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

dataLogger.ino 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 at the highest speed supported by the board that is
  70. // not over 50 MHz. Try a lower speed if SPI errors occur.
  71. if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
  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. // Read any Serial data.
  92. do {
  93. delay(10);
  94. } while (Serial.available() && Serial.read() >= 0);
  95. Serial.print(F("Logging to: "));
  96. Serial.println(fileName);
  97. Serial.println(F("Type any character to stop"));
  98. // Write data header.
  99. writeHeader();
  100. // Start on a multiple of the sample interval.
  101. logTime = micros()/(1000UL*SAMPLE_INTERVAL_MS) + 1;
  102. logTime *= 1000UL*SAMPLE_INTERVAL_MS;
  103. }
  104. //------------------------------------------------------------------------------
  105. void loop() {
  106. // Time for next record.
  107. logTime += 1000UL*SAMPLE_INTERVAL_MS;
  108. // Wait for log time.
  109. int32_t diff;
  110. do {
  111. diff = micros() - logTime;
  112. } while (diff < 0);
  113. // Check for data rate too high.
  114. if (diff > 10) {
  115. error("Missed data record");
  116. }
  117. logData();
  118. // Force data to SD and update the directory entry to avoid data loss.
  119. if (!file.sync() || file.getWriteError()) {
  120. error("write error");
  121. }
  122. if (Serial.available()) {
  123. // Close file and stop.
  124. file.close();
  125. Serial.println(F("Done"));
  126. SysCall::halt();
  127. }
  128. }