Problem with "startbit" during serial communication using processing

Hey,
I've to transfer 1000 analog values (integers) from the Arduino to processing.
This operation is repeated over and over, therefore i need a datastream start and stop marker.

Here is my concept example:

Arduino sends "-start-" then 6 (or 1000) values and then the "-end-" Signal.

In Processing i want to observe this markers so that any action can be triggered if "-start-" or "-end-" is recieved.

If my code would work, it should be a green screen in the begining, and a red one in the end, but this doesn't work, the screen is flashing blue.

Why??

I'm new to this so please be lenient :wink:

The Arduino-Code is:

void setup() {
  Serial.begin(115200);   
}
void loop() {  
    Serial.print("-start-");
    Serial.print("\n"); 
    delay(1000);  
    for (int i = 0; i < 5; i++) {
      Serial.print(i);
      Serial.print("\n");
      delay(1000); 
    }
    Serial.print("-end-");
    Serial.print("\n");  
    delay(10000);
}

My Processing-Code is:

  import processing.serial.*;
  
  Serial myPort;        // The serial port
  
  void setup () {
  size(400, 300);
  //frameRate(30);  
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 115200);
  myPort.bufferUntil('\n');
  background(0);
  }

  void draw () {
  // everything happens in the serialEvent()
  }

 void serialEvent (Serial myPort) {
    String zeichenkette = myPort.readStringUntil('\n');
  
   if (zeichenkette != null) {
     
      if (zeichenkette.equals("-start-") == true) {
       background(0,255,0);
      }
      if (zeichenkette.equals("-ende-") == true) {
      background(255,0,0);
      }
      delay(400);
      background(0,0,255);
      print(zeichenkette);

      delay(400);
      background(0,0,0); 
  }
  }

If my code would work, it should be a green screen in the begining, and a red one in the end, but this doesn't work, the screen is flashing blue.

The serialEvent() function is called each time a carriage return arrives. You send one after "-start-", one after each value, and one after "-end-".

When "-start-" arrives, you set the screen color to red, then wait a short period of time until you set the color to blue, print a value (that you don't show us), wait a short time, then set the color to black and return. A short time after you return, serialEvent() gets called again, and sees one of the values, sets the color to blue then black and returns. That gets repeated until "-end-" arrives, when you set the color to green for 0.4 seconds, then to blue, then to black.

It would be very easy to miss the red and green flashes since they are less than half a second in duration.

Now, what are you really trying to do?

Thank you for your reply.

In general I want to write a string like “start” on my arduino to the serial port, which processing receives. The received string has to be compared to a string in processing giving true/false.
All this is needed, because I want to read out a ccd line sensor with my arduino. The data from the sensor is stored in an array and transferred to processing (which is painting a graph etc.).
My problem is that I want to repeat that as quickly as possible, because I want to have a high refresh rate for my sensor.
Therefore I want to have the arduino sending not only the pixel data but also an “event-marker” this for example should tell processing where a new array of data begins.
This little code here is just to test the behavior of these event-markers.
My arduino writes –start- to serial and processing receives this (I can see that it is there, because of the print(zeichenkette) command). But for a reason I don’t understand, if I compare the received “-start-“ string to a “-start-“ string in processing it does not result as true.
So I guess the received “-start-“ string is of another type of data or sth. like this.

I’ve added a shorter and more easy example to understand what I mean. I also added some explanation to the code:

Code for Processing:

  import processing.serial.*;
  
  Serial myPort;        // The serial port
  
    
  void setup () {
  size(400, 300);
  //frameRate(30);  
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 115200);
  myPort.bufferUntil('\n');
  background(0);
  }

  
  void draw () {
  // everything happens in the serialEvent()
  }

  void serialEvent (Serial myPort) {
  String zeichenkette = myPort.readStringUntil('\n');        //reads out the serial port to the string called zeichenkette
  
   if (zeichenkette != null) {       //makes shure that string is only treated if it is not null
     
      if (zeichenkette.equals("-start-") == true) {     //Problem point: Here it results every time in false, even if the arduino performs Serial.print("-start-")
       background(0,255,0);    // only to have a visible indicator that hte if-request resulted in true 
      }
      print(zeichenkette);     //writes the string (recieved from arduino) to the console to show me, that the data really arrived in processing

      delay(4000);       //gives enough time to take a view on the black or green window
      background(0,0,0);    //resets the background to black for another round
  }
  }

And code for arduino:

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

void loop() {    //just sending "-start-"s
    Serial.println("-start-");
    //Serial.print("\n"); 
  
    delay(10000);
}

But for a reason I don’t understand, if I compare the received “-start-“ string to a “-start-“ string in processing it does not result as true.
So I guess the received “-start-“ string is of another type of data or sth. like this.

Most of us are beyond the stage in life where text-speak holds any appeal. This is NOT the place for that crap.

You need to print the string received by Processing inside delimiters, like so:
print("I got <");
print(zeichenkette);
print("> from the Arduino".);

You need to share that output with us. It is likely that you are sending "-start-", and that Processing calls serialEvent() and removes "-start-" from the buffer, storing "-start-" in zeichenkette.

Next time around, the is still in the serial buffer, so it gets read and stored in zeichenkette, in front of the data you expect.

The serialEvent() function should only set the color of the background ONCE per invocation. "as quickly as possible" and delay() do NOT belong in the same post.

Try something like this...
It uses the pause between sending data and reading the next set as the end marker. I think its the last working version I had.

ILX511_v4_MEGA.ino (5.78 KB)

ILX511_v4_MEGA.pde (3.04 KB)