-1

Data can be seen in the console but the data is not being written in the text file. The file is created but there is no data in it.

Here is my code in Processing,

import processing.serial.*;
Serial myPort;  // Create object from Serial class
String val;     // Data received from the serial port
PrintWriter output;
void setup()
{

   String portName = Serial.list()[0]; 
   myPort = new Serial(this, portName, 9600);
   output = createWriter( "my.txt" );
}
void draw()
{
    if ( myPort.available() > 0) 
     {  // If data is available,
         val = myPort.readStringUntil('\n');       
       output.print( val );
     } 
    println(val); //print it out in the console

 }

and in Arduino,

  int potPin = 0; // initialize analog pin 0 for LM35 temperature sensor
  void setup()
  {
     Serial.begin(9600);// set baud rate at”9600”
  }
  void loop()
  {
     int val;// define variable
     int dat;// define variable
     val=analogRead(0);
     dat=(125*val)>>8;// temperature calculation formula
     Serial.print("Tep:");//
     Serial.print(dat);// output and display value of dat
     Serial.println("C");// display β€œC” characters
     delay(500);// wait for 0.5 second
   }

Any answers are appreciated. Thank you!

stefanie
  • 9
  • 3

1 Answers1

2

createWriter description says:

"For the file to be made correctly, it should be flushed and must be closed with its flush() and close() methods"

Source: https://processing.org/reference/createWriter_.html
This question is not about Arduino, but Processing language.

smajli
  • 472
  • 3
  • 12