I can't keep the data I want onto my Sd Card (Adafruit datalogging shield)

Hi guys, My project (weather station) is almost finished. I just need to keep the data onto the sd card. I have achieved to keep precipitation data but I can't say the same with temperatures.

I have tried a lot of combinations but I just cant the temperature reading.

These are some of the options Ive tried so far:

Option 1: following this sketch the serial monitor says that it's not creating any file but creates an empty file:

/*-----( Declare Libraries )-----*/

#include <SPI.h>
#include <SD.h>          //Ensure that you downlaod these libraries and store them in your Arduino libraries folder 
#include <Wire.h>        //Remember no semi-colon after these commands
#include "RTClib.h"
#include <OneWire.h>
#include <DallasTemperature.h> //Get DallasTemperature Library here:  http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library

/*-----( Declare Constants and Pin Numbers )-----*/
const int REED = 9;     //The reed switch outputs to digital pin 9
int val = 0;            //Current value of reed switch
int old_val = 0;        //Old value of reed switch
int REEDCOUNT = 0;      //The intial count is zero

const int chipSelect = 10;
File logfile;
#define SYNC_INTERVAL 60000
#define ONE_WIRE_BUS_PIN 2
uint32_t syncTime = 0; // time of last sync()

/*-----( Declare objects )-----*/
RTC_Millis rtc; /////RTC_DS1307 RTC; // define the Real Time Clock object
OneWire oneWire(ONE_WIRE_BUS_PIN);// Setup a oneWire instance to communicate with any OneWire devices
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.

/*-----( Declare Variables )-----*/
DeviceAddress Probe01 = { 
  0x28, 0x5A, 0xB7, 0x00, 0x00, 0x00, 0x80, 0x6D };// Assign the addresses of your 1-Wire temp sensors. 
DeviceAddress Probe02 = { 
  0x28, 0x5C, 0xB7, 0x00, 0x00, 0x00, 0x80, 0xDF };// See the tutorial on how to obtain these addresses:
DeviceAddress Probe03 = { 
  0x28, 0x12, 0x6D, 0x73, 0x06, 0x00, 0x00, 0x27 };// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html


/////////////Program Set-up/////////////////////////////////////////////////////////////////////

void setup(void){ /****** SETUP: RUNS ONCE ******/

  Serial.begin(9600);// start serial port to show results
  pinMode (REED, INPUT_PULLUP);  //Activate the internal pull-up resistor

  /////////////Setting up the file on the SD Card///////////////////////////////

  Serial.println("Initializing SD card...");
  pinMode(10, OUTPUT);  // make sure that the default chip select pin is set to output


  if (!SD.begin(chipSelect)) {  // see if the card is present and can be initialized:
    Serial.println("Card failed, or not present");
  }
  Serial.println("card initialized.");  // create a new file

  char filename[] = "LOGGER00.CSV";      //The file names must be in the 8.3 format
  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
      logfile = SD.open(filename, FILE_WRITE); 
      break;  // leave the loop!
    }
  }

  if (! logfile) {
    Serial.println("could not create file");
  }

  Serial.print("Logging to: ");
  Serial.println(filename);

  rtc.begin(DateTime(F(__DATE__), F(__TIME__)));
  /////rtc.adjust(DateTime(2015, 7, 7, 12, 40, 0));

  Serial.print("Initializing temp. sensors.");
  Serial.println(" Reading 3 probes:");
  logfile.print("Temperatures and ");

  sensors.begin();// Initialize the Temperature measurement library

  sensors.setResolution(Probe01, 10);// set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
  sensors.setResolution(Probe02, 10);
  sensors.setResolution(Probe03, 10);

  Serial.println("Rainfall, Reading/count, Time, Date:");
  logfile.println("Rainfall, Reading/count Time Date"); 

}//--(end setup )---


