// Processing Code:
/*-------------------------------------------------------------------------
 * Simple Reading of asynchronous serial data,
 * Reads data from the serial port, changes the properties of a rectangle
 * 
 * Designed to work with Arduino "a16_Serial_Send_Analog"
 *
 *     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
int val = 100;                   // Data received from the serial port

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);
}

void draw()
{
  background(255);
  fill(val);           
  rect(50, 40, val + 50, 100);
}

//---- 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 char (10)
   if (serialString != null) {
     serialString = trim(serialString); 
     val = int(serialString);
     println(val);
   }                               // end if
}