-1

I am currently trying to get a value (Yes!) from the Arduino to a text file (data.txt).

The problem is that the data isn't being read from the Arduino's Serial. When I tried to simple print the value to the prompt in processing, I came out empty handed.

Below is my code for the Arduino

//Just a basic program to write to the Serial the word/phrase; `Yes!`.

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  Serial.println("Yes!");
}

Below is my code for processing:

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
}

Yes, this code was found from this stackoverflow question.

Any help would be greatly appreciated!

4
  • You're going to have to do some debugging. Are you sure you only have one serial connection? What is returned from Serial.list()? Which if statement isn't entered? Use print statements to find out. What happens if you just print to the console instead of to a file? Commented Feb 12, 2017 at 23:01
  • Nothing happens when I print to the console, it isn't reading the values properly. What would happen if I have more than one serial connection? Commented Feb 12, 2017 at 23:28
  • What is the length of the array returned by Serial.list()? Which if statement is not entered? Commented Feb 12, 2017 at 23:33
  • Wow, that was really stupid of me. I was calling COM1, instead of my intended COM3 while using Serial.list(). Thanks @KevinWorkman! Commented Feb 12, 2017 at 23:35

1 Answer 1

0

Check out this line:

mySerial = new Serial( this, Serial.list()[0], 9600 );

This line assumes you only have one serial connection. If you have more than one serial connection, you're going to have to change the hard-coded index being used in this line.

Sign up to request clarification or add additional context in comments.

1 Comment

Just had to change the hard coded 0 to a 1. If your looking for the COM port, keep on printing the value to the console or simply hard code the com you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.