Inconsistent data transmission from processing to arduino

Hi folks,

I am trying to send integer data from a GUI that I built up in PROCESSING to ARDUINO. There is only one variable that should assume integer numbers between 100 and 200.

The arduino code is this:

int val;
int val_set;

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

void loop() {
   if (Serial.available()) 
   { 
     val = Serial.read(); 
   }
   
   if (val != val_set) {   
     Serial.println(val);
   }

   val_set = val;
}

And the processing code is this:

import processing.serial.*;

int sliderValue = 0;
String val;   

Serial myPort; 

void setup() 
{
  String portName = Serial.list()[32]; 
  myPort = new Serial(this, portName, 9600);
  
  size(640,480);

  // SLIDER SETUP
     
}

void draw() {

    myPort.write(sliderValue);
    delay(50);
}

First it seems, that everything works fine but sometimes the date isn't transmitted correctly as you can see in that screenshot made off the Serial Monitor of arduino IDE.

As I want to take that data for controlling an engine in a little testbed, it is important to get validated data.

I hope, someone have an idea about the mistake I made or the problem that I provoked

Thanks in advance!
Konrad

Sorry!
Screenshot is here:

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

...R

Hi Robin,

Thanks for the advice but that particularly doesn't solve the problem at all. I modified my sending data to "<"number as string">" and copied example 3, but the Serial Monitor seems to be the problem as it often echoes just "This ju" or "Thi" and then abruptly aborts. After sending two or three data packages, all is displayed correctly again.

What could that be?

kone:
What could that be?

Without seeing your Arduino program I have no idea.

...R

The arduino program is exactly the same as you posted in example 3 in your tutorial.

I think I misunderstood your Reply #3 to mean that you had incorporated my code into your Arduino program.

However please post the exact program that YOU have uploaded to your Arduino - just in case some small error has crept in.

What Arduino board are you using?

Can you also post an example of the message that Processing sends (i.e. print the message from within Processing).

If the message is like this <123> then I would expect my example program to receive it correctly.

...R

I am also having issues like this with Processing to Arduino, Arduino to Processing and Arduino to Arduino Bluetooth transmissions.

I have implemented a heavily modified version of Robin2's method with start and end markers, but I still get transmissions like <123><123> or <123<123>, and while Robin2's example will deal with the first transmission example, the second one gets passed through if you are using Robin2's original example.

This is especially a problem when a sketch is uploaded and run for the first time.

JSchiltz19:
I have implemented a heavily modified version of Robin2's method with start and end markers,

That may be the problem :). There is an adage "if it works, don't fix it"

If you want help with your project it is probably best to start your own Thread and post YOUR program rather than confuse this Thread with two different projects.

...R

There is an adage "if it works, don't fix it"

The issue was that it didn't work. :confused:

I will start a new thread though because while I have a an issue that I think is related there is no benefit

for kone in me posting that here.

Sorry for any confusion.