# Processing, Arduino and Linux

**URL:** <https://discourse.processing.org/t/processing-arduino-and-linux/15344>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [November 9, 2019, 5:37am UTC](https://discourse.processing.org/t/processing-arduino-and-linux/15344 "2019-11-09T05:37:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pandl](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@pandl](https://discourse.processing.org/u/pandl)\
**Post date:** [November 9, 2019, 5:37am UTC](https://discourse.processing.org/t/processing-arduino-and-linux/15344/1 "2019-11-09T05:37:49Z")

</div>

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

```auto
  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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 9, 2019, 8:33am UTC](https://discourse.processing.org/t/processing-arduino-and-linux/15344/2 "2019-11-09T08:33:58Z")

</div>

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

```auto
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

```auto
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

```auto
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

```auto
boolean over_circle(x,y,r) {}

```

you should write first.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [November 9, 2019, 2:36pm UTC](https://discourse.processing.org/t/processing-arduino-and-linux/15344/3 "2019-11-09T14:36:17Z")

</div>

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.

```auto
  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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/d8ba1d791eddc2e6fa297f0d707ff36a7fb1e9b1.png)

🙂

---

<div class="post-metadata">

**Author:** ![pandl](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@pandl](https://discourse.processing.org/u/pandl)\
**Post date:** [November 9, 2019, 10:22pm UTC](https://discourse.processing.org/t/processing-arduino-and-linux/15344/4 "2019-11-09T22:22:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [November 9, 2019, 11:08pm UTC](https://discourse.processing.org/t/processing-arduino-and-linux/15344/5 "2019-11-09T23:08:35Z")

</div>

I have modified the “SimpleWrite” example here:

> [@Processing to Arduino Serial Library Example](https://discourse.processing.org/t/processing-to-arduino-serial-library-example/11143/5):
>
> I modified the existing “SimpleWrite” example to: save and check for the state of mouseOverRect() print state to console only writes serial data once a delay() to wait for Arduino to reset once connected to it; in my case, it is an Arduino Mega 2560 R3. I kept the spirit of the original example intact and left it as a “Simple Write” program. My edit of “SimpleWrite” example so it only sends data once: /\*\* \* Simple Write. \* \* Check if the mouse is over a rectangle and writes the status …

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.

🙂
