4

I used the code below to save output from an Arduino Uno to a text file.

The problem is whenever I run the code the old data in the text file is deleted.

I don't want the old data to be deleted, what should I change?

import processing.serial.*;
Serial mySerial;
PrintWriter output;
void setup() {
   mySerial = new Serial( this, Serial.list()[0], 9600 );
   output = createWriter( "data.txt" );
}
void draw() {
    if (mySerial.available() > 0 ) {
         String value = mySerial.readString();
         if ( value != null ) {
              output.println( value );
         }
    }
}

void keyPressed() {
    output.flush();  // Writes the remaining data to the file
    output.close();  // Finishes the file
    exit();  // Stops the program
}
2
  • 1
    See this SO answer for an example. Mar 18, 2015 at 7:40
  • I think the problem is with your file name it's the same every time as the same-named file with the same extension can not be stored. To solve you can add a variable before your file name that increments every time so your file name gets changed every time and you can store that variable in EEPROM for further use.
    – Dharmik
    Mar 16, 2021 at 11:38

2 Answers 2

1

There are a couple of ways to append to a text file.

  1. You can either read the entire file in a temporary variable, then append the new data to it and then write to file.

  2. you can implement using printWriter instead like mentioned here: https://processing.org/discourse/beta/num_1267767630.html

Let me know if it works.

-2

Load configuration data from the SPIFF file, return 0 if OK

  • SPIFFS open function needs path and mode:
  • mode = "r", "w", "a", "r+", "w+", "a+" (text file) or "rb", "wb", "ab", "rb+", "wb+", "ab+" (binary)
  • where r = read, w = write, `a = append
    • means file is open for update (read and write)
  • b means the file os open for binary operations
  • Returns 0 if OK else:
  • -1 = No SPIFFS file system
  • -2 = File does not exist
  • -3 = File too short
  • -4 = Checksum does not compare
1
  • 1
    This probably wasn't clear to you, but they're asking about on the host side, using Processing, which looks a Arduino code (for good reasons) but isn't actually Arduino code.
    – timemage
    Mar 17, 2021 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.