Back to Light Meter

//Processing Code // Solar Cell - Light Meter // John Schimmel - http://www.base2john.com // initial code by Tom Igoe

import processing.serial.*;

Serial myPort; // The serial port boolean firstContact = false; int valueToGraph,xpos,i; int pX, pY; int zoom = 10; //actually controls the delay

void setup() { size(400,255);

doBackGround();

  // List all the available serial ports 
  println(Serial.list()); 

  // keyspan on my powerbook is in slot 3 or [2]
  myPort = new Serial(this, Serial.list()[2], 9600); 
  myPort.write(65);

}

void draw() {

  while (myPort.available() > 0) {
    processSerial();
    firstContact=true;
  }

  if (firstContact == false) {
    delay(300);
    myPort.write(65);
  }

}

void drawGraph (int value) {

    stroke(0,255,0);
    strokeWeight(1);
    // draw the line:
    line(pX, pY, i, height-value);

  // at the edge of the screen, go back to the beginning:
  if (i >= width-2) {
    i = 0;
    pX = 0;
    doBackGround();
  } 
  else {
    pX = i;
    i++;
  }

 pY = height-value; //previous y.

}

void processSerial() { // Expand array size to the number of bytes you expect

  byte[] inBuffer = new byte[1]; 

    inBuffer = myPort.readBytes(); 
    myPort.readBytes(inBuffer); 
    if (inBuffer != null) { 
      String myString = new String(inBuffer);
      int value = int(myString); 
      //println(myString);
      drawGraph(value);
    } 
  delay(zoom);
  myPort.write(65); 

}

void doBackGround() { //make a gradient white on top - black on bottom background println("clear screen");

  for (int i=0;i<height;i++) {
    stroke(height-i);
    line(0,i,width,i);
  }

}