Processing, Arduino and Linux

Thank you for reading my first post. This question relates to the Simple Write serial example.

  String portName = Serial.list()[0]; 
  myPort = new Serial(this, portName, 9600);

This doesn’t find the correct port but it does if I change [0] to [32] or do the following:

myPort = new Serial(this, "/dev/ttyUSB0", 9600);

I’m inclined to use the latter, is that the correct approach?

Also, processing, again using the simple write example, continually sends serial data to the Arduino. Is that the way it should work? Moving the mouse over the circle does send the correct data and does turn the led on and off but I’m concerned about the continual sending of data. I expected data to be sent only while the mouse is over the circle.

1 Like

https://processing.org/reference/libraries/serial/Serial.html
show you that you can get a list of the ports
and use directly the

myPort = new Serial(this, Serial.list()[0], 9600); // change [0] to [..] correct port

so no need to type the name…


for the write, you only send once if needed,
so like from

void keyPresssed() {
  if ( key == '1' ) myPort.write('1');
  if ( key == '0' ) myPort.write('0');
}

and on the Arduino side find again
1 or 0 AS character !
that code there would be testable also from arduino monitor
( again by type 1 0 from keyboard ? but there must press enter? )

what i want to say is stay on the ( easy diagnostic ) ASCII level
until there is no other way as to write byte stream / own protocol …


mouse over circle is not good idea,
better mouse over circle and click send once is better.

here would use the

void mousePressed() {
  if ( over_circle(x0,y0,r0) ) myPort.write('0');
  if ( over_circle(x1,y1,r1) ) myPort.write('1');
}

or use one circle and a toggle bit ( memory )
anyhow the

boolean over_circle(x,y,r) {}

you should write first.

1 Like

Using the “Simple Write serial example” in my Processing 3.5.3…

Consider setting the “state” and only sending data if there is a “state” change.

  if (mouseOverRect() == true  &&  state != 'H') 
    { 
    // Do something if last state was not 'H' ( 'L' is the other state in example)
    state = 'H';
    }

This worked for me and I only send data once when mouse is over the rectangle.

You will have to complete the rest of the code and also declare state variable and initial state.

image

:slight_smile:

1 Like

Thank you kll and glv for your replies, you’ve given me something to work on.

1 Like

I have modified the “SimpleWrite” example here:

A search a while back revealed that it was causing issues that impacted some (I am one of them) and this is a working solution for this example.

:slight_smile:

1 Like