///////////////////The Program Loop//////////////////////////////////////////////////////////////////////
void loop(){ //****** LOOP: RUNS CONSTANTLY ******/


  DateTime now = rtc.now(); /////DateTime now;    // and record date/time of change

  val = digitalRead(REED);


  if ((val == LOW) && (old_val == HIGH))//Low means that the Reed switch is open (which happens when the magnet passess).

  {
    delay(10);    // Delay put in to deal with any "bouncing" in the switch.

    char buf1[50];

    Serial.print("Precipitation");
    Serial.print(" = ");
    REEDCOUNT = REEDCOUNT + 10;
    old_val = val;

    Serial.print(REEDCOUNT);
    Serial.print(" ml of rain ");

    sprintf(buf1, "at %02d:%02d:%02d of %02d/%02d/%02d",  now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());
    Serial.println(buf1);

    logfile.print("Precipitation");
    logfile.print(" = ");
    logfile.print("10");
    logfile.print("ml at ");
    logfile.println(buf1);

  }
  else {
    old_val = val; //If the status hasn't changed then do nothing
  }

  if ((millis() - syncTime) < SYNC_INTERVAL) return;
  logfile.flush();
  syncTime = millis();

    Serial.println();
    Serial.print("Number of Devices found on bus = ");  
    Serial.println(sensors.getDeviceCount());   
    Serial.print("Getting temperatures... ");  
    Serial.println();   
      
    sensors.requestTemperatures();  // Command all devices on bus to read temperature 

    Serial.print("Probe 01 temperature is:   ");
    printTemperature(Probe01);
    Serial.println();
    
    Serial.print("Probe 02 temperature is:   ");
    printTemperature(Probe02);
    Serial.println();
    
    Serial.print("Probe 03 temperature is:   ");
    printTemperature(Probe03);
    Serial.println();
    
    char buf1[50];
    sprintf(buf1, "at %02d:%02d:%02d of %02d/%02d/%02d",  now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());
    Serial.println(buf1);
     
    Serial.println(sensors.getDeviceCount());
    
    logfile.println("Temperature Probe 01 = ");
    logfile.println("Temperature Probe 02 = ");
    logfile.println("Temperature Probe 03 = ");
    logfile.println(buf1);

    

    
}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/
void printTemperature(DeviceAddress deviceAddress)
{

  float tempC = sensors.getTempC(deviceAddress);

  if (tempC == -127.00) 
  {
    Serial.print("Error getting temperature  ");
  } 
  else
  {
    Serial.print("C: ");
    Serial.print(tempC);
    Serial.print(" F: ");
    Serial.print(DallasTemperature::toFahrenheit(tempC));
  }
//End printTemperature
}

Option 2: following the same but removing this:

//char buf1[50];
    sprintf(buf1, "at %02d:%02d:%02d of %02d/%02d/%02d",  now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());
    Serial.println(buf1);
     
    Serial.println(sensors.getDeviceCount());
    
    logfile.println("Temperature Probe 01 = ");
    logfile.println("Temperature Probe 02 = ");
    logfile.println("Temperature Probe 03 = ");
    logfile.println(buf1);

it gives an error which says that buf1 was not declared in this scope.

Option 3: following this:

//char buf1[50];
   // sprintf(buf1, "at %02d:%02d:%02d of %02d/%02d/%02d",  now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());
    //Serial.println(buf1);
     
    Serial.println(sensors.getDeviceCount());
    
    logfile.println("Temperature Probe 01 = ");
    logfile.println("Temperature Probe 02 = ");
    logfile.println("Temperature Probe 03 = ");
//   logfile.println(buf1);

it does not create any file either.

Option 4 (best option so far): following this:

  //char buf1[50];
   // sprintf(buf1, "at %02d:%02d:%02d of %02d/%02d/%02d",  now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());
    //Serial.println(buf1);
     
    //Serial.println(sensors.getDeviceCount());
    
    //logfile.println("Temperature Probe 01 = ");
    //logfile.println("Temperature Probe 02 = ");
    //logfile.println("Temperature Probe 03 = ");
//    logfile.println(buf1);

It creates a file but just recording rainfall values as the logfile.println ("Temperature Probe...."); is not on the sketch.

Anyone can figure out where do I have to type the logfile for the temperatures probes? Thanks

Why don't you write a simple sketch just for writing to a file on the SD card?
Make sure it works and then incorporate that into your project's sketch.

I have written and tested all the simple skecths successfully. I just cant get the combinations of both data (temperatures and precipitation) onto it. thanks

ieee488:
Option 4 (best option so far): following this:

  //char buf1[50];

// sprintf(buf1, "at %02d:%02d:%02d of %02d/%02d/%02d",  now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());
   //Serial.println(buf1);
   
   //Serial.println(sensors.getDeviceCount());
   
   //logfile.println("Temperature Probe 01 = ");
   //logfile.println("Temperature Probe 02 = ");
   //logfile.println("Temperature Probe 03 = ");
//    logfile.println(buf1);




It creates a file but just recording rainfall values as the logfile.println ("Temperature Probe...."); is not on the sketch.

I bet it isn't. It's nothing but comments and won't do anything.

U shouldnt bet when not reading properly.