Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. /*
  2. SD card datalogger
  3.  
  4. This example shows how to log data from three analog sensors
  5. to an SD card using the SD library.
  6.  
  7. The circuit:
  8. * analog sensors on analog ins 0, 1, and 2
  9. * SD card attached to SPI bus as follows:
  10. ** MOSI - pin 11
  11. ** MISO - pin 12
  12. ** CLK - pin 13
  13. ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
  14.  
  15. created 24 Nov 2010
  16. modified 9 Apr 2012
  17. by Tom Igoe
  18.  
  19. This example code is in the public domain.
  20.  
  21. */
  22.  
  23. #include <SPI.h>
  24. #include <SD.h>
  25.  
  26. const int chipSelect = 4;
  27.  
  28. void saveVal(String s){
  29. File dataFile = SD.open("datalog.txt", FILE_WRITE);
  30.  
  31. // if the file is available, write to it:
  32. if (dataFile) {
  33. dataFile.println(s);
  34. dataFile.close();
  35. // print to the serial port too:
  36. Serial.print("Saved: ");
  37. Serial.println(s);
  38. }
  39. // if the file isn't open, pop up an error:
  40. else {
  41. Serial.println("error opening datalog.txt");
  42. }
  43. }
  44.  
  45.  
  46.  
  47. void setup() {
  48. // Open serial communications and wait for port to open:
  49. Serial.begin(9600);
  50. while (!Serial) {
  51. ; // wait for serial port to connect. Needed for native USB port only
  52. }
  53.  
  54.  
  55. Serial.print("Initializing SD card...");
  56.  
  57. // see if the card is present and can be initialized:
  58. if (!SD.begin(chipSelect)) {
  59. Serial.println("Card failed, or not present");
  60. // don't do anything more:
  61. return;
  62. }
  63. Serial.println("card initialized.");
  64.  
  65. }
  66.  
  67. void loop() {
  68. //Read Sensor
  69. String dataString = "";
  70. int sensor = analogRead(0);
  71. dataString += String(sensor);
  72.  
  73. //Save Val
  74. saveVal("New Data: " + dataString);
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement