I hope this is the correct place to ask this question. I think I have a code problem, rather than a hardware problem.I am trying to write the input of an analog pin to a .csv file, once per second.I am using an Arduino Uno with the Wireless SD shield, writing to a 1GB micro SD card formatted to FAT16.

The problem is that it writes several files, and then just seems to stop. The number of files it writes each time is not consistent ... so it is difficult to diagnose the exact problem.

I am quite new to coding, and to the Aduino in particular so it is most likely that I have made an obvious error. I have spent many days going over it and I can not think of any other way to fix it. Perhaps a more experienced person can spot my mistake?

The code is intended to:Create a temporary string composed of 5 readings from the analog 0 pin, each spaced 1 second apart, each separated by line feed.(Add a timestamp to each line if the RTC is attached).Write that string to two files; "mainfile" and another file with a dynamically assigned file name. Reset the string to empty, and repeat.

It is taking the voltage reading of a GSR sensor which is plugged into analog 0. The project is intended to allow the Arduino to be powered from a battery and worn all day while readings are taken every second and recorded to SD card. It is writing to two files because sometimes the files seem to get corrupted if power is removed during the write process. The "mainfile" is a continuous record of the data. In case this single massive file gets corrupted, the series of backup files contain the same data spread over many files. The series of backup files are called "split file" in the code. Currently each file is composed of only one file write (but this can be adjusted), and then a new file is created, and so on. (At the moment each file consists of only 5 seconds worth of data, but that will be adjusted to about 5 mins when it is working properly). The file name for each "split file" is composed of a single letter followed by a 7 digit number, and each file is named sequentially, for example, "A0000001", "A0000002", "A0000003", etc.

It seems to be correctly writing data to the file for the first few rounds, but eventually it stops. It seems to stop most often at split file number 9 or 26.

In addition it seems to go wrong when I plug the RTC in. I DID have the RTC working correctly, but now when I plug it in, the data which is written to the file just comes out as random characters (squares, currency signs, symbols, etc.). I had no idea why it is doing that, because I can't remember changing anything to cause it to happen!

Can anyone see if there is something majorly wrong that I am doing in the code?

(I will post the code in the next post, because I have run out of characters here).

/* On the Ethernet Shield, CS is pin 4. Note that even if it's not used as the CS pin, the hardware CS pin (10 on most Arduino boards, 53 on the Mega) must be left as an output or the SD library functions will not work. */

while ( counter <= cycles ) { digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) SERIAL_PRINT("on"); delay(milliseconds); // wait for a second digitalWrite(led, LOW); // turn the LED off by making the voltage LOW if ( Serial_OK == 1) {Serial.println("off");} delay(milliseconds); // wait for a second counter++; }

The first thing to do is get rid of String and use C-style null terminated character arrays instead.There is a known problem with the String library, on top of which there's only 2kB of Ram in total which is not enough to support the sort of memory usage that the String library requires.You are also doing something really strange with nested while loops but fix the strings first.

OK great, thanks. I guess that will probably make a big impact. I'll change the strings and see what happens. Might take me a while to figure out how to do that ;-)

The nested loops are there for the file name of the "split file". According to the sd library I'm using I think the file name has to be char. I am creating the file name one character at a time. Each new file name has to be "the previous file name +1". The only way I could figure out how to do that was to increase the ASCII character value by 1 each time, for each digit, in the correct order. Each digit starts at 48 (ASCII for "0"), and goes up in 1's to 57 (ASCII for 9), then resets to 48 and the one to the left goes up by one - going from right to left. So the first file is called "A0000001", the second is called "A0000002", the third is called "A0000003", and so on until I run out of battery. Hence the nested loops. (Using while rather than for, so that it is slightly easier to see where the loops end). I'm not used to using arrays and chars to make strings. The programming language I normally use just has string, double, bool and int, with automatic implicit conversion between them. There is probably a better way to increase the file name by 1 each time!

Yes, I have printed the file names to serial, and they seem to be working fine.

I tried using an integer, and adding that to the string, to start with. That is the method I would normally have used. But it kept giving datatype mismatch errors. Hopefully sprintf will remove the need for the nested loops, which may well help prevent other problems from occurring as well. Thank you!

Hi,Excuse me for cutting in, but I'm having exactly the same problem printing to the SD card on the Arduino wireless SD shield. I'm only printing data, not filenames, but I only get anywhere from 2 to 11 data points, while all the data show on the Serial monitor and X-CTU terminal. Also, the times are not incremented on the card, but only repeat the initial time. I'll post the problem under Project Guidance, adding to what I've already posted there.

I am using the UNO, but the only chipSelect that worked was 4, not 10. Is this true for you?

I have 4 set as chipSelect, but am also setting both 4 and 10 to OUTPUT in the setup function (contrary to my code posted above). It may not be technically correct but it seems to work, and does not seem to cause any other problems.

I have implemented sprintf, and that seems to have fixed the problem! Thank you el_supremo.

These pages also helped:http://arduino.cc/en/Reference/Stringhttp://php.net/manual/en/function.sprintf.php

Just implementing sprintf for the data that is written to the SD card was not sufficient. If there are any other strings at all in the code it seems to clog the whole thing up.I removed all strings and any references to them, and replaced them all with either char[] or text written directly into the .print or .println function.

I also removed the nested loops for generating the file name, and replaced them with an integer++. (I did not know about the "%07d" ability to place prefix zeros in this language).

The only problem now is that it writes to the SD card every second, rather than saving several seconds up and writing them all at once. This is annoying because it takes a second to write the data, so during that second no data is being collected. Also, it must reduce battery life. I will have to figure out how to join char arrays dynamically in a loop - it seems more complicated than just joining strings. I am assuming there will be some limit on the number of readings that can be joined together before the memory is overloaded and the original problem starts all over again.

On the Ethernet Shield, CS is pin 4. Note that even if it's not used as the CS pin, the hardware CS pin (10 on most Arduino boards, 53 on the Mega) must be left as an output or the SD library functions will not work.

I suppose I should also mention that I am not ending my file names with .txt or .csv

If they don't have any file endings then when you go to open them windows asks you to select which program you want to open them with. If you open them with Excel then it immediately brings up a warning message claiming the data is corrupt or in the wrong format (or something like that). If you just click to continue through to opening the file then it still opens the file correctly and all the data is still fine ... I did this because with .txt at the end of the file was causing the file to be completely unreadable, even though it was correctly being recognised as a txt file in windows (also it was printing funnily over the serial as well).

The reason Excel doesn't like your file is probably because you don't separate the fields with commas. It should work if you write the file with the .CSV extension and change the output format to this: