// Processing Code:
/*-------------------------------------------------------------------------
* Simple Reading of asynchronous serial data, with screen font
* Reads data from the serial port, changes the properties of a rectangle
*
* Designed to work with Arduino "a16_Serial_Analog_ThrowASCII"
*
* Serial Event example
* (adapted from code by Tom Igoe)
---------------------------------------------------------------------------*/
import processing.serial.*;
String serialString = ""; // a string to hold incoming data
Serial myPort; // Create object from Serial class
PFont f; // font object (for graphical text)
int val = 100; // Data received from the serial port
String Data = "Start"; // String variable for drawing serial data
void setup()
{
size(400, 200);
background(255); // Set background to white
println(Serial.list()); // Print list of available serial ports
// Usually, the first port in the serial list on a mac
// is always the FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
String portName = Serial.list()[2]; // Mac version
myPort = new Serial(this, portName, 9600);
//----------- Create the font -----------
// printArray(PFont.list());
f = createFont("Verdana", 24);
textFont(f);
textAlign(CENTER);
fill(90, 100); // Set the gray value and alpha of the letters
text(Data, 190, 160); // draws string variable at x, y
}
void draw()
{
background(255);
fill(val);
rect(50, 40, val + 50, 100);
fill(200, 50, 50);
Data = str(val); // cast serial data to a String, store in "Data"
text(Data, 200, 170); // draws string variable (from serial int) at x, y
}
//---- The SerialEvent function runs continuously in the background and
//---- stores new values whenever new data comes in on the serial port (USB)
void serialEvent(Serial myPort) {
serialString = myPort.readStringUntil(10); // read until line feed
if (serialString != null) {
serialString = trim(serialString);
val = int(serialString);
// println(val);
} // end if
}
|