SallatielFernandes

microSD-4-LDR

Jul 1st, 2022 (edited)
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SPI.h>
  2. #include <SD.h>
  3.  
  4. #define GND A0
  5. #define VCC A5
  6.  
  7. const int chipSelect = 10;
  8.  
  9. void setup() {
  10.   pinMode(GND, OUTPUT);
  11.   pinMode(VCC, OUTPUT);
  12.   digitalWrite(GND, LOW);
  13.   digitalWrite(VCC, HIGH);
  14.  
  15.   Serial.begin(9600);
  16.  
  17.   Serial.print("Initializing SD card...");
  18.  
  19.   // see if the card is present and can be initialized:
  20.   if (!SD.begin(chipSelect)) {
  21.     Serial.println("Card failed, or not present");
  22.     while (1);
  23.   }
  24.   Serial.println("card initialized.");
  25. }
  26.  
  27.  
  28. void loop() {
  29.   // make a string for assembling the data to log:
  30.   String dataString = "";
  31.  
  32.   // read three sensors and append to the string:
  33.   for (int analogPin = 1; analogPin < 5; analogPin++) {
  34.     int sensor = analogRead(analogPin);
  35.     dataString += String(sensor);
  36.     if (analogPin < 4) {
  37.       dataString += ",";
  38.     }
  39.   }
  40.  
  41.   // open the file. note that only one file can be open at a time,
  42.   // so you have to close this one before opening another.
  43.   File dataFile = SD.open("datalog2.csv", FILE_WRITE);
  44.  
  45.   // if the file is available, write to it:
  46.   if (dataFile) {
  47.     dataFile.println(dataString);
  48.     dataFile.close();
  49.     // print serial port:
  50.     Serial.println(dataString);
  51.  
  52.   }else {
  53.     Serial.println("error opening datalog.txt");
  54.   }
  55. }
Add Comment
Please, Sign In to add comment