[SOLVED] How to make a buffer for this

Hi

I got this code:

    DateTime now = rtc.now();

    myFile.print(now.year(), DEC);
    myFile.print(F(","));
    myFile.print(now.month(), DEC);
    myFile.print(F(","));
    myFile.print(now.day(), DEC);
    myFile.print(F(","));
    myFile.print(now.dayOfTheWeek());
    myFile.print(F(","));
    myFile.print(now.hour(), DEC);
    myFile.print(F(","));
    myFile.print(now.minute(), DEC);
    myFile.print(F(","));
    myFile.print(SensorNum);
    myFile.print(F(","));
    myFile.print(SensorType);
    myFile.print(F(","));
    myFile.print(DHTTempValue);
    myFile.print(F(","));
    myFile.print(DHTHumValue);
    myFile.print(F(","));
    myFile.println(X_SensorValue);
    // close the file:
    myFile.close();

It wont print it a single line every time.

How can I make a buffer and send it in a single line.

or rewrite the code. So it only looks like this, I think:

myFile.println(Buffer());

Hope for some help???

jokerper:
It wont print it a single line every time.

Can you post an example of what it actually does and an example of how you want it to appear.

Also, please post the complete program. My wild guess is that the problem is in a different part of the program.

...R

Piece of code:

void writeFile() {
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print(F("Writing to test.txt..."));
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(F("Writing to file"));
    delay(1000);
    
    DateTime now = rtc.now();

    myFile.print(now.year(), DEC);
    myFile.print(F(","));
    myFile.print(now.month(), DEC);
    myFile.print(F(","));
    myFile.print(now.day(), DEC);
    myFile.print(F(","));
    myFile.print(now.dayOfTheWeek());
    myFile.print(F(","));
    myFile.print(now.hour(), DEC);
    myFile.print(F(","));
    myFile.print(now.minute(), DEC);
    myFile.print(F(","));
    myFile.print(SensorNum);
    myFile.print(F(","));
    myFile.print(SensorType);
    myFile.print(F(","));
    myFile.print(DHTTempValue);
    myFile.print(F(","));
    myFile.print(DHTHumValue);
    myFile.print(F(","));
    myFile.println(X_SensorValue);
    // close the file:
    myFile.close();
    Serial.println(F("done."));
        lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(F("Writing done."));
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening writeFile test.txt");
  }
}

The full code:

#include <SPI.h>
#include <SD.h>
File myFile;

#include "RTClib.h"
RTC_DS1307 rtc;
//long previousMillis = 0;  
//long interval = 1000;  

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

uint16_t freeMem() {
  char top;
  extern char *__brkval;
  extern char __bss_end;
  Serial.println( __brkval ? &top - __brkval : &top - &__bss_end);
}

#include <Ethernet.h>
EthernetServer server(23);
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xAA, 0xED
};
IPAddress ip(192, 168, 0, 200);
IPAddress myDns(192, 168, 0, 1);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
boolean alreadyConnected = false;


String readString; //main captured String
String readStringA; //main captured String
String SensorNum; // String angle; //data String
String SensorType; // String fuel;
String DHTTempValue; // String speed1;
String DHTHumValue; // String altidude;
String X_SensorValue; //

int input1; // , locations
int input2;
int input3;
int input4;
int input5;



#include <EthernetUdp.h>
#include <Wire.h>



#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display



void setup() {
  // put your setup code here, to run once:
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Ethernet.begin(mac, ip, myDns, gateway, subnet);



    if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
  }

  rtc.adjust(DateTime(2020, 3, 7, 5, 30, 0));
  // start listening for clients
  server.begin();

  Serial.print(F("Initializing SD card..."));

  if (!SD.begin(4)) {
    Serial.println(F("initialization failed!"));
    return;
  }
  Serial.println(F("initialization done."));

  lcd.init();
  // Print a message to the LCD.
  lcd.backlight();
  // initialize the ethernet device

  Serial.print(F("Chat server address: "));
  Serial.println(Ethernet.localIP());
  lcd.setCursor(0, 0);
  lcd.print(F("IP:"));
  lcd.print(Ethernet.localIP());
  lcd.setCursor(3, 1);
  lcd.print(F("ChatServer"));
  freeMem();
}

void loop() {
  // put your main code here, to run repeatedly:
  // writeFile();
  // readFile();
  getData();

/*
    unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {

    previousMillis = currentMillis;   
 
      DateTime now = rtc.now();

    Serial.print(now.year(), DEC);
    Serial.print(F("/"));
    Serial.print(now.month(), DEC);
    Serial.print(F("/"));
    Serial.print(now.day(), DEC);
    Serial.print(F(" ("));
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(F(") "));
    Serial.print(now.hour(), DEC);
    Serial.print(F(":"));
    Serial.print(now.minute(), DEC);
    Serial.print(F(":"));
    Serial.println(now.second(), DEC);
  

  }
*/
}

