writing to SD without computer

Hi, i need some help. I am using Arduino LEONARDO board. I need to use Arduino for messuring accelerations and EMG signal. I have no problem when i conect to computer my Arduino board, but it doesn't write any data on SD when i am using external battery as power suplay. It doesn't open the serial port? Am I right? How can i fix this? Thank you very much for help.

Post your code! IN CODE TAGS.

Mark

I am using this one. I found it here on website and rewrite it for my use.

#include <SD.h>

const int chipSelect = 4;

void setup()
{ Serial.begin(9600);
  while (!Serial) {;}
  Serial.print("Initializing SD card...");
  pinMode(10, OUTPUT);
  if (!SD.begin(chipSelect)) 
  { Serial.println("Card failed, or not present");
    return;}
  Serial.println("card initialized.");}

void loop()
{ String dataString = "";

  for (int analogPin = 0; analogPin < 4; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 3) {dataString += ",";}
}

  File dataFile = SD.open("datalog.csv", FILE_WRITE);

  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
   Serial.println(dataString);
  }
  
  else {
    Serial.println("error opening datalog.txt");
  }
}

I worked it out :slight_smile: I deleted while(!Serial) and its working now. But i dont understand, because i must keep Serial.begin in the program.

This is because your using a Leo. And you there telling it to wait until it connected to your PC.

Mark

Adamca7:
I worked it out :slight_smile: I deleted while(!Serial) and its working now. But i dont understand, because i must keep Serial.begin in the program.

Serial.begin() is required because you are writing to the serial port later in the sketch. You must call Serial.begin() before writing anything on the serial port, so that it knows what baud rate to use, and so it can properly initialize the hardware.

The while (!Serial) code is sitting there and spinning, waiting for a serial connection to be made. That's why it worked when you are connected to a computer (but not until you opened the serial monitor, I'm sure.) But of course, that means it will never do anything until there is a connection. That's doesn't work for a stand-alone application.

Now, with the while (!Serial) loop commented out, it will not wait for a connection when you are connected to the computer. That means that any initial messages (like the card initializing/initialized messages) will probably be lost, because the code will be long past that section by the time you are able to actually make the connection from your computer.

The while (!Serial) is not part the required initialization sequence. It's only there to hold things up until you make a connection, so that the initial messages are not lost.

Thank you one more time :slight_smile:

Adamca7:
I worked it out :slight_smile: I deleted while(!Serial) and its working now. But i dont understand, because i must keep Serial.begin in the program.

I tried this exactly but on a Nano and I'm also copying from one TXT file to another TXT file on the SD card. It will only execute the program when I first upload the sketch or click and physically open the serial Monitor. If I run it without opening the serial monitor: nothing. Here is the beginning of my code:

#include <SPI.h>
 #include <SD.h>
 
 File myFile;
void setup()
{ Serial.begin(9600);
    Serial.print("Initializing SD card...");
  pinMode(10, OUTPUT);
  if (!SD.begin(4)) 
  { Serial.println("Card failed, or not present");
    return;}
  Serial.println("card initialized.");
  myFile = SD.open("GPSData.txt", FILE_WRITE);
  if (SD.exists("GPSData.txt")) { //Delete old data files to start fresh
    SD.remove("GPSData.txt");
 }   
  myFile.close();  //Close the file
  
  readSDSettings1();// this is a loop that enables to copy and paste txt files from the SD and back to it
 
 }

I figured out what I was doing wrong. I was testing the sketch after uploading, using the same USB port, used for the upload it, with the sketch still launched in Windows. If you do that, somehow the Nano knows and stops and waits for you to connect, by launching the serial monitor. I hooked the Nano to an external USB battery pack and all was good.