Skip to content

Instantly share code, notes, and snippets.

@dustynrobots
Created October 11, 2012 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustynrobots/3873494 to your computer and use it in GitHub Desktop.
Save dustynrobots/3873494 to your computer and use it in GitHub Desktop.
Processing - Punctuation
import processing.serial.*; // import the Processing serial library
Serial myPort; // The serial port
float bgcolor = #FAE312; // Background color
float fgcolor; // Fill color
float xpos, ypos; // Starting position of the ball
int sensors[];
void setup() {
size(800, 600);
// List all the available serial ports
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Arduino module, so I open Serial.list()[0].
// Change the 0 to the appropriate number of the serial port
// that your microcontroller is attached to.
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
// read bytes into a buffer until you get a linefeed (ASCII 10):
myPort.bufferUntil('\n');
}
void draw() {
background(bgcolor);
fill(fgcolor);
// Draw the shape
ellipse(xpos, ypos, 20, 20);
// make sure there are three values before you use them:
if (sensors.length > 1) {
xpos = sensors[0];
ypos = sensors[1];
// the switch values are 0 and 1. This makes them 0 and 255:
fgcolor = sensors[2] * 255;
}
}
void serialEvent(Serial myPort) {
// read the serial buffer:
String myString = myPort.readStringUntil('\n');
if (myString != null) {
myString = trim(myString);
// split the string at the commas
// and convert the sections into integers:
sensors = int(split(myString, ','));
// print out the values you got:
for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
}
// add a linefeed after all the sensor values are printed:
println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment