communication with processing messed up

I just want to send a delay value with arduino but i thought let's start with sending the frameCount of the processing sketch.

arduino:

    #define LED 13

    int incoming;

    void setup() {
      Serial.begin(9600);
      pinMode(LED, OUTPUT);
    }

    void loop() {
      if (Serial.available()) {
        incoming = Serial.read();
        Serial.println(incoming);
      }
    }

Processing:

    import processing.serial.*;

    Serial port;

    void setup() {
     println(Serial.list());
     port = new Serial(this, Serial.list()[1], 9600);
    }

    void draw() {
      port.write(frameCount);
     
    }

This is the output i get in the Serial monitor from arduino (exept the last line): Apart from it being messed up, also a lot of frames are not coming in...

123 5 7 8

1112 3 15

18

2122 3 2526 7 29 31 32

3536 7

How many times do you suppose the draw() method in the Processing application is called, while the Arduino reads the serial data that it got? draw() is sending data far faster than the Arduino can read it, so the Arduino's receive buffer fills up. Data that arrives while the buffer is full is placed in the bit bucket. There is no method to retrieve data from the bit bucket.

Your Processing application has not implemented a handler (serialEvent) to be called when serial data arrives, so that data is not processed. Not processing data is far faster than processing it.

The speed at which draw() is called is only marginally affected by serial data processing. You need to add a delay to draw, so that data is sent at a more manageable (by the Arduino) rate.

I tried with a delay of 1000 in processing but this didn’t help.
Also a frameRate(1); didn’t help, still bad output (only slower).

Now i tried the followong code, arduino serial monitor has output like this:

9
69
9989999999
10
19
299
1399499959199
7899999999
2099299
299
329
599

And processing is printing null all the times.

#define LED 13

int incoming;

void setup() {
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
}

void loop() {
  if (Serial.available()) {
    incoming = Serial.read();
    String out = incoming;
    out += 9999;
    Serial.println(out);
  }
}
import processing.serial.*;

Serial port;
String inString;
int lf = 9999;

void setup() {
  frameRate(1);
 println(Serial.list()); 
 port = new Serial(this, Serial.list()[1], 9600);
 port.bufferUntil(lf);
}

void draw() {
  port.write(frameCount);
  println(inString);  
}

void serialEvent(Serial port) {
  inString = port.readString();  
}

p.s. the function delay will be removed in next processing

out += 9999;

You are adding a String and a number. Is that really what you want to do?

Processing can be told to buffer until the carriage return/line feed that the Arduino can send, using Serial.println().

Processing is not putting any separator between the strings that it sends. So, the Arduino has no way of knowing where a number begins and ends.

ItwouldbelikeyoutryingtoreadasentenceItypedwithnospacesbetweenthewords.

p.s. the function delay will be removed in next processing

I don't see one there, now.

You can do without the delay, if you have Processing send only when the Arduino says that it is ready.

Alright, here the line feed is a new line, but this also doesn't work...

void loop() { if (Serial.available()) { incoming = Serial.read(); String out = incoming; Serial.println(out); Serial.println(9999); } }

    incoming = Serial.read();
    String out = incoming;
    Serial.println(out);
    Serial.println(9999);

Storing the one character read into a String object is a waste of resources.

What is the 9999 being sent for?

For every character read, the Arduino sends 9 back out...

but this also doesn't work...

What a shame.

If you described what did actually happen, you might get help instead of just (fake) sympathy.

the 9999 was sent for the bufferUntil, i had a high int so it would be higher then the delay i want to send later. Now i use the char 'h'

For every character read, the Arduino sends 9 back out...

how's that?

Arduino:

#define LED 13

int incoming;

void setup() {
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
}

void loop() {
  if (Serial.available()) {
    Serial.println(Serial.read());
    Serial.println('h');
  }
}

processing:

import processing.serial.*;

Serial port;
String inString;
char lf = 'h';

void setup() {
  frameRate(1);
 println(Serial.list()); 
 port = new Serial(this, Serial.list()[1], 9600);
 port.bufferUntil(lf);
}

void draw() {
  port.write(frameCount);
  println(inString);  
}

void serialEvent(Serial port) {
  inString = port.readString();  
}

processing output is like:

h

h

h

5 h

5 h

h

8 h

8 h

90 h

90 h

90 h 1

2

h

h

1 h 6 h 6 h 1h

and arduino output is like:

1 h 2 3 h 4

6 h7

h 1 1h1h13 14 5

1 7 h 18 19 h

how's that?

    incoming = Serial.read();
    String out = incoming;
    Serial.println(out);
    Serial.println(9999);

The first line reads one character.

The 3rd line prints that character plus a carriage return and line feed. 3 characters. The 4th line prints '9', '9', '9', '9' and a carriage return and line feed. 6 characters. 6 + 3 = 9.

  if (Serial.available()) {
    Serial.println(Serial.read());
    Serial.println('h');

Now, you are sending the character plus a carriage return and line feed, the 'h' plus a carriage return and line feed, for a total of 6 characters.

Perhaps the Serial.println()s should be Serial.print()s to get rid of the carriage returns and line feeds.

I don't know what frameCount is, or how or where it is initialized or incremented. So, I can't comment on the output you show.

How are you capturing the Arduino output separate from the Processing output? You have one serial connection. Either the Serial Monitor should be on the other end (the Arduino is on one end) OR the Processing application should be on the other end.

What OS are you using?

What, exactly, is the problem we are trying to address?

Then the bufferUntil is useless right?
http://processing.org/reference/libraries/serial/Serial_bufferUntil_.html

The 3rd line prints that character plus a carriage return and line feed. 3 characters.

A carriage return is like pressing enter/return on the keyboard right? So isn’t that a line feed allready?

frameCount prints the current frame number, so when you start a processing sketch it starts with 1 and then it just counts.
It would be like this in arduino (only in processing it’s a default thing, you don’t have to create the variable frameCount)

int loopCount = 0;
void loop() {
    loopCount++;
}

How are you capturing the Arduino output separate from the Processing output? You have one serial connection. Either the Serial Monitor should be on the other end (the Arduino is on one end) OR the Processing application should be on the other end.

The processing pde has a console, if i use println(“something”); then it goes to the processing console, i could also create a printWriter to create a textfile for example then i can use output.println(“something”); for example and it gets added to the textfile.

What OS are you using?

mac os x, lion

What, exactly, is the problem we are trying to address?

I want to set a simple delay with processing. Setting a delay between 0 and 255 is quite easy for that just send a byte, however i need a bigger range. I think something between 0 and 1000ms. Afcorse i could use a byte and multiply it by 4 by the arduino which is more easy (but 4 times less precise) but for the purpose of learning i don’t.
So at the moment i just try to send a value with processing, receive it with arduino and then send it back to processing (<< that is my goal).
I used frameCount for now cause it’s easy to see if things are going correct since it’s produces 1,2,3,4,5,6,7,8,9,10,11 etc. If you look in the output below, you see a bit of it coming back.

Then the bufferUntil is useless right?

Not at all.

A carriage return is like pressing enter/return on the keyboard right? So isn't that a line feed allready?

Ever seen a typewriter? There is a lever that you use to return the carriage to the left side of the typewriter, and advance the paper so you can type on the next line. So, the carriage return and line feed are separate activities. The enter key on a PC emulates the carriage return/line feed. How do you suppose it does that? Hint: it was two separate activities.

frameCount prints the current frame number, so when you start a processing sketch it starts with 1 and then it just counts.

"it just counts" when? Details are crucial. What causes it to increment?

The processing pde has a console, if i use println("something"); then it goes to the processing console

That explains how you know what Processing received. You seem to know what the Arduino sent, too. How?

Afcorse i could use a byte and multiply it by 4 by the arduino which is more easy (but 4 times less precise) but for the purpose of learning i don't.

Afcorse.

I used frameCount for now cause it's easy to see if things are going correct since it's produces 1,2,3,4,5,6,7,8,9,10,11 etc. If you look in the output below, you see a bit of it coming back.

Then why complicate with that 9999 or h crap. Just use the Serial.println() function on the Arduino and bufferUntil('\n'); in the Processing application.

The Processing application sends a string ("0", "1", ... , "10", "11"). You need to read that string, convert it to a number, and send that number back to Processing (as a string) to validate the communication process.

Ever seen a typewriter? There is a lever that you use to return the carriage to the left side of the typewriter, and advance the paper so you can type on the next line. So, the carriage return and line feed are separate activities. The enter key on a PC emulates the carriage return/line feed. How do you suppose it does that? Hint: it was two separate activities.

I understand it with a typewriter, but not with arduino.

frameCount prints the current frame number, so when you start a processing sketch it starts with 1 and then it just counts.

"it just counts" when? Details are crucial. What causes it to increment?

from: http://processing.org/reference/frameCount.html

The system variable frameCount contains the number of frames displayed since the program started. Inside setup() the value is 0 and and after the first iteration of draw it is 1, etc.

So every iteration of draw increments it.

The processing pde has a console, if i use println("something"); then it goes to the processing console

That explains how you know what Processing received. You seem to know what the Arduino sent, too. How?

by the serial monitor, or don't you mean that?

I used frameCount for now cause it's easy to see if things are going correct since it's produces 1,2,3,4,5,6,7,8,9,10,11 etc. If you look in the output below, you see a bit of it coming back.

Then why complicate with that 9999 or h crap. Just use the Serial.println() function on the Arduino and bufferUntil('\n'); in the Processing application.

bufferUntil('\n'); is a good idea! thanks! wish i could test it right away.

So every iteration of draw increments it.

OK. Facts are good.

I understand it with a typewriter, but not with arduino.

The Serial.println() function outputs the data specified followed by a carriage return and a line feed. They are separate ASCII characters (10 and 13).

by the serial monitor

On Windows, you can have the Serial Monitor communicating with the Arduino, OR Processing communicating with the Arduino, but not both. I have no idea what that isn't the same on other OSs.

is a carriage return there so it knows that the output of the data specified is complete?

And if so, wouldn't it then outputs the data specified followed by a line feed and the the carriage return? (instand of carriage return and then line feed).

cause Serial.print("something\n");

produces the same as Serial.println("something");

cause Serial.print("something\n");

produces the same as Serial.println("something");

Your proof?

no i assume, in processing those results will be the same:

print("something\n"); println("something");

print("something\n\r") (or print("something\r\n") (I can never remember the correct order)) and println("something") are equivalent.

I used port.bufferUntil('\n') But still no luck. How hard can it be?

Serial monitor:

1 2 3 4

6 8 0 1 2 1151671 120

5

7

9

1

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

void loop() {
  if (Serial.available()) {
    Serial.println(Serial.read());
  }
}
import processing.serial.*;

Serial port;
String inString;

void setup() {
  frameRate(1);
 println(Serial.list()); 
 port = new Serial(this, Serial.list()[1], 9600);
 port.bufferUntil('\n');
}

void draw() {
  port.write(frameCount);
  println(inString);  
}

void serialEvent(Serial port) {
  inString = port.readString();  
}
  port.write(frameCount);

This is writing the frame count out as a string - NOT A SINGLE CHARACTER.

  if (Serial.available()) {
    Serial.println(Serial.read());
  }

This is reading ONE CHARACTER and pretending that it is the COMPLETE frame count. It, obviously, is NOT.

Put this in the Processing application, instead.

port.write("[");
port.write(frameCount);
port.write(']");

Try this on the Arduino:

bool started = false;
bool ended = false;

char inData[20];
byte index;

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

void loop()
{
   while(Serial.available() > 0)
   {
      char inChar = Serial.read();
      if(inChar == '[')
      {
        started = true;
        ended = false;
        index = 0;
        inData[index] = '\0';
      }

      else if(inChar == ']')
      {
         ended = true;
         break;
      }

      else
      {
         if(index < 19)
         {
            inData[index++] = inChar;
            inData[index] = '\0';
         }
      }
   }

   if(started && ended)
   {
      Serial.print("Received: <");
      Serial.print(inData);
      Serial.println(">");
   }
}

PaulS:

  port.write(frameCount);

This is writing the frame count out as a string - NOT A SINGLE CHARACTER.

The frameCount keeps incrementing so after a while it get’s higher then 255/256 which is the maximum character value if i’m correct.

  if (Serial.available()) {

Serial.println(Serial.read());
  }



This is reading ONE CHARACTER and pretending that it is the COMPLETE frame count. It, obviously, is NOT.

i must have looked over that, thanks, good to know

I tryed the code you posted, thanks for the contribution!!
What does this line do?
inData[index] = ‘\0’;

(that line is also in the if(index < 19) part, where it is not needed i believe).

Unfortunatly it’s still not working:
My serial monitor output is:

Recived
Receivd: <>
Receied:
Recived

Red: <>
Receied: Received

Receive:

Rceived: a>
ece

Receive:

R
Received: a>
ved:

Recived>
Rceivd: >
ved: <>

and the processing console shows:

R<>
eive: <

RecReceiveived: < <>

Rece:

ived: <>

Reed: < >

Received>

Rved: < >

Receied: <

What do i need for bufferUntil (or nothing)?

port.bufferUntil(’\n’);

import processing.serial.*;

Serial port;
String inString;

void setup() {
  frameRate(1);
  println(Serial.list()); 
  port = new Serial(this, Serial.list()[1], 9600);
  //port.bufferUntil('\n');
}

void draw() {
  port.write("[");
  port.write(frameCount);
  port.write("]");
  println(inString);
}

void serialEvent(Serial port) {
  inString = port.readString();
}

Your boolean started was never set back to false so it could be removed.
I also removed the other boolean and ended up with this:
(afcorse still no frameCount cause i continued on something that didn’t work).

char inData[20];
byte index;

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

void loop()
{
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == '[')
    {
      index = 0;
      inData[index] = '\0';
    }

    else if(inChar == ']')
    {
      Serial.print("Received: <");
      Serial.print(inData);
      Serial.println(">");
      break;
    }
    else
    {
      if(index < 19)
      {
        inData[index++] = inChar;
      }
    }
  }


}

The frameCount keeps incrementing so after a while it get’s higher then 255/256 which is the maximum character value if i’m correct.

Yeah. So? Big difference between a string (an array of characters) and a character. When frameCount is 2,348,448,998,723, Processing will still be able to send it as a string.

What does this line do?
inData[index] = ‘\0’;

It adds a NULL terminator. When index is 0, it effectively empties the array.

(that line is also in the if(index < 19) part, where it is not needed i believe).

Yes, it is.

I’m beginning to think that your problem is that you have two applications trying to read the serial data sent by the Arduino, and they are interfering with each other. I do not know how you are able to do that, but you really need to stop doing it.