SD Card logging

hello, I wrote a small code to collect values from some sensors and to write on SD card in csv format. It does the work but only when I have serial monitor open. Writing on SD card stops after the moment I close serial monitor. I want the process to keep going on until the time I turn off Arduino since this sensor unit will be working standalone without any connection with a computer.

Thanks a lot.

Regards, Z

ps. I am using Arduino Mega and official Arduino Ethernet Shield.

Someone?

What does it do it you never open the serial monitor?

Everytime you are writing to the SD card you are trying to send dataString over a serial connection that doesn't exist. I would suggest adding a flag, that you can control from your pc, to this bit of code at the end of your loop():

  if (logFile)
  {
    logFile.println(dataString);
    logFile.close();
    if (serial_avail == true)
        Serial.println(dataString);
    end
  }

And...

boolean serial_avail = false;

void setup(){
.
.
.
}

Then, write a small function that will allow you to send a command to change the value of serial_avail when you connect to the serial port.

Just don't forget to set it back to false before disconnecting the serial port. ;)

Could you also use

if(Serial.available() >0) { Serial.println(dataString); }

?

plusminus_: Could you also use

if(Serial.available() >0) { Serial.println(dataString); }

?

I thought the same thing at first but I figured since Serial.available() only checks for incoming data what happens if he never sends any data from his pc even if the Arduino serial port is open? That would mean he would have to constantly send requests from the pc before the Arduino would send any logging info.

Unless I'm completely missing how Serial.available() works which is a distinct possibility.

wildbill: What does it do it you never open the serial monitor?

It doesn't even create a csv file on SD card. :cold_sweat:

mstanley and

plusminus_: Could you also use

if(Serial.available() >0) { Serial.println(dataString); }

?

I am trying make the code work for me.. Do you guys have a better data logger code?

Thanks.

Z

mstanley:

plusminus_: Could you also use

if(Serial.available() >0) { Serial.println(dataString); }

?

I thought the same thing at first but I figured since Serial.available() only checks for incoming data what happens if he never sends any data from his pc even if the Arduino serial port is open? That would mean he would have to constantly send requests from the pc before the Arduino would send any logging info.

Unless I'm completely missing how Serial.available() works which is a distinct possibility.

Ah, I see.

Don't use that then, Z! :P

No, it worked after I applied at two different places in code. Now a csv file is created and written even I don't turn on serial monitor first or I turn on in the middle. Kindly comment how is it possible to make this code more efficient.

Thanks.

Z

...not really. It doesn't work if USB is not connected. There is also no file present on SD card. Please post a solution.

Thanks.

Z

What does the debug output show?

[quote author=Nick Gammon link=topic=100345.msg753756#msg753756 date=1333937818] What does the debug output show? [/quote] I am testing the code without serial monitor (USB unplugged). If not (with USB), code creates a csv file and logs data. Without USB (Serial monitor), this code is even unable to create a file on SD. I want Arduino to work standalone without a computer/no serial communication/no USB attached, only powered by AC adopter.

Thanks.

Z

zeus2kx: ...not really. It doesn't work if USB is not connected. There is also no file present on SD card. Please post a solution.

Go back to my first post and try using the boolean flag like I showed you. When the USB is disconnected you need to avoid calling any Serial functions. By using the flag it will prevent those calls unless the USB is connected AND you tell your sketch it's OK to use the serial port.

mstanley Thanks, problem solved. There wasn't any problem with code but the way I was using it. After everytime I unplug USB cable, I need to turn off and on Arduino for working.

Z