Go Down

Topic: [SOLVED]Sd works in example append but fails in integration. (Read 984 times) previous topic - next topic

SouthernBinary

Sep 10, 2012, 11:36 pm Last Edit: Sep 11, 2012, 12:10 am by SouthernBinary Reason: 1
The example of append is the following code.
/*
* Append Example
*
* This sketch shows how to use open for append.
* The sketch will append 100 line each time it opens the file.
* The sketch will open and close the file 100 times.
*/
#include <SdFat.h>

// SD chip select pin
const uint8_t chipSelect = SS;

// file system object
SdFat sd;

// create Serial stream
ArduinoOutStream cout(Serial);

// store error strings in flash to save RAM
#define error(s) sd.errorHalt_P(PSTR(s))
//------------------------------------------------------------------------------
void setup() {
  // filename for this example
  char name[] = "APPEND.TXT";

  Serial.begin(9600);

  // pstr() stores strings in flash to save RAM
  cout << endl << pstr("Type any character to start\n");
  while (Serial.read() < 0) {}

  // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  // breadboards.  use SPI_FULL_SPEED for better performance.
  if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();

  cout << pstr("Appending to: ") << name;

  for (uint8_t i = 0; i < 100; i++) {
    // open stream for append
    ofstream sdout(name, ios::out | ios::app);
    if (!sdout) error("open failed");

    // append 100 lines to the file
    for (uint8_t j = 0; j < 100; j++) {
      // use int() so byte will print as decimal number
      sdout << "line " << int(j) << " of pass " << int(i);
      sdout << " millis = " << millis() << endl;
    }
    // close the stream
    sdout.close();

    if (!sdout) error("append data failed");

    // output progress indicator
    if (i % 25 == 0) cout << endl;
    cout << '.';
  }
  cout << endl << "Done" << endl;
}
//------------------------------------------------------------------------------
void loop() {}

The code I have which fails to open the code is as follows


// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

#include <Wire.h>              // communication protocol
#include "RTClib.h"            //Real Time Clock Library
#include <SdFat.h>             //SD library
#define error(s) sd.errorHalt_P(PSTR(s)) // error strings for SD
const uint8_t chipSelect = SS; // SD chip select pin
SdFat sd;                      // File system object
ArduinoOutStream cout(Serial);
RTC_DS1307 RTC;

void setup () {
    Serial.begin(9600);
    Wire.begin();
    RTC.begin();
    RTC.adjust(DateTime(__DATE__, __TIME__));//sets time and date at compile
 
}

void loop () {
    //gets time for the now.function
    DateTime now = RTC.now();
   if(now.year()>2000){
//    Serial.print(now.year(), DEC);
    if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt(); // slows SD speed down
    char name[] = "GardenLog.TXT";
    ofstream sdout(name, ios::out | ios::app);
    if (!sdout) error("open failed");     Code fails here and ofstream doesn't open
    // use sdout<<string<<int(name)
    // use sdout<< endl to end the line for a CR LF
    sdout << " Humidityint" << " tempint" << " plantH";
//  sdout << now.year() << " Humidityint" << " tempint" << " plantH";
//    sdout << "line " << int(j) << " of pass " << int(i);
    sdout << endl;
    //sdout.close closes and saves the file in the SD card
    sdout.close();
    cout << pstr("tested timestap w string in folder ") << name;
    }
//    Serial.print('/');
//    Serial.print(now.month(), DEC);
//    Serial.print('/');
//    Serial.print(now.day(), DEC);
//    Serial.print(' ');
//    Serial.print(now.hour(), DEC);
//    Serial.print(':');
//    Serial.print(now.minute(), DEC);
//    Serial.print(':');
//    Serial.print(now.second(), DEC);
//    Serial.println();
//
//    Serial.println();
    //delay(3000); // delay is 3 seconds
    delay(60000); //delay is once a min
}

I've been trying to troubleshoot it for a few hours now and I don't see it so I thought I'd post and see if there is a reply after my class. Thanks.
I'm not a newbie I just lurk a lot.

SurferTim

I haven't checked the rest, but sd.begin should be called only once in the setup function for this application.
Code: [Select]
void setup () {
    Serial.begin(9600);
    Wire.begin();
    RTC.begin();
    RTC.adjust(DateTime(__DATE__, __TIME__));//sets time and date at compile
    Serial.print("Starting sd...");
    if (!sd.begin(chipSelect, SPI_HALF_SPEED))  Serial.println("fail");
    else Serial.println("ok");
}


fat16lib

SdFat only supports FAT 8.3 names.  This name has nine characters before the dot so is not valid.

Code: [Select]

char name[] = "GardenLog.TXT"

SouthernBinary

#3
Sep 11, 2012, 12:06 am Last Edit: Sep 11, 2012, 12:12 am by SouthernBinary Reason: 1
Thanks it did need to be in setup surferTim and thank you fat16lib! It fixed my problem!
I'm not a newbie I just lurk a lot.

Go Up