Last active
March 22, 2016 15:10
-
-
Save scottswaaley/5b1e26218a7567be313f 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
/* | |
A Processing sketch takes an analog 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); | |
// the map() function converts the values of a variable to a more convenient range | |
int mappedHeight = (int)map(dataVal,0,800,0,height); | |
int mappedColor = (int)map(dataVal,0,800,0,255); | |
fill(mappedColor); | |
rectMode(CENTER); | |
rect(width/2,height/2,width/2,mappedHeight); | |
} | |
// 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