Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <SD.h>
- #include <DS3231.h>
- File myFile;
- DS3231 rtc(SDA, SCL);
- int pinCS = 53;//pin 10 on arduino
- void setup() {
- Serial.begin(9600);
- pinMode(pinCS, OUTPUT);
- //sd card initialisation
- if(SD.begin())
- {
- Serial.println("sd card is rady to use");
- }
- else{
- Serial.println("sd card initialisation failed");
- return;
- }
- rtc.begin();
- }
- void loop(){
- Serial.print(rtc.getTimeStr());
- Serial.print(",")
- Serial.println(int(rtc.getTemp()));
- //creat/open file
- myFile = SD.open("test.txt", FILE_WRITE);
- //if the file is opened okay, write to it:
- if(myFile){
- Serial.println("writing to file...");
- //write to file
- myFile.print(rtc.getTimeStr());
- myFile.print(",");
- myFile.println(int(rtc.getTemp()));
- myFile.close();//closing file
- Serial.println("done");
- }
- //if the file didnt open, printing an error
- else{
- Serial.println("error opening test.txt");}
- //reading from the file
- myFile = SD.open("test.txt");
- if(myFile){
- Serial.println("read:");
- //reading the whole file
- while(myFile.available()){
- Serial.write(myFile.read());
- }
- myFile.close();
- }
- else
- {
- Serial.println("error opening test.txt");
- }
- delay(3000);
- }
- void loop()
- {
- //empty
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement