Pages: [1]   Go Down
Author Topic: Sketch will only write files when the Serial Port is opened  (Read 593 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/* 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);
 
}

Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1250
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Fixed it, CHEERS!
Logged

Pages: [1]   Go Up
Jump to: