Arduino code to send data to Processing program: (Processing code follows Arduino code)

/*

  Sending three pieces of data - the original data, a weighted average and the standard deviation,
  deliminated by a header "A", "B", "C" at the beginning and a line break at the end

  based on stuff from Melvin Ochsmann and Tom Igoe. 
  reworked a bit for Sensor Workshop class by Jamie Allen, 2007
  • /

int blinkPin = 13; // LED connected to digital pin 13 int analogPin = 2; // sensor output connected to Analog Pin 2

int greenLED = 7; // green LED - digital pin 6 int blueLED = 8; // blue LED - digital pin 7 int redLED = 9; // red LED - digital pin 8

int sensorValue = 0; // variable to store the ADC value of the TSL13S

void setup() {

  pinMode(blinkPin, OUTPUT);  // declare the blinkPin as an OUTPUT
  pinMode(greenLED, OUTPUT);  // declare the greenLED pin as an OUTPUT
  pinMode(blueLED, OUTPUT);   // declare the blueLED pin as an OUTPUT
  pinMode(redLED, OUTPUT);    // declare the redLED pin as an OUTPUT

  Serial.begin(9600);	// opens serial port, sets data rate to 9600 bps

}

void loop() {

    ///RED LED ///////////
    digitalWrite(blinkPin, HIGH); // sets the blinkLED on
    digitalWrite(redLED, LOW);    // sink redLED pin (on)
    delay(10);
    Serial.print("A");  // header variable, so we know which sensor value is which
    delay(50);

    sensorValue = analogRead(analogPin);  // read the value from the sensor
    Serial.print(sensorValue, DEC);  //send as a ascii encoded number - we'll turn it back into a number at the other end
    delay(50);
    Serial.print(10, BYTE);  //terminating character
    delay(50);

    digitalWrite(blinkPin, LOW); // blinkLED off
    digitalWrite(redLED, HIGH);  // source redLED pin (off)
    delay(25);

    ///GREEN LED ///////////
    digitalWrite(blinkPin, HIGH);  // sets blinkLED on
    digitalWrite(greenLED, LOW);   // sink greenLED pin
    delay(10);
    Serial.print("B");  //header variable, so we know which sensor value is which
    delay(50);

    sensorValue = analogRead(analogPin);  // read the value from the sensor
    Serial.print(sensorValue, DEC);  //send as a ascii encoded number - we'll turn it back into a number at the other end
    delay(50);
    Serial.print(10, BYTE);  //terminating character
    delay(50);

    digitalWrite(blinkPin, LOW);  // blinkLED off
    digitalWrite(greenLED, HIGH); // source greenLED pin
    delay(25); 

    ///BLUE LED ///////////
    digitalWrite(blinkPin, HIGH); // blinkLED on
    digitalWrite(blueLED, LOW); // sink blueLED pin
    delay(10);
    Serial.print("C");  //header variable, so we know which sensor value is which
    delay(50);

    sensorValue = analogRead(analogPin);  // read the value from the sensor
    Serial.print(sensorValue, DEC);  //send as a ascii encoded number - we'll turn it back into a number at the other end
    delay(50);
    Serial.print(10, BYTE);  //terminating character
    delay(50);

    digitalWrite(blinkPin, LOW); // blinkLED off
    digitalWrite(blueLED, HIGH); // source blueLED pin
    delay(25);

    delay(1000);
  }




//Processing Code:

/* This is a set of statistics test for your sensor data It's the same as the other data viewers on this end, but it expects three values: 1) red value 2) green value 3) blue value

It then graphs these values (with FAKE hysteresis)

This program takes raw bytes from the serial port that are seperated by headers "A", "B", etc. at 9600 baud and graphs them.

Based on Tom Igoe's & Melvin Ochsmann's work done up for sensor workshop, Jamie Allen, 2007 slightly modified by christopher kucinski for a color scanner April 2007

  • /

import processing.serial.*;

Serial myPort; // The serial port PFont myFont; // The display font

// initial variables: String buff = ""; int val = 0; int NEWLINE = 10; int i = 1; // counter int valueA, valueB, valueC; // the converted data from serial port

int[] valuesA = new int[10]; int[] valuesB = new int[10]; int[] valuesC = new int[10];

float valNormA, valNormB, valNormC; // normalized values of A, B, C

String bufA="", bufB="", bufC=""; // buffers in which to store ascii data as it comes in int buf;

int wrote = 0; int legendary = 0; int offset = 0; int offsettext = 25; int lf = 10;

void setup () {

  size(500, 500);        // window size
  frameRate(60);

  myFont = loadFont("ArialMT-12.vlw"); 
  textFont(myFont, 12); 
  textAlign(LEFT);
  fill(#E9FF5B, 200);
  smooth(); 

  // set inital background:
  background(0);
  strokeCap(ROUND);
  ellipseMode(CENTER);
  strokeWeight(3);

  println(Serial.list()); // List all the available serial ports
  // The third port in the serial list on my mac is always 
  //the USB/Serial connected to arduino, so I open Serial.list()[2].
  myPort = new Serial(this, Serial.list()[2], 9600);
  legend();

}

void draw() {

  fill(0,1);
  rect(0,0, width, height); 

  while (myPort.available() > 0) {
    serialEvent(myPort.read());
  }

  //scale and normalize values
  valNormA = (valueA*5)/1023.0;
  valNormB = (valueB*5)/1023.0;
  valNormC = (valueC*5)/1023.0;

  //RED Graph
  stroke(255, 0, 0);
  fill(255, 0, 0);
  ellipse(i, height - valNormA*height, 7, 7);

  //GREEN Graph
  stroke(0, 255, 0);
  fill(0, 255, 0);
  ellipse(i, height - valNormB*height, 7, 7);

  //BLUE Graph
  stroke(0, 0, 255);
  fill(0, 0, 255);
  ellipse(i, height - valNormC*height, 7, 7);

  // Rectangle that will show the scanned color:
  fill(valNormA * 256, valNormB * 256, valNormC * 256);
  rect(250, 20, 100, 75);


  // at the edge of the screen, go back to the beginning:
  if (i > width) {
    i = 0;
    //background(#000000);
    fill(0,1);
    rect(0,0, width, height); 
  } 
  else {
    i++;
  }

  // display the value once every 75 readings to keep screen clean 
  if (wrote > 75)
  {
    fill(#FFFFFF);
    text(valNormA, i, (height - valNormA*height)-15);
    text(valNormB, i, (height - valNormB*height)-30);
    text(valNormC, i, (height/2 - valNormC*height)-45);
    wrote = 0;
  }
  wrote++;


  if (legendary == 9){legend(); legendary = 0;}
  legendary++;

}

//Serial parsing stuff to get the raw values from the serial event void serialEvent(int serial){

  if(serial!=10) {      
      if (serial=='A') buf = 1;
      if (serial=='B') buf = 2;
      if (serial=='C') buf = 3;

      if (buf==1) {if (serial!='A') bufA += char(serial);}
      else if (buf==2) { if (serial!='B') bufB+= char(serial);}
      else if (buf==3) { if (serial!='C') bufC+= char(serial);}
   } else {
      if (buf==1)        {valueA = int(bufA); bufA="";} 
      else if (buf==2)   {valueB = int(bufB); bufB="";}
      else if (buf==3)   {valueC = int(bufC); bufC="";}
   }      
      println("a = " + valueA);      
      println("b = " + valueB);
      println("c = " + valueC);
      println();

}

void legend() {

  stroke(255, 0, 0);
  fill(255, 0, 0);
  ellipse(width/20, height/30, 7, 7);

  stroke(0, 255, 0);
  fill(0, 255, 0);
  ellipse((width/20), (height/30)+20, 5, 5);

  stroke(0, 0, 255);
  fill(0, 0, 255);
  ellipse((width/20), (height/30)+40, 2, 2);

  fill(#FFFFFF,255);
  text("RED:", (width/20)+10, 5+(height/30)); 
  text("BLUE:", (width/20)+10, 5+(height/30)+20); 
  text("GREEN", (width/20)+10, 5+(height/30)+40); 

}