You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

eventlog.ino 1.5KB

10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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.read() <= 0) {
  39. SysCall::yield();
  40. }
  41. delay(400); // catch Due reset problem
  42. // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  43. // breadboards. use SPI_FULL_SPEED for better performance.
  44. if (!sd.begin(chipSelect, SPI_HALF_SPEED)) {
  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() {}