Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1. #include <SD.h>
  2. #include <SPI.h>
  3.  
  4. //MOSI 11
  5. //MISO 12
  6. //SCK  13
  7.  
  8. File dataFile;
  9.  
  10. const int chipSelect = 10;
  11. const int sensorPin = A0;
  12. void setup() {
  13.  
  14.   Serial.begin(9600);
  15.  
  16.   while(!SD.begin(chipSelect)){
  17.     Serial.print("Error initializing SD card, retrying");
  18.     delay(250);
  19.     for (int i=1;i<4;i++){
  20.       Serial.print(".");
  21.       delay(250);
  22.     }
  23.     Serial.println("");
  24.   }
  25.   Serial.println("SD card initialized");
  26.  
  27.   if (SD.exists("temp.csv")){
  28.     SD.remove("temp.csv");
  29.     Serial.println("temp.csv already exists, removing...");
  30.   }
  31.  
  32.   dataFile = SD.open("temp.csv", FILE_WRITE);
  33.   if (dataFile){
  34.     Serial.println("Succesfully opened test.txt");
  35.     dataFile.close();
  36.   } else {
  37.     Serial.println("Error opening test.txt");
  38.   }
  39. }
  40.  
  41. void loop() {
  42.   // put your main code here, to run repeatedly:
  43.   int sensorVal = analogRead(sensorPin);
  44.   float voltage = (sensorVal/1024.0) * 5.0;
  45.   float temperature = (voltage - 0.5) * 100;
  46.   dataFile = SD.open("temp.csv", FILE_WRITE);
  47.   dataFile.print(millis());
  48.   dataFile.print(",");
  49.   dataFile.println(temperature);
  50.   dataFile.close();
  51.   Serial.println(temperature);
  52.   delay(1000);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement