Processing to Arduino, mistakes while transfering data

Hello everyone, I allready tried to send data from processing to arduino. The problem is that not all numbers arrive at arduino.

I see this Serial Monitor

2234
42
42
242
2242
4
2
4

instead of every line repeating 234

My arduino code

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

void loop()
{
  if(Serial.available() > 0 )
  {
    recieve=Serial.read();
    
    Serial.println(recieve);    
  }
}

my processing code

import processing.serial.*;
Serial myPort;
int send=234,;

    void setup()
    {
      String arduinoPort = Serial.list()[0];       
       myPort = new Serial(this, arduinoPort, 9600);
       myPort.write(2);
    }
    
    void draw()
    {
     myPort.write(send);
     print("sent ");
     println(send);
     delay(2000);
    }

Thanks

P.S. I know that I can only send from Processing to Arduino 8byte on data. 234<255.

I have some doubts about it. It seems to me that arduino dont get the data because of some reasons.
The one is maybe the cable issue, contact or the length of it ??? I allready tried with another one, doesnt work

another ideas?

2nd p.s. the strange thing is if i send data from arduino to processing it works perfectly

I see this Serial Monitor

Maybe you ought to close the Serial Monitor, then. Windows will (properly) only allow one application to connect to the serial port at a time. Having Processing AND Serial Monitor connected to the serial port, at the same time, on wonky operating systems is not a good thing.

well, I do all this on Mac and you are probably right. But how can I know then whether the data is sent correct or not, if I' can't open the Serial monitor?

I was allready thinking about the processing program. I think it doesn't depend on it, because I use the newest one. And also don't need a library for communication with arduino, it's in processing implemented.

Should I try asking people on Processing Forum?! I dont know, seems to me that is a hardware issue. hm?!

But how can I know then whether the data is sent correct or not, if I' can't open the Serial monitor?

You have to find another way. Say a software serial emulator with a TTL level USB interface. Or echo back what you recieve and use processing to see what is recieved, it should be the same as what you sent.

But how can I know then whether the data is sent correct or not, if I' can't open the Serial monitor?

You don't seem to be aware that Processing can READ from the serial port, too, and print in its window.

PaulS I are right I didnt pay enough attention on this effect that serial monitor can cause. Good that you mentioned it, somehow I forgot it.

But how can I know then whether the data is sent correct or not, if I' can't open the Serial monitor? You have to find another way. Say a software serial emulator with a TTL level USB interface.

-> no idea about it

Or echo back what you recieve and use processing to see what is recieved, it should be the same as what you sent.

-> just did it, the result you can see beneath

P*rocessing said:*

Stable Library

Native lib Version = RXTX-2.1-7 Java lib Version = RXTX-2.1-7 RXTX Warning: Removing stale lock file. /var/lock/LK.012.011.028 sent 234 recieved -1 sent 234 recieved -1 sent 234 recieved -1 sent 234 recieved -1 sent 234 recieved -1 sent 234 recieved -1 sent 234 recieved -1 sent 234 recieved -1 sent 234 recieved -1 sent 234 recieved -1 sent 234 recieved 234 sent 234 recieved 234 sent 234 recieved 234 sent 234 recieved 234 sent 234 recieved 234

Well the first recieved data I can't explain why, normaly when there is no data sent back it returns -1. But after a while it became more stable.

Showing us output without showing the sending and receiving isn't all that helpful.

the code

arduino

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

void loop()
{
  if(Serial.available() > 0 )
  {
    recieve=Serial.read();
    delay(50);
    Serial.write(recieve);    
  }
}

processing

import processing.serial.*;
int val;
Serial myPort;
int send=234;

    void setup()
    {
      String arduinoPort = Serial.list()[0];       
       myPort = new Serial(this, arduinoPort, 9600);
       myPort.write(2);
    }
    
    void draw()
    {
     myPort.write(send);
     print("sent ");
     println(send);
     delay(50);
     val = myPort.read();
     print("recieved ");
     println(val);
    }

Why the delay in the arduino side? Get rid of it. Why are you not checking serial availability on the processing side? Those two points address your perceived problem.

Hi I have just written my first (and only) processing sketch myself and had similar problems as you but solved them when I noticed the following:

The type has to be the same i.e. if you want to receive a byte, you have to send a byte as they are stored differently. A byte is one byte and an int is 2 bytes etc. I notice you have mixed these.

If you want to send something longer than a byte then you need to know when you have all of its component parts before converting it into something else such as an integer.

I used an I2C 16x2 LCD display connected to the Arduino to print any debugging messages as the serial monitor won't work at the same time as processing has the port.

I hope this helps Mrs Z

You should check the Serial Monitor baud rate too. It is supposed to be 9600bps.