Advertisement
farcop

Arduino Serial to SD-card dataloger

Nov 12th, 2011
1,654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include "SD.h"
  2.  
  3. const int chipSelect = 4;
  4. const int buffer_length = 256;
  5.  
  6. byte buffer[buffer_length];
  7.  
  8. File dataFile;
  9.  
  10. unsigned long data_length;
  11.  
  12.  
  13. void setup()
  14. {
  15.   Serial.begin(115200);
  16.   Serial.flush();
  17.   // make sure that the default chip select pin is set to
  18.   // output, even if you don't use it:
  19.   pinMode(10, OUTPUT);
  20.     // see if the card is present and can be initialized:
  21.   if (!SD.begin(chipSelect)) {
  22.      // don't do anything more:
  23.     return;
  24.  }
  25.  
  26.   dataFile = SD.open("datalog.txt", FILE_WRITE);
  27. }
  28.  
  29. void loop()
  30. {
  31.  
  32.     if (Serial.available() > 0) {
  33.     buffer[data_length] = Serial.read();
  34.     ++data_length;
  35.     if(data_length > buffer_length - 1) {
  36.        dataFile.write(buffer, data_length);
  37.        data_length = 0;
  38.     }
  39.   } else { if (data_length > 0) {
  40.        dataFile.write(buffer, data_length);
  41.        data_length = 0;
  42.   }
  43.  
  44.   }
  45.  
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement