Ardunio SD Card data Store



  • HI,
    I am beginner of ardunio, I have written the ardunio code to store the temp and humidity data with time stamp information .
    while collecting the data, it shows correctly (temp and humidity with time stamp) but not stored in the SD card.
    any one help me in this ? (i like to know where i am doing mistake ?) . My code:
    #include <SD.h>
    #include "DHT.h"
    #include <Wire.h>
    #include "RTClib.h"
    #include "DS1307.h"
    #define DHTPIN A0
    #define DHTTYPE DHT22
    DHT dht(DHTPIN, DHTTYPE);
    DS1307 clock; // declare object for DS3234 class

    const int chipSelect = 10;

    void setup()
    {
    // Open serial communications and wait for port to open:
    Serial.begin(9600);
    while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
    }

    Serial.print("Initializing SD card...");
    // make sure that the default chip select pin is set to
    // output, even if you don't use it:
    pinMode(10, OUTPUT);

    // see if the card is present and can be initialized:
    if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
    }
    Serial.println("card initialized.");

    // create a new file
    char filename[] = "LOGGER00.txt";a
    for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
    // only open a new file if it doesn't exist
    break; // leave the loop!
    }

    }
    Serial.print("Logging to: ");
    Serial.println(filename);
    // Enter the time and date when flashing the program
    clock.begin();
    clock.fillByYMD(2015,2,15);//Feb 10,2015
    clock.fillByHMS(18,05,30);//15:28 30"
    clock.fillDayOfWeek(SUN);//Tuesday
    clock.setTime();//write time to the RTC chip
    }

    void loop()
    {
    // Reading temperature or humidity takes about 250 milliseconds!
    // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
    float h = dht.readHumidity();
    float t = dht.readTemperature();

    //Time stamp for your program
    printTime();
    // check if returns are valid, if they are NaN (not a number) then something went wrong!

    if (isnan(t) || isnan(h))
    {
    Serial.println("Failed to read from DHT");
    }
    else
    {
    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.println(" *C");
    }

    }
    void printTime()
    {
    clock.getTime();
    Serial.print(clock.hour, DEC);
    Serial.print(":");
    Serial.print(clock.minute, DEC);
    Serial.print(":");
    Serial.print(clock.second, DEC);
    Serial.print(" ");
    Serial.print(clock.month, DEC);
    Serial.print("/");
    Serial.print(clock.dayOfMonth, DEC);
    Serial.print("/");
    Serial.print(clock.year+2000, DEC);
    Serial.print(" ");
    Serial.print(clock.dayOfMonth);
    Serial.print("*");
    switch (clock.dayOfWeek)// Friendly printout the weekday
    {
    case MON:
    Serial.print("MON");
    break;
    case TUE:
    Serial.print("TUE");
    break;
    case WED:
    Serial.print("WED");
    break;
    case THU:
    Serial.print("THU");
    break;
    case FRI:
    Serial.print("FRI");
    break;
    case SAT:
    Serial.print("SAT");
    break;
    case SUN:
    Serial.print("SUN");
    break;
    }
    Serial.println(" ");
    }


  • Hero Member

    can you indent your code with 4 spaces? It's too difficult to read un-formatted.



  • #include <SD.h>
    #include "DHT.h"
    #include <Wire.h>
    #include "RTClib.h"
    #include "DS1307.h"
    #define DHTPIN A0
    #define DHTTYPE DHT22
    DHT dht(DHTPIN, DHTTYPE);
    DS1307 clock; // declare object for DS3234 class

    const int chipSelect = 10;

    void setup()
    {
    // Open serial communications and wait for port to open:
    Serial.begin(9600);
    while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
    }

    Serial.print("Initializing SD card...");
    // make sure that the default chip select pin is set to
    // output, even if you don't use it:
    pinMode(10, OUTPUT);

    // see if the card is present and can be initialized:
    if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
    }
    Serial.println("card initialized.");

    // create a new file
    char filename[] = "LOGGER00.txt";
    for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
    // only open a new file if it doesn't exist
    break; // leave the loop!
    }

    }
    Serial.print("Logging to: ");
    Serial.println(filename);
    // Enter the time and date when flashing the program
    clock.begin();
    clock.fillByYMD(2015,2,15);//Feb 15,2015
    clock.fillByHMS(22,55,30);//19:05 30"
    clock.fillDayOfWeek(SUN);//Sunday
    clock.setTime();//write time to the RTC chip
    }

    void loop()
    {
    // Reading temperature or humidity takes about 250 milliseconds!
    // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    delay(60000); // wait one second (1000) before do it again (60 sec =1 min) 60 *1000
    //Time stamp for your program
    printTime();
    // check if returns are valid, if they are NaN (not a number) then something went wrong!

    if (isnan(t) || isnan(h))
    {
    Serial.println("Failed to read from DHT");
    }
    else
    {
    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.println(" *C");
    }

    }
    void printTime()
    {
    clock.getTime();
    Serial.print(clock.hour, DEC);
    Serial.print(":");
    Serial.print(clock.minute, DEC);
    Serial.print(":");
    Serial.print(clock.second, DEC);
    Serial.print(" ");
    Serial.print(clock.month, DEC);
    Serial.print("/");
    Serial.print(clock.dayOfMonth, DEC);
    Serial.print("/");
    Serial.print(clock.year+2000, DEC);
    Serial.print(" ");
    Serial.print(clock.dayOfMonth);
    Serial.print("*");
    switch (clock.dayOfWeek)// Friendly printout the weekday
    {
    case MON:
    Serial.print("MON");
    break;
    case TUE:
    Serial.print("TUE");
    break;
    case WED:
    Serial.print("WED");
    break;
    case THU:
    Serial.print("THU");
    break;
    case FRI:
    Serial.print("FRI");
    break;
    case SAT:
    Serial.print("SAT");
    break;
    case SUN:
    Serial.print("SUN");
    break;
    }
    Serial.println(" ");
    }

    • list item

Log in to reply
 

Suggested Topics

  • 4
  • 9
  • 2
  • 4
  • 8
  • 2

1
Online

11.4k
Users

11.1k
Topics

112.7k
Posts