Go Down

Topic: Sketch will only write files when the Serial Port is opened (Read 767 times) previous topic - next topic

DennyP

Hello all,

I've written a sketch to be part of a bigger sketch.
Assuming there is no files in the SD to begin with, the sketch creates empty text files in the form "x.text", where x is a integer,
x is generated from an incrementer in the loop function.

The strange thing is that this only works when I've opened the serial monitor, to me based on my limited experience it shouldn't happen? It's not like I'm doing the logical (if Serial =="True"){evalute the rest of the code}", or am I?.

Reason for me asking is that I would like to initiate the loop using a pushbutton, and If I can't because it would only initialize when I open the serial monitor, then yeah I'd probably have a problem.

Here is the code:

Code: [Select]

/* This code demonstrates what happens when
you write multiple .csv files with the same name
repeatedly in the SD directory
*/

#include <SD.h>


const int SSPin = 10; // Hardware SS pin

void setup() {
  Serial.begin(9600);
 
  while (!Serial){
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  Serial.print("Initializing SD card...");
  pinMode(SSPin, OUTPUT); 
 
  if (!SD.begin(4)) {
  Serial.println("initialization failed!");
  return;
  }
  else {
  Serial.println("initialization done.");
  }
}

void loop() {
 
  // Local Var
  static int count = 0;
  char fileName[8];

 
  //Count and char/string conversions
 
  count++;
  String fileNumber = String(count) + ".txt";
  fileNumber.toCharArray(fileName, 8);
  Serial.println(fileName);


  //Create File
 
  File myFile = SD.open(fileName, FILE_WRITE);
  myFile.close();

  delay(1000);
 
}


marco_c

Code: [Select]
  while (!Serial){
    ; // wait for serial port to connect. Needed for Leonardo only
  }


If you are leaving this code in when you don't start the serial monitor it may never gat past this point.

It would be useful for you to post the code with and without serial initialisation.
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com


Go Up