void getData() {
  // wait for a new client:
  EthernetClient client = server.available();

  // when the client sends the first byte, say hello:
  if (client) {
    if (!alreadyConnected) {
      // clear out the input buffer:
      client.flush();
      // Serial.println("We have a new client");
      //client.println("Hello, client!*");
      alreadyConnected = true;
    }

    if (client.available() > 0) {
      char c = client.read();  //gets one byte from serial buffer

      if (c == '*') {
        //do some stuff of input
        input1 = readString.indexOf(',');  //finds location of first ,
        SensorNum = readString.substring(0, input1);   //captures first data String

        input2 = readString.indexOf(',', input1 + 1 ); //finds location of second ,
        SensorType = readString.substring(input1 + 1, input2); //captures second data String

        input3 = readString.indexOf(',', input2 + 1 );
        DHTTempValue = readString.substring(input2 + 1, input3);

        input4 = readString.indexOf(',', input3 + 1 );
        DHTHumValue = readString.substring(input3 + 1, input4); //captures remain part of data after last ,

        input5 = readString.indexOf(',', input4 + 1 );
        X_SensorValue = readString.substring(input4 + 1, input5);



        int xType = SensorType.toInt();
        switch (xType) {
          case 2:    // your hand is close to the sensor
            //PIRAlarm();
            readFile();
            break;
          case 3:    // your hand is close to the sensor
            //Temperatur();
            writeFile();
            break;
          case 9: //
            //SD.remove("test.txt");
            break;
        }
        readString = ""; //clears variable for new input
        SensorNum = "";
        SensorType = "";
        DHTTempValue = "";
        DHTHumValue = "";
        X_SensorValue = "";
      }
      else {
        readString += c; //makes the string readString

      }
    }
  }

}


void readFile() {
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println(F("test.txt:"));

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening readFile test.txt");
  }

}

void writeFile() {
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print(F("Writing to test.txt..."));
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(F("Writing to file"));
    delay(1000);
    
    DateTime now = rtc.now();

    myFile.print(now.year(), DEC);
    myFile.print(F(","));
    myFile.print(now.month(), DEC);
    myFile.print(F(","));
    myFile.print(now.day(), DEC);
    myFile.print(F(","));
    myFile.print(now.dayOfTheWeek());
    myFile.print(F(","));
    myFile.print(now.hour(), DEC);
    myFile.print(F(","));
    myFile.print(now.minute(), DEC);
    myFile.print(F(","));
    myFile.print(SensorNum);
    myFile.print(F(","));
    myFile.print(SensorType);
    myFile.print(F(","));
    myFile.print(DHTTempValue);
    myFile.print(F(","));
    myFile.print(DHTHumValue);
    myFile.print(F(","));
    myFile.println(X_SensorValue);
    // close the file:
    myFile.close();
    Serial.println(F("done."));
        lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(F("Writing done."));
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening writeFile test.txt");
  }
}

Robin2:
Can you post an example of what it actually does and an example of how you want it to appear.

Also, please post the complete program. My wild guess is that the problem is in a different part of the program.

...R

Thanks for the complete program. Now please post the test.txt file.

test.txt file content:

2020,3,7,6,5,31,1,3,nan,nan,0

2020,3,7,6,5,31,1,3,nan,nan,0

2020,3,7,6,5,33,1,3,nan,nan,0

2020,3,7,6,5,33,
1,3,nan,nan,0

2020,3,7,6,5,36,1,3,nan,nan,0

2020,3,7,6,5,36,
1,3,nan,nan,0

2020,3,7,6,5,37,
1,3,nan,nan,0

2020,3,7,6,5,38,
1,3,nan,nan,0

2020,3,7,6,5,38,
1,3,nan,nan,0

2020,3,7,6,5,39,
1,3,nan,nan,0

2020,3,7,6,5,39,
1,3,nan,nan,0

2020,3,7,6,5,40,
1,3,nan,nan,0

2020,3,7,6,5,40,
1,3,nan,nan,0

2020,3,7,6,5,41,
1,3,nan,nan,0

2020,3,7,6,5,41,
1,3,nan,nan,0

2020,3,7,6,5,42,
1,3,nan,nan,0

2020,3,7,6,5,42,
1,3,nan,nan,0

2020,3,7,6,5,43,
1,3,nan,nan,0

2020,3,7,6,5,43,
1,3,nan,nan,0

2020,3,7,6,5,44,
1,3,nan,nan,0

2020,3,7,6,5,44,
1,3,nan,nan,0

2020,3,7,6,5,45,
1,3,nan,nan,0

2020,3,7,6,5,45,
1,3,nan,nan,0

2020,3,7,6,5,46,
1,3,nan,nan,0

2020,3,7,6,5,46,
1,3,nan,nan,0

Is it possible to make a line like this:

myFile.println(2020,3,7,6,5,31,1,3,nan,nan,0);

