# Processing to Arduino Serial Library Example

**URL:** https://discourse.processing.org/t/processing-to-arduino-serial-library-example/11143
**Category:** Electronics (Arduino, etc.)
**Created:** [May 11, 2019, 11:04pm UTC](https://discourse.processing.org/t/processing-to-arduino-serial-library-example/11143 "2019-05-11T23:04:57Z")
**Posts on this page:** 6
**Page:** 1

<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: [May 11, 2019, 11:04pm UTC](https://discourse.processing.org/t/processing-to-arduino-serial-library-example/11143/1 "2019-05-11T23:04:58Z")

</div>

The SimpleWrite example in the Processing examples was not working on PCs I had available to me.

> <https://github.com/processing/processing/blob/master/java/libraries/serial/examples/SimpleWrite/SimpleWrite.pde>

The Arduino resets when establishing a serial connection.

I added a delay(1000) to allow the Arduino to reset after serial connection to Processing and it now works.

I modified the Processing setup() as follows:

```auto
void setup() 
  {
  size(200, 200);
  printArray(Serial.list()); //Added! List all the available serial ports
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
  delay(1000); //Added! Allow time for Arduino to reset
  }

```

References:  
[https://dev.playground.arduino.cc/Main/DisablingAutoResetOnSerialConnection](https://dev.playground.arduino.cc/Main/DisablingAutoResetOnSerialConnection)

Hardware:  
Arduino Mega 2560 R3  
Dell Optiplex 7010

Software:  
Windows 10 1809  
Arduino IDE 1.8.9  
Processing 3.5.3

---

<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: [May 12, 2019, 12:43am UTC](https://discourse.processing.org/t/processing-to-arduino-serial-library-example/11143/2 "2019-05-12T00:43:03Z")

</div>

now that delay would only be needed if you want to write to the arduino  
at the beginning of your program.

for usual operation the serial event will tell you when some data FROM arduino have arrived, so it is not needed in that case.

the arduinos what reset at serial connection ( not all do that (Leonardo) ) also reset at  
at end of communication, a mostly unknown thing, but if you want to setup a arduino  
by some initial serial commands and then let it run alone ( disconnect )  
that will fail unless you write all settings ( changes) to eeprom ( and read from it at boot ).

---

<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: [September 22, 2019, 11:03pm UTC](https://discourse.processing.org/t/processing-to-arduino-serial-library-example/11143/3 "2019-09-22T23:03:00Z")

</div>

I am working with an **existing example** provided with Processing in this discussion.

> [@kll](#):
>
> now that delay would only be needed if you want to write to the arduino  
> at the beginning of your program.

The example is writing TO the Arduino.

> [@kll](#):
>
> for usual operation the serial event will tell you when some data FROM arduino have arrived, so it is not needed in that case.

The example is writing TO the Arduino.

The Processing example worked after I added the delay(1000).

🙂

---

<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: [September 23, 2019, 1:20am UTC](https://discourse.processing.org/t/processing-to-arduino-serial-library-example/11143/4 "2019-09-23T01:20:40Z")

</div>

> [@glv](#):
>
> I added the delay(1000)

this also depends on the arduino board ( leonardo need this setup wait for serial )  
and other arduino init routines…

so again, why not just wait for a info FROM arduino “READY” ( like a handshake )  
( assuming that processing serial start causes a reboot of the arduino anyway )

---

<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:01pm UTC](https://discourse.processing.org/t/processing-to-arduino-serial-library-example/11143/5 "2019-11-09T23:01:33Z")

</div>

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:

```auto
/**
 * Simple Write. 
 * 
 * Check if the mouse is over a rectangle and writes the status to the serial port. 
 * This example works with the Wiring / Arduino program that follows below.
 */

import processing.serial.*;

Serial myPort; // Create object from Serial class
int val; // Data received from the serial port

int state = 'L'; //GLV added

void setup() 
{
  size(200, 200);
  // I know that the first port in the serial list on my mac
  // is always my FTDI adaptor, so I open Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
  delay(1000); //GLV added Wait for Arduino to reset
}

void draw() {
  background(255);
  //if (mouseOverRect() == true ) { // If mouse is over square,
  if (mouseOverRect() == true && state != 'H' ) { // If mouse is over square, //GLV modified
    fill(204); // change color and
    myPort.write('H'); // send an H to indicate mouse is over square
  println('H'); //GLV added
  state = 'H'; //GLV added
  } 
// else { // If mouse is not over square,
  if (mouseOverRect() == false && state != 'L' ) { // If mouse is over square, //GLV modified

    fill(0); // change color and
    myPort.write('L'); // send an L otherwise
    println('L'); //GLV added
    state = 'L'; //GLV added
  }
  rect(50, 50, 100, 100); // Draw a square
}

boolean mouseOverRect() { // Test if mouse is over square
  return ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150));
}

```

🙂

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [November 11, 2019, 11:44pm UTC](https://discourse.processing.org/t/processing-to-arduino-serial-library-example/11143/6 "2019-11-11T23:44:32Z")

</div>

@glv – have you considered submitting this to the library, whether as a change to the original or as a “SimpleWrite2” example?
