Skip to content

Instantly share code, notes, and snippets.

@unknowndomain
Created March 23, 2015 16:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save unknowndomain/6f3eb4b6f5bef631bf85 to your computer and use it in GitHub Desktop.
Save unknowndomain/6f3eb4b6f5bef631bf85 to your computer and use it in GitHub Desktop.
This is an example of how to connect to an Arduino automatically in Processing
import processing.serial.*;
Serial arduino;
void setup() {
// Create a canvas
size( 800, 600 );
// Get a list of serial ports
String ports[] = Serial.list();
int id = 0;
// Find an Arduino tty.usb* port
for ( int i = 0; i < ports.length; i++ ) {
if ( ports[i].indexOf( "tty.usb" ) != -1 )
id = i;
}
// Connect to the Arduino
println( "Connecting to: " + ports[id] );
arduino = new Serial( this, ports[id], 9600 );
}
void draw() {
}
// New Serial Data Event
void serialEvent(Serial p) {
println( p.readChar() );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment