No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

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