Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- en/*
- SD card read/write
- This example shows how to read and write data to and from an SD card file
- The circuit:
- SD card attached to SPI bus as follows:
- ** MOSI - pin 11
- ** MISO - pin 12
- ** CLK - pin 13
- ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
- created Nov 2010
- by David A. Mellis
- modified 9 Apr 2012
- by Tom Igoe
- This example code is in the public domain.
- */
- #include <SPI.h>
- #include <SD.h>
- #include <Wire.h>
- #include <RTC.h>
- static DS1307 RTC;
- #include "DHT.h"
- #define dht_int A2
- #define dht_ext A1
- #define DHTTYPE DHT11
- DHT dht1(dht_int, DHTTYPE);
- DHT dht2(dht_ext, DHTTYPE);
- int resetPin = A0;
- File myFile;
- void setup() {
- // Open serial communications and wait for port to open:
- Serial.begin(115200);
- while (!Serial) {
- ;
- }
- pinMode(resetPin, OUTPUT);
- RTC.begin();
- Wire.begin();
- dht1.begin();
- dht2.begin();
- delay(1000);
- Serial.print(".");
- delay(0500);
- Serial.print(".");
- delay(0500);
- Serial.print(".");
- delay(5000);
- Serial.print("Initializing SD card...");
- delay(3000);
- if (!SD.begin(4)) {
- Serial.println("initialization failed!");
- delay(5000);
- digitalWrite(resetPin, HIGH);
- while (1);
- }
- Serial.println("initialization done.");
- // open the file. note that only one file can be open at a time,
- // so you have to close this one before opening another.
- myFile = SD.open("test.txt", FILE_WRITE);
- //if the file opened okay, write to it:
- if (myFile) {
- Serial.print("Writing to test.txt...");
- myFile.println("testing 1, 2, 3.");
- // close the file:
- myFile.close();
- Serial.println("done.");
- }
- /*else {
- // if the file didn't open, print an error:
- Serial.println("error opening test.txt");
- }
- // re-open the file for reading:
- myFile = SD.open("test.txt");
- if (myFile) {
- Serial.println("test.txt:");
- // read from the file until there's nothing else in it:
- while (myFile.available()) {
- Serial.write(myFile.read());
- }
- // close the file:
- myFile.close();
- } else {
- // if the file didn't open, print an error:
- Serial.println("error opening test.txt");
- }*/
- }
- void loop() {
- myFile = SD.open("test.txt", FILE_WRITE);
- float h1 = dht1.readHumidity();
- float t1 = dht1.readTemperature();
- float f1 = dht1.readTemperature(true);
- float h2 = dht2.readHumidity();
- float t2 = dht2.readTemperature();
- float f2 = dht2.readTemperature(true);
- if (RTC.getDay()<10){
- myFile.print(0);
- }
- myFile.print(RTC.getDay());
- myFile.print("-");
- if (RTC.getMonth()<10){
- myFile.print(0);
- }
- myFile.print(RTC.getMonth());
- myFile.print("-");
- myFile.print(RTC.getYear());
- myFile.print(",");
- myFile.print(RTC.getHours());
- myFile.print(":");
- if (RTC.getMinutes()<10){
- myFile.print(0);
- }
- myFile.print(RTC.getMinutes());
- myFile.print(":");
- if (RTC.getSeconds()<10){
- myFile.print(0);
- }
- myFile.print(RTC.getSeconds());
- myFile.print(",");
- myFile.print("T_INT:,");
- myFile.print(t1);
- myFile.print(",degré_C & ");
- myFile.print(" HUMIDITÉ:,");
- myFile.print(h1);
- myFile.print(",% ");
- myFile.print("T_EXT:,");
- myFile.print(t2);
- myFile.print(",degré C & ");
- myFile.print("HUMIDITÉ :, ");
- myFile.print(h2);
- myFile.println();
- myFile.close();
- delay(10000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement