Processing to Arduino Serial Library Example

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

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:

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

Hardware:
Arduino Mega 2560 R3
Dell Optiplex 7010

Software:
Windows 10 1809
Arduino IDE 1.8.9
Processing 3.5.3

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 ).

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

The example is writing TO the Arduino.

The example is writing TO the Arduino.

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

:slight_smile:

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 )

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 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));
}

:slight_smile:

3 Likes

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

1 Like