From all this printing lines:

    DateTime now = rtc.now();

    myFile.print(now.year(), DEC);
    myFile.print(F(","));
    myFile.print(now.month(), DEC);
    myFile.print(F(","));
    myFile.print(now.day(), DEC);
    myFile.print(F(","));
    myFile.print(now.dayOfTheWeek());
    myFile.print(F(","));
    myFile.print(now.hour(), DEC);
    myFile.print(F(","));
    myFile.print(now.minute(), DEC);
    myFile.print(F(","));
    myFile.print(SensorNum);
    myFile.print(F(","));
    myFile.print(SensorType);
    myFile.print(F(","));
    myFile.print(DHTTempValue);
    myFile.print(F(","));
    myFile.print(DHTHumValue);
    myFile.print(F(","));
    myFile.println(X_SensorValue);
    // close the file:
    myFile.close();

No, it is not possible. Have you consulted the reference documentation for println() on this site?

I suppose you think about this one:

arduino.cc/reference/en/language/functions/communication/serial/println/

yes, but it is not giving me the answer, on a specific issue, like this.

When it sometimes run fine and suddently break up the line.

Something I do not understand.

jokerper:
yes, but it is not giving me the answer, on a specific issue, like this.

The documentation can not detail all the 1,000,000,000,000 ways that it can not work… it can only list the several ways in which it can work. If a way is not listed there, you can usually assume that it will not work. If you are “mining” a function to find features that you think are missing from the documentation, the best way to find out is to read the open source code.

If you want to make your output statements more “inline”, you can use a streaming approach
https://github.com/geneReeves/ArduinoStreaming

Then you can say things like:

Serial << "The current date is " << day << "-" << month << "-" << year << "." << endl;

jokerper:
Is it possible to make a line like this:

myFile.println(2020,3,7,6,5,31,1,3,nan,nan,0);

Why? It's not going to do the job any better.

...R

Hi,

Anyone who can be a little more specific about any help.

As you proberly can see, I am beginner with very small knowledge in coding. Everthing in this code is sampled from other working codes.

How do I make the line:

myFile.println(2020,3,7,6,5,31,1,3,nan,nan,0);

So it not will break the line when sending data to SD card.

From all the printing lines???

jokerper:
Hi,

Anyone who can be a little more specific about any help.

Please. How could I possibly be more specific? I told you it's not possible. I gave you a link to a good alternative.

I wonder if you mean the print() function? It does not output a line break. Have you consulted the reference documentation for print() on this site?

I can see a pattern where it breaks the line every time:

2020,3,7,6,5,39,
1,3,23.00,33.00,0

What am I missing?

myFile.println(2020,3,7,6,5,31,1,3,nan,nan,0);

jokerper:
Hi,

Anyone who can be a little more specific about any help.

As you proberly can see, I am beginner with very small knowledge in coding. Everthing in this code is sampled from other working codes.

How do I make the line:

myFile.println(2020,3,7,6,5,31,1,3,nan,nan,0);

So it not will break the line when sending data to SD card.

From all the printing lines???

If your output has an extra line break in it, it's probably because you have accidentally used println() somewhere where you intended to use print().

aarg:
Please. How could I possibly be more specific? I told you it's not possible. I gave you a link to a good alternative.

I wonder if you mean the print() function? It does not output a line break. Have you consulted the reference documentation for print() on this site?

Sorry, But I think, to bring in antoher library and more code will do that I run out of memory.

Thats why I keep on working for get this to work.

In some kind of code modification in flash ram, eeprom or progmem. Any help on this maybe... link to a guide/documentation to look for.

thanks for your reply...
/jokerper

jokerper:
Sorry, But I think, to bring in antoher library and more code will do that I run out of memory.

The library in question does not consume any additional memory. So the author claims, at least, to the best of my recollection…

jokerper:
Hi,

Anyone who can be a little more specific about any help.

As you proberly can see, I am beginner with very small knowledge in coding. Everthing in this code is sampled from other working codes.

How do I make the line:

myFile.println(2020,3,7,6,5,31,1,3,nan,nan,0);

So it not will break the line when sending data to SD card.

From all the printing lines???

Solve the nan issue.

jokerper:
Any help on this maybe..

You've been asked to post ALL your code. It's really not possible to help you unless you do.

aarg:
If your output has an extra line break in it, it's probably because you have accidentally used println() somewhere where you intended to use print().

I do not see any line breaks with the code now:

  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print(F("Writing to test.txt..."));
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(F("Writing to file"));
    delay(1000);
    
    DateTime now = rtc.now();

    myFile.print(now.year());
    myFile.print(F(","));
    myFile.print(now.month());
    myFile.print(F(","));
    myFile.print(now.day());
    myFile.print(F(","));
    myFile.print(now.dayOfTheWeek());
    myFile.print(F(","));
    myFile.print(now.hour());
    myFile.print(F(","));
    myFile.print(now.minute());
    myFile.print(F(","));
    myFile.print(SensorNum);
    myFile.print(F(","));
    myFile.print(SensorType);
    myFile.print(F(","));
    myFile.print(DHTTempValue);
    myFile.print(F(","));
    myFile.print(DHTHumValue);
    myFile.print(F(","));
    myFile.print(X_SensorValue);
 
    // close the file:
    myFile.close();

It does not help with the "nan", because it break the before and it also break the line with the numbers readings.

jokerper:
I do not see any line breaks with the code now:

You're focusing on a section of code that appears to have no errors. That is why you must post ALL your code - it's somewhere else...