Advertisement
Tasin98

Untitled

Mar 30th, 2020
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <SD.h>
  3. #include <DS3231.h>
  4. File myFile;
  5. DS3231 rtc(SDA, SCL);
  6. int pinCS = 53;//pin 10 on arduino
  7.  
  8. void setup() {
  9. Serial.begin(9600);
  10. pinMode(pinCS, OUTPUT);
  11. //sd card initialisation
  12. if(SD.begin())
  13. {
  14. Serial.println("sd card is rady to use");
  15. }
  16. else{
  17. Serial.println("sd card initialisation failed");
  18. return;
  19. }
  20. rtc.begin();
  21. }
  22. void loop(){
  23. Serial.print(rtc.getTimeStr());
  24. Serial.print(",")
  25. Serial.println(int(rtc.getTemp()));
  26. //creat/open file
  27. myFile = SD.open("test.txt", FILE_WRITE);
  28. //if the file is opened okay, write to it:
  29. if(myFile){
  30. Serial.println("writing to file...");
  31. //write to file
  32. myFile.print(rtc.getTimeStr());
  33. myFile.print(",");
  34. myFile.println(int(rtc.getTemp()));
  35. myFile.close();//closing file
  36. Serial.println("done");
  37. }
  38. //if the file didnt open, printing an error
  39. else{
  40. Serial.println("error opening test.txt");}
  41. //reading from the file
  42. myFile = SD.open("test.txt");
  43. if(myFile){
  44. Serial.println("read:");
  45. //reading the whole file
  46. while(myFile.available()){
  47. Serial.write(myFile.read());
  48. }
  49. myFile.close();
  50. }
  51. else
  52. {
  53. Serial.println("error opening test.txt");
  54. }
  55. delay(3000);
  56. }
  57. void loop()
  58. {
  59. //empty
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement