>>>>> "Denis" == Denis Heidtmann <[email protected]> writes:
Denis> On Sat, Sep 24, 2016 at 5:56 PM, Russell Senior Denis> <[email protected]> Denis> wrote: >> >>>>> "Denis" == Denis Heidtmann <[email protected]> writes: >> Denis> I am developing a logging program. The Arduino is a Uno R2. It Denis> has an ATAfruit SD shield w/ RTC. For testing purposes I record Denis> data once per second. When I am finished testing I will be Denis> taking data much less frequently, although there might be random Denis> times when a few data points are only one second apart. >> Denis> I convert the data ( a long; two strings, 3 ints) to a string in Denis> CSV format. But often not all the data gets assembled into the Denis> string. It seems that the += operation does not append the Denis> string to be added. >> Denis> I removed the references to the SD card and the failures went Denis> away, or so it seems. I need to run it for a longer time, but Denis> before I removed the SD stuff the problem showed up in two to Denis> four seconds. So let's assume that the issue was the SD writing. >> Denis> How might I go about exploring this issue? >> >> Can you post the relevant code? Denis> Since I do not know what code is relevant, I must post the entire Denis> thing, warts and all. The function which has been my focus is Denis> the csv_data, so it is filled with spurious Serial.print Denis> commands. I have since found that += will append ints to Denis> Strings. Changing csv_data to use that feature does not change Denis> the behavior: things do not get added to the string being Denis> assembled. Note that initially the code worked fine. Then I Denis> noticed that some items did not appear in the returned string. Denis> Finally most recently no complete strings get returned. Then I Denis> found that if I removed the code relating to the SD, the problem Denis> went away. Denis> What I believe the cause of the gradual change from good to bad Denis> is that I regularly remove the SD from the Arduino, put it in the Denis> laptop, view the data, then delete the files before unmounting it Denis> and returning it to the Arduino. I notice that although I empty Denis> the trash the laptop reports that there is still some space used, Denis> an amount that has been slowly increasing over the days I have Denis> been exploring this issue. (It would not surprise me if Denis> formatting the SD would make the problem go away for a while.) Denis> As to keeping data in ram; other threads in use, etc.: I thought Denis> that the Arduino was single-threaded, and I have no clue which Denis> data is stored where. Denis> Looks like version 1.6.9 of the Arduino IDE. I am running Ubuntu Denis> 16.04 The SD library is SD Built-in by Arduino, SparkFun Version Denis> 1.0.8 Denis> Please excuse the long post. Feel free to discard any irrelevant Denis> code below. Denis> -Denis Denis> /* hot water heater logger 9/13/2016 added limit to # records in Denis> file 9/21/2016 changing to using difference between successive Denis> reading as a way to detect change in state. Of questionable Denis> accuracy in detecting water in use. The circuit: * Flue TC pin 0 Denis> amp gain 325 type J TC * Cold H2O pin 1 amp gain 1000 type J TC * Denis> temp. of cold water pipe at input to tank rises * when no water Denis> is being used. It drops to the * temp. of water supply when the Denis> water is used. * Hot H2O pin 2 type 36 sensor * * SD card Denis> attached to SPI bus as follows: ** MOSI - pin 11 ** MISO - pin 12 Denis> ** CLK - pin 13 ** CS - pin 10 Denis> */ Denis> #include <SPI.h> #include <SD.h> #include <Wire.h> #include Denis> "RTClib.h" RTC_DS1307 rtc; Denis> const int chipSelect = 10; const int flue_pin = 0;//amp gain Denis> approx. 360 const int cold_pin = 1;//amp gain approx. 1000 const Denis> int hot_pin = 2;//type 36, no gain const int d_flueToff = -10; Denis> //burner going off diff const int d_flueTon = +10; //burner going Denis> on diff const int d_coldTon = -1; //water going on diff const int Denis> d_coldToff = +2; //water going off diff long t1; //unix time Denis> const int report_interval = 600; //seconds const int maxcount = Denis> 1008; // limits # records in file. int rem1; //remainder of Denis> time/interval boolean burn_on; //burner is on boolean H2O_on; Denis> //water is in use Denis> volatile boolean close_file = false; Denis> const int greenLEDpin=4; const int redLEDpin=3; Denis> String dataString; String filename; File dataFile; int Denis> Tflue1,Tflue2,Tcold1,Tcold2,Thot; Denis> void setup() { pinMode(redLEDpin, OUTPUT); pinMode(greenLEDpin, Denis> OUTPUT); Denis> // Open serial communications: Serial.begin(9600); Denis> attachInterrupt(digitalPinToInterrupt(2),button_press,RISING); Denis> Serial.print("Initializing SD card..."); Denis> // see if the card is present and can be initialized: if Denis> (!SD.begin(chipSelect)) { Serial.println("Card failed, or not Denis> present"); fail_flash(2); Denis> } Denis> Serial.println("card initialized."); Denis> if (! rtc.begin()) { Serial.println("Couldn't find RTC"); Denis> fail_flash(3); Denis> } Denis> //short wait on reset or power on w/ rapid green flashing Denis> for(int i = 1; i<16; i++){ digitalWrite(greenLEDpin,HIGH); Denis> delay(150); digitalWrite(greenLEDpin,LOW); delay(150); Denis> } Denis> } Denis> void loop() { Denis> int count = 0; //Generate file name: filename = "D"; Denis> time_temp(t1,Tflue1,Tcold1,Thot);//initial data String foo = Denis> String(t1); int n = foo.length(); foo = foo.substring(n-7); //7 Denis> digits of seconds to make name filename += foo; filename += Denis> ".csv"; Denis> //now set initial states burn_on = false; //arbitrary H2O_on = Denis> false; //arbitrary rem1 = t1%report_interval; Serial.println(""); Denis> Serial.println(""); Serial.println(filename); Denis> dataFile = SD.open(filename,FILE_WRITE); if(!dataFile){ Denis> Serial.print(filename); Serial.println(" failed to open"); Denis> fail_flash(4); Denis> } You probably shouldn't be re-opening the file everytime through the loop. Do it once in setup. If you have more than one file to write to, open all of them. Or if you don't know how many files you are going to open, keep track of which ones you've opened and only open a new one when you have to. -- Russell Senior, President [email protected] _______________________________________________ PLUG mailing list [email protected] http://lists.pdxlinux.org/mailman/listinfo/plug
