Skip to content

Instantly share code, notes, and snippets.

@scottswaaley
Last active March 22, 2016 15:09
Show Gist options
  • Save scottswaaley/e8183796c76470380003 to your computer and use it in GitHub Desktop.
Save scottswaaley/e8183796c76470380003 to your computer and use it in GitHub Desktop.
/*
A Processing sketch takes a digital input from the serial bus and uses it to scale a color a rectangle.
*/
import processing.serial.*;
// this selects the port number for your computer. If the code has trouble, try
// changing this value to a number between 0 and 9
int portNum = 3; // selects the port number
Serial myPort; // The serial port
String inVal; // Input string from serial port
int lf = 10; // ASCII linefeed
int dataVal;
void setup() {
//sets the size of the canvas
size(400,400);
// prints out a list of all the serial ports connected to your computer and can
// help with selecting the port number
printArray(Serial.list());
// this code prepares the sketch to receive serial data
myPort = new Serial(this, Serial.list()[portNum], 9600);
myPort.bufferUntil(lf);
}
void draw() {
background(255);
if(dataVal == 1) fill(255);
else fill(0);
rectMode(CENTER);
rect(width/2,height/2,width/2,height/2);
}
// this function will run automatically every time data is received.
void serialEvent(Serial p) {
//readString() reads the incoming serial data as a string.
inVal = p.readString();
//trim() takes the string it just received and trims off any word spaces or formatting
inVal = trim(inVal);
// int() converts the string to an integer
dataVal = int(inVal);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment