Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

54 linhas
1.4KB

  1. /*
  2. * Append a line to a file - demo of pathnames and streams
  3. */
  4. #include <SdFat.h>
  5. // SD chip select pin
  6. const uint8_t chipSelect = SS;
  7. // file system object
  8. SdFat sd;
  9. // define a serial output stream
  10. ArduinoOutStream cout(Serial);
  11. //------------------------------------------------------------------------------
  12. /*
  13. * Append a line to LOGFILE.TXT
  14. */
  15. void logEvent(const char *msg) {
  16. // create dir if needed
  17. sd.mkdir("LOGS/2011/JAN");
  18. // create or open a file for append
  19. ofstream sdlog("LOGS/2011/JAN/LOGFILE.TXT", ios::out | ios::app);
  20. // append a line to the file
  21. sdlog << msg << endl;
  22. // check for errors
  23. if (!sdlog) sd.errorHalt("append failed");
  24. sdlog.close();
  25. }
  26. //------------------------------------------------------------------------------
  27. void setup() {
  28. Serial.begin(9600);
  29. while (!Serial) {} // wait for Leonardo
  30. // pstr stores strings in flash to save RAM
  31. cout << pstr("Type any character to start\n");
  32. while (Serial.read() <= 0) {}
  33. delay(400); // catch Due reset problem
  34. // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  35. // breadboards. use SPI_FULL_SPEED for better performance.
  36. if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
  37. // append a line to the logfile
  38. logEvent("Another line for the logfile");
  39. cout << "Done - check /LOGS/2011/JAN/LOGFILE.TXT on the SD" << endl;
  40. }
  41. //------------------------------------------------------------------------------
  42. void loop() {}