Go Down

Topic: SD Card logging (Read 1 time) previous topic - next topic

zeus2kx

Apr 07, 2012, 10:19 pm Last Edit: Apr 10, 2012, 12:50 am by zeus2kx Reason: 1
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.


wildbill

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

mstanley

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():
Code: [Select]
  if (logFile)
  {
    logFile.println(dataString);
    logFile.close();
    if (serial_avail == true)
        Serial.println(dataString);
    end
  }


And...
Code: [Select]
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. ;)

plusminus_

Could you also use
Code: [Select]
if(Serial.available() >0) { Serial.println(dataString); }
?

mstanley


Could you also use
Code: [Select]
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.

zeus2kx


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

It doesn't even create a csv file on SD card.  :smiley-roll-sweat:

zeus2kx

mstanley and

Could you also use
Code: [Select]
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

plusminus_



Could you also use
Code: [Select]
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

zeus2kx

#9
Apr 09, 2012, 12:46 am Last Edit: Apr 10, 2012, 12:51 am by zeus2kx Reason: 1
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

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.

Thanks.

Z

Nick Gammon

What does the debug output show?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

zeus2kx


What does the debug output show?

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

mstanley


...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.

zeus2kx

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

Go Up