Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

eventlog.ino 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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/2011/JAN");
  19. // create or open a file for append
  20. ofstream sdlog("LOGS/2011/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) sd.errorHalt("append failed");
  25. sdlog.close();
  26. }
  27. //------------------------------------------------------------------------------
  28. void setup() {
  29. Serial.begin(9600);
  30. while (!Serial) {} // wait for Leonardo
  31. // pstr stores strings in flash to save RAM
  32. cout << pstr("Type any character to start\n");
  33. while (Serial.read() <= 0) {}
  34. delay(400); // catch Due reset problem
  35. // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  36. // breadboards. use SPI_FULL_SPEED for better performance.
  37. if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
  38. // append a line to the logfile
  39. logEvent("Another line for the logfile");
  40. cout << "Done - check /LOGS/2011/JAN/LOGFILE.TXT on the SD" << endl;
  41. }
  42. //------------------------------------------------------------------------------
  43. void loop() {}