Log data from Arduino Serial to .txt file

Hi all,
I know this question pops up on the web a lot but so fa I havent’ been able to find one solution than works for me. I am a complete newbie to Java and Processing, so please be patient with me.
I have an Arduino Sketch that writes on the serial monitor a series on numbers (later I will need these to be readings from sensors but for the moment I’m practising on this)

`

void setup() {
  // put your setup code here, to run once:
  #include <Wire.h>
  Serial.begin(9600);
  Serial.println("Arduino-Processing Communication Test"); Serial.println("");
}

void loop() {
  // put your main code here, to run repeatedly:
  int x = 0;
  while(x<=20)
  {
    Serial.println(x);
    x++;
    delay(1000);
  }
  while(1){}
}`

And this is my processing sketch that should be creating a .txt file with the data from Arduino:

//From Arduino to Processing to Txt.
//import
import processing.serial.*;
//declare
PrintWriter output;
Serial udSerial;

void setup() {
  udSerial = new Serial(this, Serial.list()[0], 9600);
  String filePath="C:/Users/Beatrice/Documents/Tesi Magistrale/prova.txt";//
  output = createWriter (filePath);
}

  void draw() {
    // printArray(Serial.list()); // to check serial
    if (udSerial.available() > 0) {
      if(Serial.list()[0] == "COM3") {
      String SenVal = udSerial.readStringUntil('\n');
      if (SenVal != null) {
        output.println(SenVal);
      }
      }
    }
  }

  void keyPressed(){
    output.flush();
    output.close();
    exit();
  }

My procedure is : plug in Arduino (the sketch is already one) and then run the Processing code for about 1 minute, then stop.
The file is correctly created in the specified location but remains empty. Using println(SenVal) as debugger I find out that actually the values from Arduino are never passed to Processing apparently (ie the console remains empty).
I also double checked my serial number, but apparently I only have COM3 as possibile option so there is no actual possibility to mistake that. The port is correct
Can somebody please help? Thanks

1 Like

Please format your code :blush:

It consist on these two steps:

  1. In your code editor (PDE, VS code, Eclipse, etc) ensure you execute the beautifier function. This function automatically indents your code. Auto-indenting makes your code easier to read and helps catching bugs due to mismatch parenthesis, for instance. In the PDE, you use the key combination: ctrl+t
  2. You copy and paste your code in the forum. Then you select the code and you hit the formatting button aka. the button with this symbol: </>

That’s it! Please notice you do not create a new post in case you need to format something you already posted. You can edit your post, copy the code to the PDE, indent the code properly there and then past it back here, format the code and >> save << the edits.

Extra info:

Formatting your code makes everybody’s life easier, your code looks much better plus it ensures your code integrity is not affected by the forum’s formatting (Do you know the forum processes markup code?) Please visit the sticky posts or the FAQ section/post to learn about this, other advantages and super powers you can get in this brand new forum.

Kf

Do you get data in the arduino monitor? Notice you cannot run the arduino monitor at the same time as Processing (when processing is accessing the serial port)

To verify what port you are connected, you can do that through the Arduino IDE. In that interface, you need to specify what port you are connecting to and at what speed. After you load your ino code into your arduino unit, you close the arduino monitor (if it is open) and then start your processing code, matching the com port and speed as described before.

I will not do save the data to the hard drive for the starters. Instead, print the data into the console. I have modified the code for you below. Also check GoToLoop’s post here

Kf

#include <Wire.h>

int x;

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);
  Serial.println("Arduino-Processing Communication Test"); 
  Serial.println("");
  x=0;
}

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

  Serial.println(x++);
  delay(1000);
}

//From Arduino to Processing to Txt.
//import
import processing.serial.*;


//declare
PrintWriter output;
Serial udSerial;
String senVal;

void setup() {
  size(400,600);
  background(145,25,225);
  udSerial = new Serial(this, Serial.list()[0], 9600);
  //String filePath="C:/Users/Beatrice/Documents/Tesi Magistrale/prova.txt";//
  //output = createWriter (filePath);
}


// https://processing.org/reference/libraries/serial/index.html

void draw() {
  println(senVal);  //It is updated every time a new package arrives
}

void serialEvent(final Serial s) {
  
  String rx= = udSerial.readStringUntil('\n');
  
  if(rx!=null){
    sentVal=rx;
  }
}
1 Like

Yes, I do get the data on Arduino monitor. I started exactly from seeing the data on the serial monitor and then I decided I wanted them saved and tried this code. And also noticed I could not see serial monitor while Processing was running. I also checked already that the COM was correct,and in fact Processing only shows that Serial.list() = “COM3”. It is because the code still did not work that I asked for help.
By the way, I understood that the txt stayed empty because it could not enter the keyPressed statement, as I noticed that I could press all the keys I wanted but if the window of Processing was not selected it did not work. Stupid me.
As for why it did not show the values on the console, that I don’t really understand yet, but for the moment I’m happy as I got my .txt filled out.
Thank you very much anyway

I did ctrl+C and ctrl+V the code in the page but the indentations were lost, thanks for the tip

Yes, this is a typical problem working with key events unfortunately. You need to ensure the sketch windows gets the focus so to process any key events. Sometimes, when loading the sketch, Processing gives the focus automatically. Most of the times, you have to do this yourself as you have stated above. I have never figured out why this is not consistent. Good to hear it is working now.

Kf

1 Like