Created
April 10, 2017 16:52
-
-
Save anonymous/4b906d376248d37dfeab456ec144d253 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//import serial library | |
import processing.serial.*; | |
//create sensor values | |
int sensorVal1; | |
int sensorVal2; | |
int sensorVal3; | |
//create and instance of the serial libaray | |
Serial myPort; | |
//create an instance of a font | |
PFont f; | |
void setup() { | |
//set up the size fo the screen | |
size(570, 500); | |
//my port is: /dev/tty.usbmodem1411 | |
String portName = "/dev/tty.usbmodem1411"; | |
myPort = new Serial(this, portName, 9600); | |
//create a font of size 16 Helvetica | |
f = createFont("Helvetica", 16, true); | |
} | |
void draw() { | |
// create a background that changed color based on the input from 3 potentiometers | |
background(sensorVal1, sensorVal2, sensorVal3); | |
// create strings to hold the values of each potentiometer | |
String message1 = "Red: " + sensorVal1; | |
String message2 = "Green: " + sensorVal2; | |
String message3 = "Blue: " + sensorVal3; | |
textFont(f,36); | |
// change the color of the display text based on the amount of red and green in the background color | |
if (sensorVal1>150 || sensorVal2>150) { | |
fill(0); | |
} else { | |
fill(255); | |
} | |
// display the text | |
text(message1,10,35); | |
text(message2,190,35); | |
text(message3,410,35); | |
} | |
//listening for serial events | |
void serialEvent(Serial myPort) { | |
String input = myPort.readStringUntil('\n'); | |
if (input != null) { | |
input = trim(input); | |
int[] sensors = int(split(input, ',')); | |
println(sensors); | |
// if the length of the sensors array is longer than 2 | |
if (sensors.length > 2) { | |
//set each variable to its given value from the potentiometer | |
sensorVal1 = sensors[0]; | |
sensorVal2 = sensors[1]; | |
sensorVal3 = sensors[2]; | |
} | |
} | |
myPort.write('x'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment