communication with processing messed up

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?

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: frameCount / Reference / Processing.org

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.

if i test this in processing:

void setup() {
  char t = 'a';
  t = '\0';

  println(t);
}

then it prints nothing, if i comment the t = '\0'; line then it prints 'a' to the console.

So in this part of your code:

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

Shouldn't this line: inData[index] = '\0';
not be one line higher? (and then ++ switched afcorse).

And about the interfacing serials, i stripped the processing code to this:

import processing.serial.*;

Serial port;

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

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

my serial monitor output is this:

Received: <>
Receivived: <>
Received: <>
Received: <>
Received: <>
Received: <>
Received: <>
Received: <>
Received: <>
Received: <>

then it prints nothing

When you set t to NULL, it makes it "nothing", so when nothing is printed, that is the expected behavior.

Setting a character to NULL, and setting an element in a character array to NULL have much different results.

Shouldn't this line: inData[index] = '\0';
not be one line higher? (and then ++ switched afcorse).

No, it shouldn't.

The elements in an array are like boxes in a post office. There is one letter in each box. If you open the boxes in order, and read each letter, until you encounter an empty box, you make something of the letters you have read.

The "empty box" is the NULL character in a char array.

What that code is doing is putting a letter in a box, and removing the letter from the next box, saying "stop reading when you get here".

i stripped the processing code to this

my serial monitor output is this:

I'm not sure how many more times I need to say this. There can only be one application on each end of the serial port. The Arduino is on one end. If the serial monitor is on the other end, Processing can not be there, too. So, the Arduino gets no data.

If the Processing application is there, then the Serial Monitor should not be trying to intercept the output.

You can make Processing read the responses from the Arduino. That is the ONLY way that you can debug the communications process.

Until this sinks in, I can't help you any more.

ok got it :slight_smile:
And i used a pot meter for now. I will give it another attempt later.