Log data from Arduino Serial to .txt file

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