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

63 行
1.5KB

  1. /*
  2. * Append a line to a file - demo of pathnames and streams
  3. */
  4. #include <SPI.h>
  5. #include "SdFat.h"
  6. // SD chip select pin
  7. const uint8_t chipSelect = SS;
  8. // file system object
  9. SdFat sd;
  10. // define a serial output stream
  11. ArduinoOutStream cout(Serial);
  12. //------------------------------------------------------------------------------
  13. /*
  14. * Append a line to logfile.txt
  15. */
  16. void logEvent(const char *msg) {
  17. // create dir if needed
  18. sd.mkdir("logs/2014/Jan");
  19. // create or open a file for append
  20. ofstream sdlog("logs/2014/Jan/logfile.txt", ios::out | ios::app);
  21. // append a line to the file
  22. sdlog << msg << endl;
  23. // check for errors
  24. if (!sdlog) {
  25. sd.errorHalt("append failed");
  26. }
  27. sdlog.close();
  28. }
  29. //------------------------------------------------------------------------------
  30. void setup() {
  31. Serial.begin(9600);
  32. // Wait for USB Serial
  33. while (!Serial) {
  34. SysCall::yield();
  35. }
  36. // F() stores strings in flash to save RAM
  37. cout << F("Type any character to start\n");
  38. while (!Serial.available()) {
  39. SysCall::yield();
  40. }
  41. delay(400); // catch Due reset problem
  42. // Initialize at the highest speed supported by the board that is
  43. // not over 50 MHz. Try a lower speed if SPI errors occur.
  44. if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
  45. sd.initErrorHalt();
  46. }
  47. // append a line to the logfile
  48. logEvent("Another line for the logfile");
  49. cout << F("Done - check /logs/2014/Jan/logfile.txt on the SD") << endl;
  50. }
  51. //------------------------------------------------------------------------------
  52. void loop() {}