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

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
EDCisBack
  • 41
  • 2
  • 11
  • 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? – Kevin Workman Feb 12 '17 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? – EDCisBack Feb 12 '17 at 23:28
  • What is the length of the array returned by `Serial.list()`? Which `if` statement is not entered? – Kevin Workman Feb 12 '17 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! – EDCisBack Feb 12 '17 at 23:35

1 Answers1

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.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • 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. – EDCisBack Feb 13 '17 at 15:20