Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

10 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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, pin 7 on Teensy with audio board
  9. ** MISO - pin 12
  10. ** CLK - pin 13, pin 14 on Teensy with audio board
  11. ** CS - pin 4, pin 10 on Teensy with audio board
  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. #include <SPI.h>
  19. // On the Ethernet Shield, CS is pin 4. Note that even if it's not
  20. // used as the CS pin, the hardware CS pin (10 on most Arduino boards,
  21. // 53 on the Mega) must be left as an output or the SD library
  22. // functions will not work.
  23. // change this to match your SD shield or module;
  24. // Arduino Ethernet shield: pin 4
  25. // Adafruit SD shields and modules: pin 10
  26. // Sparkfun SD shield: pin 8
  27. // Teensy audio board: pin 10
  28. // Wiz820+SD board: pin 4
  29. // Teensy 2.0: pin 0
  30. // Teensy++ 2.0: pin 20
  31. const int chipSelect = 4;
  32. void setup()
  33. {
  34. //UNCOMMENT THESE TWO LINES FOR TEENSY AUDIO BOARD:
  35. //SPI.setMOSI(7); // Audio shield has MOSI on pin 7
  36. //SPI.setSCK(14); // Audio shield has SCK on pin 14
  37. // Open serial communications and wait for port to open:
  38. Serial.begin(9600);
  39. while (!Serial) {
  40. ; // wait for serial port to connect. Needed for Leonardo only
  41. }
  42. Serial.print("Initializing SD card...");
  43. // see if the card is present and can be initialized:
  44. if (!SD.begin(chipSelect)) {
  45. Serial.println("Card failed, or not present");
  46. // don't do anything more:
  47. return;
  48. }
  49. Serial.println("card initialized.");
  50. }
  51. void loop()
  52. {
  53. // make a string for assembling the data to log:
  54. String dataString = "";
  55. // read three sensors and append to the string:
  56. for (int analogPin = 0; analogPin < 3; analogPin++) {
  57. int sensor = analogRead(analogPin);
  58. dataString += String(sensor);
  59. if (analogPin < 2) {
  60. dataString += ",";
  61. }
  62. }
  63. // open the file. note that only one file can be open at a time,
  64. // so you have to close this one before opening another.
  65. File dataFile = SD.open("datalog.txt", FILE_WRITE);
  66. // if the file is available, write to it:
  67. if (dataFile) {
  68. dataFile.println(dataString);
  69. dataFile.close();
  70. // print to the serial port too:
  71. Serial.println(dataString);
  72. }
  73. // if the file isn't open, pop up an error:
  74. else {
  75. Serial.println("error opening datalog.txt");
  76. }
  77. }