-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!

6
  • Don't know "processing" but do you close the file at the end? Because if you open it, write and then let it open, the file size stay at 0 and you can't read data. So maybe you really write data but they stay invisible because of the fact you don't close the file.
    – Peter
    Commented Nov 24, 2017 at 10:31
  • Don't post images of text; post the text itself. No one is going to retype your code to test it and you will get no help. And, what is your question?
    – user31481
    Commented Nov 24, 2017 at 10:36
  • The Arduino has simple task of sending data to your computer, and does not create any files. Check your Processing script. As mentioned by Peter, be sure you closing file after storing anything into it.
    – smajli
    Commented Nov 24, 2017 at 10:41
  • 1
    Processing questions are off-topic.
    – gre_gor
    Commented Nov 24, 2017 at 13:08
  • 1
    I'm voting to close this question as off-topic because it is about Processing and not about Arduino. While similar in heritage, writing files on the development machine with Processing is not an Arduino question. Commented Nov 24, 2017 at 17:16

1 Answer 1

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.

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