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.

93 lines
2.0KB

  1. /*
  2. SD card datalogger
  3. This example shows how to log data from three analog sensors
  4. to an SD card using the SD library.
  5. The circuit:
  6. * analog sensors on analog ins 0, 1, and 2
  7. * SD card attached to SPI bus as follows:
  8. ** MOSI - pin 11
  9. ** MISO - pin 12
  10. ** CLK - pin 13
  11. ** CS - pin 4
  12. created 24 Nov 2010
  13. modified 9 Apr 2012
  14. by Tom Igoe
  15. This example code is in the public domain.
  16. */
  17. #include <SD.h>
  18. // On the Ethernet Shield, CS is pin 4. Note that even if it's not
  19. // used as the CS pin, the hardware CS pin (10 on most Arduino boards,
  20. // 53 on the Mega) must be left as an output or the SD library
  21. // functions will not work.
  22. const int chipSelect = 4;
  23. void setup()
  24. {
  25. // Open serial communications and wait for port to open:
  26. Serial.begin(9600);
  27. while (!Serial) {
  28. ; // wait for serial port to connect. Needed for Leonardo only
  29. }
  30. Serial.print("Initializing SD card...");
  31. // make sure that the default chip select pin is set to
  32. // output, even if you don't use it:
  33. pinMode(10, OUTPUT);
  34. // see if the card is present and can be initialized:
  35. if (!SD.begin(chipSelect)) {
  36. Serial.println("Card failed, or not present");
  37. // don't do anything more:
  38. return;
  39. }
  40. Serial.println("card initialized.");
  41. }
  42. void loop()
  43. {
  44. // make a string for assembling the data to log:
  45. String dataString = "";
  46. // read three sensors and append to the string:
  47. for (int analogPin = 0; analogPin < 3; analogPin++) {
  48. int sensor = analogRead(analogPin);
  49. dataString += String(sensor);
  50. if (analogPin < 2) {
  51. dataString += ",";
  52. }
  53. }
  54. // open the file. note that only one file can be open at a time,
  55. // so you have to close this one before opening another.
  56. File dataFile = SD.open("datalog.txt", FILE_WRITE);
  57. // if the file is available, write to it:
  58. if (dataFile) {
  59. dataFile.println(dataString);
  60. dataFile.close();
  61. // print to the serial port too:
  62. Serial.println(dataString);
  63. }
  64. // if the file isn't open, pop up an error:
  65. else {
  66. Serial.println("error opening datalog.txt");
  67. }
  68. }